java8方法大全
根据一个属性去重
List<Book> unique = books.stream().collect( Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.id))), ArrayList::new));
根据两个属性去重
List<CarseriesConfiguration> carseriesConfigurationList = configurations.stream() .collect(Collectors.collectingAndThen(Collectors.toCollection( ()->new TreeSet<>(Comparator.comparing( l -> l.getCarMaterialCode()+l.getCountryArea()))), ArrayList::new));
根据属性值过滤
List<LanguageLibrary> libraryList = languageLibraries.stream().filter(l -> StringUtils.isNotEmpty(l.getValue())) .collect(Collectors.toList());
List<ServiceStationUser> list = serviceStationUserList.stream().filter(s -> !s.getDefaultLanguage().equals("zh")).collect(Collectors.toList());
取出某一个属性集合
List<String> carMaterialCodes = configurations.stream().map(CarseriesConfiguration::getCarMaterialCode).collect(Collectors.toList());
去除重复对象
doubleKeyList = doubleKeyList.stream().distinct().collect(Collectors.toList());
根据属性值分组
Map<String, List<LanguageLibrary>> groupList = libraries.stream().collect(Collectors.groupingBy(LanguageLibrary::getKey));
获得某一属性的集合
List<String> serviceStationCode = userList.stream().map(l -> l.getServiceCode() ).collect(Collectors.toList());
List<List<String>> dictionaryPath = configs.stream() .map(c -> Arrays.asList(new String[]{c.getRegion(), c.getEngineConfig(), c.getModel()})) .collect(Collectors.toList());
获得某一属性的集合并用特殊字符分隔
List<String> list = Stream.of(entity.getRole().split("/")).collect(toList());
将/A/B/C/格式变为数组{A,B,C}
List<String> pathList = Arrays.stream(assemblyToDB.getPath().split("/")) .filter(a -> StringUtils.isNotEmpty(a)) .collect(Collectors.toList());
转换为map结构
Map<Integer, String> fieldMap = Arrays.stream(fields).collect(Collectors.toMap(ExcelFieldMap::getExcelColumn, ExcelFieldMap::getFieldName));
转换为set
Set<String> carYears = list.stream().map(CarseriesConfiguration::getCarYear).collect(Collectors.toSet());