java的map

目錄

1.HashMap

2.TreeMap

4.map排序

4.1key排序

4.2value排序

1.HashMap

import java.util.*;

public class map {
    public static void main(String args[]){
        HashMap<String, Integer> map = new HashMap();
        //增加
        map.put("a1",1);
        HashMap<String, Integer> map1 = new HashMap();
        map1.put("a2",2);
        map.putAll(map1);//{a1=1, a2=2}
        //刪除
        map.remove("a1");//{a2=2}
        //修改
        map.replace("a2",1);//{a2=1}
        map.replace("a2",2);
        //查尋
        map.put("a1",1);
        Boolean isEmpty = map.isEmpty();//false
        Boolean containsValue = map.containsValue("a1");//false
        Boolean containsKey = map.containsKey("a1");//true
        Set<String> keySett = map.keySet();//[a1, a2]
        Collection<Integer> values = map.values();//[1, 2]
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();//[a1=1, a2=2]
        Integer get = map.get("a1");//1
        Integer getOrDefault = map.getOrDefault("a3",3);//3
        Integer size = map.size();//2
        //System.out.println(values);
        //遍歷
        for(Integer value : map.values()){
            System.out.println(value);
        }
        for(String key : map.keySet()){
            Integer value = map.get(key);
            System.out.println(key+":"+value);
        }
        for(Map.Entry<String, Integer> entry : map.entrySet()){
            String Key1 = entry.getKey();
            Integer Value1 = entry.getValue();
            System.out.println(Key1+":"+Value1);
        }
        Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
        while(entries.hasNext()){
            Map.Entry<String, Integer> entry = entries.next();
            String key2 = entry.getKey();
            Integer value2 = entry.getValue();
            System.out.println(key2+":"+value2);
        }
        Set<Map.Entry<String,Integer>> entrySet1 = map.entrySet();
        Iterator<Map.Entry<String,Integer>> iterators = entrySet1.iterator();
        while(iterators.hasNext()){
            Map.Entry<String,Integer> entrys  = iterators.next();
            System.out.println("key="+entrys.getKey()+"&&value="+entrys.getValue());
        }
    }
}

2.TreeMap

import java.util.*;

public class map {
    public static void main(String args[]){
        TreeMap<String, Integer> map = new TreeMap();
        //增加
        map.put("a2",2);
        TreeMap<String, Integer> map1 = new TreeMap();
        map1.put("a1",1);
        map.putAll(map1);//{a1=1, a2=2}
        //刪除
        map.remove("a1");//{a2=2}
        map.put("a1",1);//{a1=1, a2=2}
        //修改
        map.replace("a2",1);//{a1=1, a2=1}
        map.replace("a2",2);//{a1=1, a2=2}
        //修改和刪除
        Map.Entry<String, Integer> pollFirstEntry = map.pollFirstEntry();//{a1=1}
        Map.Entry<String, Integer> pollLastEntry = map.pollLastEntry();//{a2=2}
        System.out.println(map);//{}
        //查尋
        map.put("a1",1);
        map.put("a2",2);
        Boolean isEmpty = map.isEmpty();//false
        Boolean containsValue = map.containsValue("a1");//false
        Boolean containsKey = map.containsKey("a1");//true
        Set<String> keySett = map.keySet();//[a1, a2]
        Collection<Integer> values = map.values();//[1, 2]
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();//[a1=1, a2=2]
        Integer get = map.get("a1");//1
        Integer getOrDefault = map.getOrDefault("a3",3);//3
        Integer size = map.size();//2
        String firstKey = map.firstKey();//a1
        String lastKey = map.lastKey();//a2
        Map.Entry<String, Integer> firstEntry = map.firstEntry();//a1=1
        Map.Entry<String, Integer> lastEntry = map.lastEntry();//a2=2
        map.put("b1",1);
        map.put("b2",2);
        String floorKey = map.floorKey("b2");//b2
        Map.Entry<String, Integer> floorEntry = map.floorEntry("b2");//b2=2
        String lowerKey = map.lowerKey("b2");//b1
        Map.Entry<String, Integer> lowerEntry = map.lowerEntry("b2");//b1=1
        String higherKey = map.higherKey("b1");//b2
        Map.Entry<String, Integer> higherEntry = map.higherEntry("b1");//b2=2
        //獲取子map
        SortedMap<String, Integer> subMap = map.subMap("a1","b1");//{a1=1, a2=2}
        SortedMap<String, Integer> tailMap = map.tailMap("a2");//{a2=2, b1=1, b2=2}
        //遍歷
        for(Integer value : map.values()){
            System.out.println(value);
        }
        for(String key : map.keySet()){
            Integer value = map.get(key);
            System.out.println(key+":"+value);
        }
        for(Map.Entry<String, Integer> entry : map.entrySet()){
            String Key1 = entry.getKey();
            Integer Value1 = entry.getValue();
            System.out.println(Key1+":"+Value1);
        }
        Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
        while(entries.hasNext()){
            Map.Entry<String, Integer> entry = entries.next();
            String key2 = entry.getKey();
            Integer value2 = entry.getValue();
            System.out.println(key2+":"+value2);
        }
        Set<Map.Entry<String,Integer>> entrySet1 = map.entrySet();
        Iterator<Map.Entry<String,Integer>> iterators = entrySet1.iterator();
        while(iterators.hasNext()){
            Map.Entry<String,Integer> entrys  = iterators.next();
            System.out.println("key="+entrys.getKey()+"&&value="+entrys.getValue());
        }
    }
}

4.map排序

4.1key排序

import java.util.*;

public class map {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("c", "ccccc");
        map.put("a", "aaaaa");
        map.put("b", "bbbbb");
        map.put("d", "ddddd");

//        Map<String, String> sortMap = new TreeMap<String, String>(
//                new Comparator<String>() {
//                    public int compare(String obj1, String obj2) {
//                        // 降序排序
//                        return obj2.compareTo(obj1);
//                    }
//                });
        Map<String, String> sortMap = new TreeMap<String,String>((o1, o2) -> o2.compareTo(o1));

        sortMap.putAll(map);

        Set<String> keySet = sortMap.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":" + map.get(key));
        }
    }
}

4.2value排序


import java.util.*;

public class map {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("a", "cf");
        map.put("b", "ca");
        map.put("c", "bf");
        map.put("d", "aa");

        List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
            //升序排序
            public int compare(Map.Entry<String, String> o1,
                               Map.Entry<String, String> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }

        });
        //Collections.sort(list, (m1,m2)->(m1.getValue().compareTo(m2.getValue())));

        for(Map.Entry<String,String> mapping:list){
            System.out.println(mapping.getKey()+":"+mapping.getValue());
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章