定义学生类集合、字符串数组、正数数组
List < Student > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
list. add ( new Student ( "" + i, new Random ( ) . nextInt ( 22 ) , new Random ( ) . nextInt ( 100 ) ) ) ;
}
String [ ] words = { "aA" , "Bb" , "cC" , "Dd" } ;
int [ ] nums = { 4 , 3 , 2 , 7 , 4 , 2 , 7 , 2 } ;
生成Stream流
Stream < Student > studentStream = list. stream ( ) ;
Stream < String > stringStream = Arrays . stream ( words) ;
IntStream intStream = Arrays . stream ( nums) ;
Stream < Integer > integerStream = Stream . of ( 23 , 45 , 23 , 65 , 75 , 23 ) ;
终端操作
integerStream. forEach ( System . out:: println ) ;
Set < String > set = stringStream. collect ( Collectors . toSet ( ) ) ;
System . out. println ( set) ;
String s = Arrays . stream ( words)
. collect ( Collectors . joining ( "--" ) ) ;
System . out. println ( s) ;
List < String > strList = Arrays . stream ( words)
. collect ( Collectors . toList ( ) ) ;
System . out. println ( strList) ;
boolean result = list. stream ( )
. allMatch ( stu -> stu. getScore ( ) > 60 ) ;
System . out. println ( result) ;
result = Arrays . stream ( words)
. anyMatch ( str -> str. length ( ) > 5 ) ;
System . out. println ( result) ;
long count = Arrays . stream ( words) . count ( ) ;
System . out. println ( count) ;
OptionalInt max = intStream. max ( ) ;
System . out. println ( max) ;
int xx = max. getAsInt ( ) ;
System . out. println ( xx) ;
Optional < Student > min = list. stream ( )
. min ( Comparator . comparing ( Student :: getAge ) ) ;
System . out. println ( min. get ( ) ) ;
Optional < Student > maxScore = list. stream ( )
. max ( ( stu1, stu2) -> stu1. score - stu2. score) ;
System . out. println ( maxScore. get ( ) ) ;
maxScore = list. stream ( )
. max ( Comparator . comparing ( Student :: getScore ) . reversed ( ) ) ;
System . out. println ( maxScore. get ( ) ) ;
System . out. println ( "--------" ) ;
Object [ ] objects = list. stream ( ) . toArray ( ) ;
中间操作
list. stream ( )
. filter ( stu -> stu. score > 60 )
. forEach ( System . out:: println ) ;
list. stream ( )
. filter ( stu -> stu. getScore ( ) < 60 )
. map ( Student :: getName )
. forEach ( System . out:: println ) ;
list. stream ( )
. map ( Student :: getAge )
. distinct ( )
. forEach ( System . out:: println ) ;
Arrays . stream ( words)
. sorted ( )
. forEach ( str -> System . out. println ( str + "\t" ) ) ;
System . out. println ( ) ;
list. stream ( )
. sorted ( Comparator . comparing ( Student :: getAge ) )
. forEach ( System . out:: println ) ;
System . out. println ( "--------------------------" ) ;
list. stream ( )
. sorted ( Comparator . comparing ( Student :: getScore ) . reversed ( ) )
. forEach ( System . out:: println ) ;
System . out. println ( "------------------" ) ;
list. stream ( )
. sorted ( Comparator . comparing ( Student :: getName ) )
. limit ( 4 )
. forEach ( System . out:: println ) ;
String [ ] strArr = { "good" , "nice" , "OK" } ;
Stream < String [ ] > arrayStream = Stream . of ( words, strArr) ;
Stream < String > strStream = arrayStream. flatMap ( Arrays :: stream ) ;
strStream. forEach ( str -> System . out. println ( str + "\t" ) ) ;
System . out. println ( ) ;
静态内部类——Student类
static class Student {
private String name;
private Integer age;
private Integer score;
public Student ( String name, Integer age, Integer score) {
this . name = name;
this . age = age;
this . score = score;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}' ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
public Integer getScore ( ) {
return score;
}
public void setScore ( Integer score) {
this . score = score;
}
}