package com.ziniao.manage.service; import com.ziniao.manage.pojo.Role; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @Service public class LambdaService { public void lambdaTest(){ //lambda對list的使用 List<Role> roles = new ArrayList<>(); roles.add(new Role(1,"test")); roles.add(new Role(2,"test")); roles.add(new Role(3,"meiri")); roles.add(new Role(3,"meiri")); List<Role> roleList = new ArrayList<>(); roleList.add(new Role(4,"rolea")); roleList.add(new Role(3,"roleb")); List<Role> roleList1 = new ArrayList<>(); roleList1.add(new Role(1.1,2.2f)); roleList1.add(new Role(1.2,2.3f)); /** * for循環的使用 */ roles.forEach(role -> { roleList.forEach(role1 ->{ if(role.getId().equals(role1.getId())){ //System.out.println(role.getId()); } } ); }); /** * distinct中去重的使用 */ //這個不起作用why??? List<Role> listRole = roles.stream().distinct().collect(Collectors.toList()); System.out.println(); /** * 對象中某個值的去重 * 這里有個問題就是為null的時候會會報錯 */ List<Role> roleListByKey = roles.stream().filter(LambdaService.distinctByKey(Role::getRoleName)).collect(Collectors.toList()); System.out.println(); /** *直接跳過skip()直接查看skip的后面list//這個在實際應用上應該很少 */ List<Role> skipRoleList = roles.stream().skip(2).collect(Collectors.toList()); System.out.println(); /** * 排序sort(正序r1,r2順序一樣,倒敘相反) * 比較最好用compare來用這樣基本類型都可以用,-號的話只能是Long,Integer,int 整數 */ List<Role> sortRoleList = roleList.stream().sorted((r1,r2)->r2.getId().compareTo(r1.getId())).collect(Collectors.toList()); //這里還有個簡寫的方式 roleList.sort((r1,r2)->r2.getId().compareTo(r1.getId())); List<Role> sortRoleList1 = roleList.stream().sorted((r1,r2)->r1.getRoleName().compareTo(r2.getRoleName())).collect(Collectors.toList()); List<Role> sortRoleList2 = roleList1.stream().sorted((r1,r2)->r2.getTest2().compareTo(r1.getTest2())).collect(Collectors.toList()); System.out.println(); /** * 過濾filter */ //這個是過濾某個類型中的值這個會很好用點 List<Role> filterRoleList = roleList.stream().filter(role ->role.getId()>3 && role.getRoleName().equals("test")).collect(Collectors.toList()); //這個null也可以 List<Role> filterRoleList2 = roleList1.stream().filter(role ->role.getTest1()==null).collect(Collectors.toList()); System.out.println(); /** * 直接獲取list中的某個屬性的值map(->/::get)都可(這個我用的還比較多) */ List<Integer> mapIdList = roleList.stream().map(item -> item.getId()).collect(Collectors.toList()); List<String> mapIdList1 = roleList.stream().map(Role::getRoleName).collect(Collectors.toList()); /** * 具體某個屬性的.sum(),average(),max()先列這么多//這里點就是.average 等獲取到的是Optional+基本類型這種數據類型 */ long count = roleList.stream().count(); Integer sumInt = roleList.stream().mapToInt(Role::getId).sum(); Double sumDouble = roleList1.stream().mapToDouble(Role::getTest1).sum(); Double avageDouble = roleList1.stream().mapToDouble(Role::getTest1).average().getAsDouble(); Double minDouble = roleList1.stream().mapToDouble(Role::getTest1).min().getAsDouble(); Double maxDouble = roleList1.stream().mapToDouble(Role::getTest1).max().getAsDouble(); //用規約的方式 reduce Optional<Integer> reduceSum = roleList.stream().map(role -> role.getId()).reduce(Integer::sum);//沒有初始值返回的值為Optional<> Integer reduceSums = roleList.stream().map(role -> role.getId()).reduce(0,Integer::sum);//這里有個初始值 Integer a = reduceSum.get(); System.out.println(); /** * 對某個屬性值直接拼接字符串輸出(這里int還不能直接用) */ String str = roleList.stream().map(Role::getRoleName).collect(Collectors.joining(", ")); /** * 分組Collectors.groupingBy(屬性名) */ Map<Integer,List<Role>> groupRoleList = roles.stream().collect(Collectors.groupingBy(Role::getId)); /** * 多重分組(Collectors.groupingBy(屬性名),Collectors.groupingBy(屬性名)) */ Map<String,Map<Integer,List<Role>>> mutilGroupRoleList =roles.stream().collect(Collectors.groupingBy(Role::getRoleName,Collectors.groupingBy(Role::getId))); System.out.println(); List<Long> longs = new ArrayList<>(); longs.add(1L); longs.add(2L); longs.add(1L); List<Long> longList = longs.stream().distinct().collect(Collectors.toList()); System.out.println(); } public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) { Map<Object, Boolean> seen = new ConcurrentHashMap<>(); return object ->seen.putIfAbsent(keyExtractor.apply(object), true) == null; } }