java stream使用

java stream的特性能大大提高代码的可观赏性,而且使用起来非常方便。由于项目开发中经常用到一些stream的操作,记录下来回忆之用。也供大家参考

/**
 * 移除数组中是空的元素
 */
public void removeblank(){
    List 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 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 stringList= Arrays.asList(new String[]{"","bb"});
    boolean result= stringList.stream().allMatch(p->p.equals(""));
    //结果false
    System.out.println(result);
}

/**
 * 是否匹配其中一个
 */
public void anyMatch(){
    List 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 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 stringListOrder= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"});
    //结果"aa","bb","cc","bb","","dd"
    stringListOrder.stream().parallel().forEachOrdered(System.out::println);

    List 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 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 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 stringList= Arrays.asList(new String[]{"aa","bb","cc","bb","","dd"});
    System.out.println(stringList.stream().peek(System.out::println).count());
}

/**
 * 默认升序
 */
public void sorted(){
    List stringList= Arrays.asList(new Integer[]{1,3,5,2,9,6});
    stringList.stream().sorted().forEach(System.out::println);
}

/**
 * 降序
 */
public void sortedReverse(){
    List 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 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() {
        @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 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 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 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 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);
}

你可能感兴趣的:(java)