java stream的特性能大大提高代码的可观赏性,而且使用起来非常方便。由于项目开发中经常用到一些stream的操作,记录下来回忆之用。也供大家参考
/** * 移除数组中是空的元素 */ public void removeblank(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //结果"aa","bb","cc","bb","dd" stringList.stream().filter(p->!p.equals("")).collect(Collectors.toList()).forEach(System.out::println); } public void distinct(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //结果 "aa","bb","cc","","dd" stringList.stream().distinct().forEach(System.out::println); } /** * 是否全部匹配 */ public void allMatch(){ List<String> stringList= Arrays.asList(new String[]{"","bb"}); boolean result= stringList.stream().allMatch(p->p.equals("")); //结果false System.out.println(result); } /** * 是否匹配其中一个 */ public void anyMatch(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); boolean result= stringList.stream().anyMatch(p->p.equals("")); //结果true System.out.println(result); } /** * 是否一个都不匹配 */ public void noneMatch(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); boolean result= stringList.stream().noneMatch(p->p.equals("ff")); //结果true System.out.println(result); } /** * forEachOrdered和forEach * 开启并行时候强制保证有序或者无序 */ public void foreachSortandforeach(){ List<String> stringListOrder= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //结果"aa","bb","cc","bb","","dd" stringListOrder.stream().parallel().forEachOrdered(System.out::println); List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //结果随机的顺序 stringList.stream().parallel().forEach(System.out::println); } /** *去掉前n个skip(n)中的值返回一个新list */ public void skip(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //"bb","","dd" stringList.stream().skip(3).forEach(System.out::println); } /** * 返回原list的前n个值,并返回一个新list */ public void limit(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); //结果"aa","bb","cc" stringList.stream().limit(3).forEach(System.out::println); } /** * peek方法方法会使用一个Consumer消费流中的元素,但是返回的流还是包含原来的流中的元素。 */ public void peek(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"}); System.out.println(stringList.stream().peek(System.out::println).count()); } /** * 默认升序 */ public void sorted(){ List<Integer> stringList= Arrays.asList(new Integer[]{1,3,5,2,9,6}); stringList.stream().sorted().forEach(System.out::println); } /** * 降序 */ public void sortedReverse(){ List<Integer> stringList= Arrays.asList(new Integer[]{1,3,5,2,9,6}); stringList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); } /** * 自定义排序,按姓名的首字母的ascii码排序 * 三种不同的comparator写法 */ public void sortedDefine(){ List<User> userList=new ArrayList<>(); User user=new User(); user.setName("json"); user.setSex("male"); User user1=new User(); user1.setName("Tomas"); user1.setSex("female"); userList.add(user); userList.add(user1); //第一种写法 userList.stream().sorted(new Comparator<User>() { @Override public int compare(User o1, User o2) { return o1.getName().charAt(0)-o2.getName().charAt(0); } }).forEach(System.out::println); //第二种写法 userList.stream().sorted((s1,s2)->{return s1.getName().charAt(0)-s2.getName().charAt(0);}).forEach(System.out::println); //第三种写法 userList.stream().sorted(Comparator.comparing(User::getName)).forEach(System.out::println); } /** * list求和 */ public void reduce(){ List<Integer> integerList= Arrays.asList(new Integer[]{1,3,5,2,9,6}); //结果26,求和 System.out.println(integerList.stream().reduce((s1,s2)->s1+s2).get()); } /** * 增加所有姓名的一个前缀 * 其中一种User类加lombok的@Builder标签 */ public void addPrex(){ List<User> userList=new ArrayList<>(); User user=new User(); user.setName("json"); user.setSex("male"); User user1=new User(); user1.setName("Tomas"); user1.setSex("female"); userList.add(user); userList.add(user1); String prex="前缀"; //结果 前缀;json前缀;Tomas // userList.stream().map(p->prex+";"+p.getName()).collect(Collectors.toList()).forEach(System.out::println); userList.stream().map(p->new User(prex+";"+p.getName(),p.getSex())).forEach(System.out::println); //User类加lombok的@Builder标签 userList.stream().map(p->User.builder() .name(prex+";"+p.getName()) .sex(p.getSex()).build()).forEach(System.out::println); } /** * 字符串合并中间+"#" */ public void listToString(){ List<String> stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","dd"}); //结果 aa#bb#cc#bb#dd System.out.println(stringList.stream().collect(Collectors.joining("#"))); } /** * 分组排序 */ public void UserlistToString(){ List<User> userList=new ArrayList<>(); User user=new User(); user.setName("json"); user.setSex("male"); User user1=new User(); user1.setName("Tomas"); user1.setSex("female"); User user2=new User(); user2.setName("kifta"); user2.setSex("female"); userList.add(user); userList.add(user1); userList.add(user2); userList.stream().collect(Collectors.groupingBy(p->p.getSex())).entrySet().stream().forEach(System.out::println); }
版权声明:本文为xiaofeixia__9999原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。