자바 stream 사용
6553 단어 자바
/**
*
*/
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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.