자바 stream 사용

6553 단어 자바
자바 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);
}

좋은 웹페이지 즐겨찾기