map-java集合

Map集合
-------------
1、Map与Collection不同
2、Map集合存储于取出元素的方式
3、Map集合的特点
4、Map集合中常用类

Map与Collection
1、Map与Collection在集合框架中属并列存在
2、Map存储的是键值对
3、Map存储元素使用put方法,Colletion使用add方法
4、Map集合没有直接取出元素的方法,而是先转成Set集合,在通过迭代获取元素
5、Map集合中键要保证唯一性

hashMap的每个元素叫条目
所有的kev位于set当中

1、Hashtable:线程安全,速度慢,不允许存放null键,null值,已被HashMap替代
2、HashMap:线程不安全,速度快,允许存放null键,null值
3、TreeMap: 对键进行排序,排序原理与TreeSet相同
----------------------

拿出一个Map得到所有的kev:
Set set=map.keySet();是Map集合的方法
map.get(kev) //通过kev获得value

//Map代码案例参考:

public static void main(String[] args) {
  Map<String,Dog> map = new HashMap<String, Dog>();
  Dog dog = new Dog ("" , 12, "");
  map.put(null, null);
  map.put("dog-1000-1981", dog);
  map.put("dog-1000-1982", dog);
  map.put("dog-1000-1983", new Dog("",12,""));
  map.put("dog-1000-1984",new Dog("",12,""));
  map.put("dog-1000-1985",new Dog("",12,""));
  System.out.println(map.size());
  
  Set<Entry<String,Dog>> entries = map.entrySet();
  //循环map的所有的entry
  for(Entry<String,Dog> entry : entries){
    String key = entry.getKey();
    Dog d = entry.getValue();
    System.out.println(key + ":" + d.toString());
  }
  //map删除
  Dog d2 = map.remove("dog-1000-1984");
  System.out.println("删除了" + d2);
  
  d2 = map.remove("dog -1000-1984");
  System.out.println("删除了" + d2);
  
  //通过key遍历map
  Set<String> keySet = map.keySet();
  for(String str : keySet){
   Dog value =map.get(str);  
  }
  
 }

}


------------------------

TreeSet
-----------------------------
1、使用比较方法判断对象是否重复
2、比较方法实现有两种
 a、自定义Comparator比较器,和TreeeSet关联
 b、让javaBean实现Comparable接口,实现CompareTo()方法
3、TreeSet可以容纳null元素
4、TreeSet可以使用降序排序,通过descendingIterator()方法得到降序迭代器实现
5、TreeSet默认升序排列
6、TreeSet是依靠TreeMap来实现的。
TreeSet是一个有序集合,很多java视频里面都是这样讲的,TreeSet中的元素将按照升序排列,缺省是按照自然排序进行排列,意味着TreeSet中的元素要实现Comparable接口。或者有一个自定义的比较器。
我们可以在构造TreeSet对象时,传递实现Comparator接口的比较器对象。
 
 

-------------------

public class TeerSet {
 private static final Comparator<? super Person> comp = null;
 public static void main(String[] args) {
  /*
   * 自定义比较器,实现的是对象大小比较
   * 按照年龄判断大小情况
   */
  Comparable<Person> comp =new Comparable<Person>() {
   public int compareTo(Person o1,Person o2) {
    //o1==null
    if(o1==null){
     if(o2==null){      
      return 0;
     }
     else{
      return -1;
     }
    }
    //o1!=null
    else{
     if(o2==null){
      return 1;
     }
     else{
      return o1.getAge()-o2.getAge();
     }
    }
   }

   @Override
   public int compareTo(Person o) {
    // TODO Auto-generated method stub
    return 0;
   };
  };
  
 }
  //通过比较器构造TreeSet集合
 TreeSet<Person> tr=new TreeSet<Person> (comp);
 tr.add(null);
 tr.add(new Person("p1",10));
 tr.add(new Person("p2",20));
 tr.add(new Person("p3",30));
 tr.add(new Person("p4",40));
 
 System.out.println(tr.size());
 for(Iterator<Person> it=tr.descendingIterator();it.hadsNext();){
  Person p=it.next();
  System.out.println(p!=null ?p.getName():"Nobody");
 }
 /*
  * 使用comprable接口实现对象大小的比较
  * 
  */
 TreeSet<Dog> trr=new TreeSet<Dog>();
 trr.add(new Dog("black",20,"金巴"));
 trr.add(new Dog("black",12,"金巴"));
 trr.add(new Dog("white",20,"金巴"));
 trr.add(new Dog("black",20,"藏獒"));
 trr.add(new Dog("yellow",20,"藏獒"));
 
 System.out.println(trr.szie());
 //进行拍排序(升序输出),用遍历迭代器的方法(降序的话把trr.iterator改成trr.descendingIterator就ok)
 for(Iterator<Dog> it = trr.iterator();it.hasNext(){
  System.out.println(it.next());//调用了toString方法,it.next返回的是对象
 }

 }
}

posted @ 2016-04-01 09:12  jinshiyill  阅读(179)  评论(0编辑  收藏  举报