java8关于常用的list的操作

tech2024-03-16  58

java8常用list操作

List<Object> lists = Lists.newArrayList(); 1.去重 //根据某一元素去重 List<Object> list = lists.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>( Comparator.comparing( Object::getId))), ArrayList::new)); // 根据name,sex两个属性去重 List<Person> unique = persons.stream().collect(Collectors. collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new) ); //list去重对象 List<Object> listAll = lists.stream().distinct().collect(Collectors.toList()); 2.过滤 List<Object> listFilter = lists.stream().filter(item -> StringUtils.isEmpty(item.getPatentName()) || "0".equals(item..getLevel())) .collect(Collectors.toList()); 3.提取出list对象中的一个属性并去重 List<String> receiptNoList= lists.stream().map(Object::getPatentName) .distinct()//去重 .collect(Collectors.toList()); 4.List转Map Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));

 

最新回复(0)