黑马程序员_java基础--集合框架(3)

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

Map

Map:顶层接口,该集合存储的是键值对,而且键是唯一的,Map和Set很像,Set集合底层就是使用了Map集合。
  Map集合没有迭代器,要取出元素必须先将Map集合转换成Set集合才能遍历元素
    |--->HashTable(JDK1.0):
  底层是哈希表数据结构;
  不可以使用null键和null值;
  用作键的对象必须实现hashCode和equals方法来保证键的唯一性
  线程同步,效率低
    |--->HashMap(JDK1.2):
  底层是哈希表数据结构;
  允许使用null键和null值;
  线程不同步,效率高;
  保证元素唯一性的:
    原理:先判断元素的hashCode值是否相同,再判断两元素的equals方法是否为true
    (往HashSet里面存的自定义元素要复写hashCode和equals方法,
    以保证元素的唯一性!)
  

class Student {
   private String name;
   private int age;
   public Student(String name, int age) {
    super();
    this.name = name;
    this.age = age;
   }
   public int getAge() {
    return age;
   }
   public void setAge(int age) {
    this.age = age;
   }
   public String getName() {
    return name;
   }
   public void setName(String name) {
    this.name = name;
   }
   
   @Override
   public int hashCode(){
    return name.hashCode()+age*34;
   }
   @Override
   public boolean equals(Object obj){
    
    if(!(obj instanceof Student))
     return false;
    Student stu = (Student)obj;
    return this.name.equals(stu.name)&&this.age==stu.age;
   }
  public class HashMapDemo1 {
   public static void main(String[] args) {
    Map<Student , String> hmap = new HashMap<Student , String>();
    hmap.put(new Student("001",20), "beijing");
    hmap.put(new Student("002",25), "hebei");
    hmap.put(new Student("003",50), "hainan");
    hmap.put(new Student("001",20), "beijing");
    
    System.out.println(hmap.size());
    Set<Student> keySet = hmap.keySet();
    Iterator<Student> it = keySet.iterator();
    while(it.hasNext()){
     Student stu = it.next();
     String addr = hmap.get(stu);
     System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);
    } 
   } 
  }   


 

    |--->TreeMap(JDK1.0):
  底层是二叉树结构;
  允许使用null键和null值;
  线程不同步;
  可以给Map集合中的键进行排序.
  TreeMap排序的第一种方式:让元素自身具备比较性,比如八种基本数据类型或则字符串,
     实现Compareble接口,覆盖compareTo方法,
     此方式是元素的自然顺序   
  TreeMap排序的第一种方式:当元素自身不具备比较性(比如存储学生对象时)或者具备的
     比较性不是我们所需要的比较性时(比如想字符串的长度排序),
     此时就需要让集合自身具备自定义的比较性。
     那如何让集合自身具备比较性呢?可在集合初始化时,
     就让集合具备比较方式。即定义一个类,
     实现Comparator接口,覆盖compare方法。
 

 class Student implements Comparable<Student>{
   private String name;
   private int age;
   public Student(String name, int age) {
    super();
    this.name = name;
    this.age = age;
   }
   public int getAge() {
    return age;
   }
   public void setAge(int age) {
    this.age = age;
   }
   public String getName() {
    return name;
   }
   public void setName(String name) {
    this.name = name;
   }
   @Override
   public int compareTo(Student stu) {
    int num = new  Integer(this.age).compareTo(new Integer(stu.age));
    if(num==0)
     return this.name.compareTo(stu.name);
    return num;
   }   
  }

  public class HashMapDemo1 {
   public static void main(String[] args) {
       
    Map<Student , String> tmap = new TreeMap<Student , String>();
    tmap.put(new Student("001",20), "beijing");
    tmap.put(new Student("002",25), "hebei");
    tmap.put(new Student("003",50), "hainan");
    tmap.put(new Student("001",20), "beijing");
    
    System.out.println(tmap.size());
    Set<Student> keySet1 = tmap.keySet();
    Iterator<Student> it1 = keySet1.iterator();
    while(it1.hasNext()){
     Student stu = it1.next();
     String addr = tmap.get(stu);
     System.out.println(stu.getName()+".."+stu.getAge()+"::"+addr);  
    }
   }
  }

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值