跟上Java的脚步(二) 集合相关笔记 list map set(8&9)

这里我把之前的集合相关笔记重新简单整理了一下。(时间:2020年6月15日)

排序:

排序:

List<Integer> numbers = Arrays.asList(1,5,4,88,66,44);
        numbers.sort(Comparator.naturalOrder());
        System.out.println(numbers);

  之前我们这样写:

Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                int d= u1.getAge() - u2.getAge();
                if (d> 0) {
                    return 1;
                } else if (d< 0) {
                    return -1;
                } else {
                    return 0;// 相等为0
                }
            }
}); // 按年龄排序

  现在我们可以这样写:

 

List<Menu> result=
                menuList.stream()
                        .sorted(comparing(Menu::getPrice))
                        .collect(Collectors.toList());
        System.out.println(result);
View Code

map排序:

 

 

遍历:  

  之前我们的各种遍历集合:

/*Map<String,String> map = new HashMap<String,String>();
        map.put("999", "哈密瓜");
        map.put("4", "菠萝");
        map.put("2", "草莓");
        map.put("3", "香蕉");
        map.put("111", "榴莲");
        System.out.println(map);*/
        
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(99, "哈密瓜");
        map.put(4, "菠萝");
        map.put(2, "草莓");
        map.put(3, "香蕉");
        map.put(11, "榴莲");
        System.out.println(map);
        
        Map<Integer,String> map1 = new LinkedHashMap<Integer,String>();
        map1.put(99, "哈密瓜");
        map1.put(4, "菠萝");
        map1.put(2, "草莓");
        map1.put(3, "香蕉");
        map1.put(11, "榴莲");
        System.out.println(map1);
        
        Map<Integer,String> map2 = new TreeMap<Integer,String>();
        map2.put(99, "哈密瓜");
        map2.put(4, "菠萝");
        map2.put(2, "草莓");
        map2.put(3, "香蕉");
        map2.put(11, "榴莲");
        System.out.println(map2);
        
        //遍历
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
        }
        System.out.println("*-----------------------");
        for (Integer key : map.keySet()) {
            System.out.println("key = " + key+ ", value = " +map.get(key));
        }
        System.out.println("-----------------------");
        Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, String> entry = it.next();
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
        }
        System.out.println("*-----------------------");
        map.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });
        System.out.println("-----------------------");
        
        List<Integer> list= new ArrayList<Integer>();
        list.add(99);
        list.add(96);
        list.add(95);
        list.add(98);
        /*Collections.sort(list,new Comparator<Integer>(){  
            public int compare(Integer arg0, Integer arg1) {  
                return arg0-arg1;  
            }  
        });  */
        Collections.sort(list, (item1, item2) -> item1 - item2);
        for(Integer u : list){  
            System.out.println(u);  
        } 
        //冒泡
        /*int k[] = {3,4,5,1,0,2};
        for(int i=0;i<k.length-1;i++){
            for(int j=i+1;j<k.length;j++){
                if(k[i]>k[j]){
                    int temp = k[i];
                    k[i]=k[j];
                    k[j]=temp;
                }
            }
        }
        for(int i=0;i<k.length;i++){
            System.out.println(k[i]);
        }*/
        System.out.println("-------------------------");
        //sort
        int k2[] = {3,4,5,1,0,2};
        Arrays.sort(k2);
        for(int n=0;n<k2.length;n++){
            System.out.println(k2[n]);
        }
        System.out.println("-------------------------");
        //选择排序 xxxxx
        /*int min=0;
        int temp=0;
        int k3[] = {3,4,5,3,2,1,0,2};    
        for(int i=0;i<k3.length-1;i++){
            min = i;
            for(int j=i+1;j<k3.length;j++){
                if(k3[min]>k3[j]){
                    min=j;
                }
                if(min!=i){
                    temp = k3[i];
                    k3[i]=k3[min];
                    k3[min]=temp;
                }
            }
        }*/
        int [] arr= {56,62,47,21,27,76,11,39,77,19,23};
        int k=0;
        int temp=0;
        for(int i=0;i<arr.length-1;i++) {
             k=i;
             for(int j=i+1;j<arr.length;j++) {
                if(arr[k]>arr[j]) {
                    k=j;
                }
             }
             if(k!=i) {
                //交换位置
                temp=arr[i];
                arr[i]=arr[k];
                arr[k]=temp;
             }
          }
          System.out.println("排序后:"+Arrays.toString(arr));
         //选择排序
        System.out.println("-------------------------");
        
        int k4[] = {3,4,5,3,2,1,0,2};        
        for(int i=1;i<k4.length;i++){   
            for(int j=i;j>0;j--) {   
                if (k4[j]<k4[j-1]){   
                    temp=k4[j-1];  
                    k4[j-1]=k4[j];  
                    k4[j]=temp;          
                }else break;   
            }  
        }
        for(int i=0;i<k4.length;i++)
        {
            System.out.println(k4[i]);
        }  //插入排序
        System.out.println("-------------------------");
        //倒序排序
        Collections.reverse(list);
        System.out.println(list);
        
        //数组倒序排序
        Integer[] a = { 9, 6, 7, 2, 3, 4, 0, 1, 8, 5 };
        Comparator<Integer> cmp = new MyComparator();
        Arrays.sort(a, cmp);
        System.out.println("排序后:"+Arrays.toString(a));
        
        System.out.println("-------------------------");
        Integer[] A={10,23,42,12,20,6};
        Arrays.sort(A,Collections.reverseOrder());
        for(int c : A){
           System.out.println(c);
        }
        
        
        //map排序
        Map<String, String> map6 = new TreeMap<String, String>();
        map6.put("d", "ddddd");
        map6.put("b", "bbbbb");
        map6.put("a", "aaaaa");
        map6.put("c", "ccccc");
        
        //这里将map.entrySet()转换成list
        List<Map.Entry<String,String>> listm = new ArrayList<Map.Entry<String,String>>(map6.entrySet());
        //然后通过比较器来实现排序
        Collections.sort(listm,new Comparator<Map.Entry<String,String>>() {
            //升序排序
            @Override
            public int compare(Entry<String, String> o1,Entry<String, String> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        
        for(Map.Entry<String,String> mapping:listm){ 
               System.out.println(mapping.getKey()+":"+mapping.getValue()); 
          } 

  现在遍历集合:

List:

result.forEach(System.out::println);

map:

result.forEach((key,value)->System.out.println("key="+key+";value="+value));

 

############################❤###############################

分组:

List<myDTO> strings = new ArrayList<myDTO>();
myDTO m=new myDTO();
m=new myDTO();m.setName("果粒橙");m.setValue(1);m.setId(1L);;strings.add(m);
m=new myDTO();m.setName("果粒橙");m.setValue(2);m.setId(2L);strings.add(m);
m=new myDTO();m.setName("拼多多");m.setValue(1);m.setId(1L);strings.add(m);
m=new myDTO();m.setName("拼多多");m.setValue(1);m.setId(2L);strings.add(m);
m=new myDTO();m.setName("果粒橙");m.setValue(4);m.setId(1L);strings.add(m);
m=new myDTO();m.setName("拼多多");m.setValue(1);m.setId(2L);strings.add(m);
m=new myDTO();m.setName("果粒橙");m.setValue(5);m.setId(1L);strings.add(m);
m=new myDTO();m.setName("菠萝蜜");m.setId(1L);strings.add(m);
        
/*Map<String, List<myDTO>> result = strings.stream() 
          .collect(Collectors.groupingBy(myDTO::getNameId)); 组合key (需要在BEAN 里写get方法)*/
/*Map<String, Map<Long,List<myDTO>>> result = strings.stream() 
                  .collect(Collectors.groupingBy(myDTO::getName,Collectors.groupingBy(myDTO::getId))); 嵌套*/
/*Map<String, Long> result = strings.stream() 
                  .collect(Collectors.groupingBy(myDTO::getName,Collectors.counting())); */
/*Map<String, Long> result = strings.stream() 
                  .collect(Collectors.groupingBy(myDTO::getNameId,Collectors.counting())); */

list 去重

// 过滤去重
List<Integer> orgIds = new ArrayList<Integer>();
orgIds.add(1);
orgIds.add(11);
orgIds.add(12);
orgIds.add(1);
List<Integer> newOrgIds =orgIds .stream().distinct().collect(Collectors.toList());
System.out.println(newOrgIds);
List<myDTO> orgIds2 = new ArrayList<myDTO>();
myDTO dto= new myDTO();
dto.setId(1L);dto.setName("果粒橙");orgIds2.add(dto);
dto= new myDTO();
dto.setId(2L);dto.setName("拼多多");orgIds2.add(dto);
dto= new myDTO();
dto.setId(1L);dto.setName("果粒橙2");orgIds2.add(dto);
dto= new myDTO();
dto.setId(1L);dto.setName("果粒橙");orgIds2.add(dto);
List<myDTO> newOrgIds2 =orgIds2 .stream().distinct().collect(Collectors.toList());
System.out.println(newOrgIds2);
[1, 11, 12]
[myDTO [name=果粒橙, id=1, value=0.0], myDTO [name=拼多多, id=2, value=0.0], myDTO [name=果粒橙2, id=1, value=0.0]]

 数组转list:

List<String> ids= Arrays.asList(param.getIds().split(","));

 

 数组转set:

 

创建&初始化:通过工厂 创建小型不可变得 set list map 

Set

Set<String> result=Set.of("a","b","c");

List

List<String> result=List.of("a","b","c");

 但是HoHo他是不可变得!:

 Map

Map<String,Integer> result=Map.of("a",1,"b",2,"c",3);

Map.ofEntries
Map<String,Integer> result=Map.ofEntries(Map.entry("a",1),Map.entry("b",2),Map.entry("c",3));

 

map返回默认值:getOrDefault

System.out.println(result.getOrDefault("d",0));
System.out.println(result.get("d"));

 

 

 删除指定条件元素:

list:

result.removeIf(d ->d.getType().equals("水果"));

 map:

 

 替换指定条件元素:

map:

 

 加条件:

result.replaceAll((key,value)->{
            if(key.equals("a")) return value+10;
            else return value;
        } );
result.forEach((key,value)->System.out.println("key="+key+";value="+value));

 合并:putAll();

 merge:

 

 

 

 

 

 

 

- 待-

 

@

posted @ 2019-10-25 09:50  DarGi  阅读(291)  评论(0编辑  收藏  举报