Map集合

来源:互联网 发布:帝国cms图片集分割 编辑:程序博客网 时间:2024/04/30 07:31


 

Map集合

Map<String ,Integer> map =newHashMap<>();

map.put("张三",100);

map.put("李四",100);

map.put("王五",100);

map.put("张三",66);            //键是唯一的,键如果相同,后面的值会把前面覆盖

 

System.out.println(map.size());  //打印为3

System.out.println(map);       //

 

HashSetHaseMap的关系

1.HashSetAdd方法底层

 

 

Map<String ,String> map =newHashMap<>();

map.put("张三","北京");

map.put("李四","北京");

map.put("王五","广州");

map.put("赵六","上海");

 

map.clear();       //清空集合中的元素

map.containsKey("张三")     //判断是否包含键,包含则返回true

map.containsValue("北京")   //判断是否包含的值,包含则返回true

 

map.get("张三")           //通过键获得值北京,但不能通过值获取键

map.isEmpty();           //判断是否为空

 

String val =map.remove("张三"); //通过键来删除,返回对应的值

Collection<String> coll =map.values(); //获取双列集合中的所有的值

 

map集合的两种迭代方式

   1、迭代器

  

 Set<String> setMap = map.keySet();        Iterator<String> it = setMap.iterator();        while(it.hasNext()){        String key = it.next();        Integer value = map.get(key);//通过键获取Value值        System.out.println(key+" = "+value);        }


   2、增强for循环

  

for(String str:map.keySet()){        System.out.println(str+" = "+map.get(str));        }interface Map{   interface Entry{              //  entry 正在封闭的借口         }}class Test implements Map.Entry{}


 

 

Map集合的迭代Map.Entry

    1 迭代器实现

    //将键值对象存储在Set集合里

    Set<Map.Entry<String, Integer>>entrySet = map.entrySet();

   //获取迭代器

        

  Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();        while(it.hasNext()){        Map.Entry<String, Integer> en = it.next();//获取集合的每个                                                             //键值对象        String key = en.getKey();          //通过键值对象获取键        Integer value = en.getValue();   //通过键值对象获取值        System.out.println(key+" = "+value);        }


 

     2增强for循环实现

        //map.entrySet代表键值对象的集合Entry

        for(Map.Entry<String,Integer> en:map.entrySet()){

            System.out.println(en.getKey()+"="+en.getValue());

        }

 

HashMap

//如果存的是对象如Person new("张三",20)需要重写hashCode()equals方法

HashMap<String,Integer>hm=new HashMap<>();

 

LinkedHashmap//怎么存进去怎么取出来

 

TreeMap<String,integer> tm=new TreeMap<>();//按照键排序

如果是比较对象可以实现Comparable接口重写compareTo()方法

Comparator接口中重写compare方法

 

Collections.sort(list)//升序,字典顺序

Collections.sort(list,Collections.reverseOrder())//对集合中的数据反转

 

Collections.sort(list,Collections.reverseOrder(newCompareBylen()));  //把比较器的顺序反转。

 

String[] arr={"a","b","c","d"};

List<String>list=Arrays.asList(arr);//数组转集合就是用集合的思想去考虑

//问题可以使用集合中除了改变长度的方法

 

int []arr2={12,32,43,54};

list<int []>list2 = Arrays.asList(arr2);//如果数组是基本类型的,转换

                             //为集合,会将整个数组当做一个对象存储到集合中

 

Integer[]arr3={12,32,43,54};

list<Integer[]> list2 = Arrays.asList(arr3)

 

String[] arr = list.toArray(newString[list.Size()])//集合转数组

                                         //只有一个好处,集合长度不能改变

 

静态导入(1.5版的新特性)

静态导入是导入类中的静态方法,只要类中有静态方法都可以使用静态导入。建议不用

 

HashtablehashMap

底层都是哈希算法

1hashtable是线程安全的,效率低jdk1.0

hashMap是线程不安全的,效率高,jdk1.2

2Hashtable不能存储null键和null

   HashMap

 

 

 泛型补充

? extends E固定上边界

 

ArrayList<Person> list1 = new ArrayList<>();list1.add(new Person("张三",23));list1.add(new Person("李四",24));ArrayList<Student> list2 = new ArrayList<>();list2.add(new Student("王五",25));list2.add(new Student("赵六",26));


 

? super E固定下边界

TreeSet<Person> list1 = new TreeSet<>(new CompareByAge());list1.add(new Person("张三",23));list1.add(new Person("李四",24));TreeSet<Student> list1 = new TreeSet<>(new CompareByAge());list2.add(new Student("王五",25));list2.add(new Student("赵六",26));class CompareByAge implements Comparator<Person>{   @Override   public int compare(Person p1,Person p2){     int num=p1.getAge-p2.getAge();      return num==0?p1.getName()-p2.getName():num;   }}


 

 

0 0
原创粉丝点击