常用集合操作(待完善)

将包含id的字符串转换成set集合

   String ids = "1,2,5,7,8";  
    public Set<Long> transformLongSet(String ids){
    
    
        System.out.println("string转换");
        List<String> a = Arrays.asList(ids.split(","));
        Set<Long> idList = a.stream().map(item ->
                Long.parseLong(item)
        ).collect(Collectors.toSet());
        return idList;
    }

map/list添加数据的方式

        Map<String, String> map = new HashMap<String, String>() {
    
    {
    
    
            put("realInformerId", StrUtil.removeSuffix(realInformerId.toString(), ","));
            put("realInformerName", StrUtil.removeSuffix(realInformerName.toString(), ","));
            put("realInformerCaseId", StrUtil.removeSuffix(realInformerCaseId.toString(), ","));
            put("fakeInformerCaseId", StrUtil.removeSuffix(fakeInformerCaseId.toString(), ","));
            put("fakeName", StrUtil.removeSuffix(fakeInformerName.toString(), ","));
        }};
        new ArrayList<LocalDateTime>() {
    
    {
    
    
                add(LocalDateTime.ofEpochSecond(time / 1000, 0, ZoneOffset.ofHours(8)));
                add(LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
        }};

将list转换成map

		//属性为值
        Map<Long, String> collectProblems = problemCategoryService.list()
                .stream()
                .collect(Collectors.toMap(TProblemCategory::getId, TProblemCategory::getCateName));

        //对象为值
        List<User> users= userMapper.selectList(null);
        users.stream().collect(Collectors.toMap(User::getId,user->user));

元素排序

 //基本数据类型
  List<Integer> a = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
    
    
            a.add(random.nextInt(100));
        }
        //升序
        a.sort((i, m) -> i.compareTo(m));
        //或者
        a.sort(Comparator.comparing(b -> b));
        //或者
        a.sort((i, m) -> i - m);
        //或者
        a.sort(new Comparator<Integer>() {
    
    
            @Override
            public int compare(Integer o1, Integer o2) {
    
    
                return o1 - o2;
            }
        });

        //降序
        a.sort(new Comparator<Integer>() {
    
    
            @Override
            public int compare(Integer o1, Integer o2) {
    
    
                return o2 - o1;
            }
        });


引用数据类型

        Student studentOne = new Student();
        studentOne.setAge(18);
        studentOne.setId(200);
       Student studentTwo = new Student();
        studentTwo.setAge(20);
        studentTwo.setId(100);
        List<Student> a = new ArrayList<Student>(){
    
    {
    
    
            add(studentOne);
            add(studentTwo);
        }};
        //正序
        a.sort(Comparator.comparing(Student::getAge));
        //倒序
        a.sort(Comparator.comparing(i->((Student)i).getAge()).reversed());
        //先按照年龄排,在按照id排
        a.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getId));
        System.out.println(a);
先遍历,再排序
            List<DataAnalysisDeptCountsBO> collect = map.keySet().stream().map(id -> {
    
    
                DataAnalysisDeptCountsBO data = new DataAnalysisDeptCountsBO();
                data.setId(id);
                data.setDept(deptMapper.selectById(id).getName());
                data.setCaseNum(map.get(id));
                return data;
            }).sorted(Comparator.comparing(DataAnalysisDeptCountsBO::getCaseNum).reversed()).limit(10).collect(Collectors.toList());

集合取交并差

      List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        List<Integer> b = new ArrayList<>();
        b.add(2);
        b.add(3);
        b.add(4);
        b.forEach(item -> {
    
    
            if (a.contains(item))
                System.out.println(item);
        });
        System.out.println(a.contains(b.get(0)));
//        //交
        List<Integer> intersectionOne = b.stream().filter(item -> a.contains(item)).collect(Collectors.toList());
        List<Integer> intersectionTwo = a.stream().filter(b::contains).collect(Collectors.toList());
        intersectionOne.forEach(System.out::print);
        //并
        a.addAll(b);
        a.parallelStream().forEach(System.out::print);
        //差 b-a
        List<Integer> difference = a.stream().filter(item -> !b.contains(item)).collect(Collectors.toList());
        difference.parallelStream().forEach(System.out::print);

二位数组常用操作
二维数据常用操作

猜你喜欢

转载自blog.csdn.net/zhang19903848257/article/details/115741017