Java8 Stream学习笔记
list
去重
定义对象(重写hashCode()和equals()方法)
@Data
@EqualsAndHashCode
public class User implements Serializable {
private int id;
private String name;
}
public static void main ( String[ ] args) {
List< User > newList= new ArrayList < > ( ) ;
List< User > list = new ArrayList < > ( ) ;
{
list. add ( new Book ( 1 , "村里有个姑娘叫小芳" ) ) ;
list. add ( new Book ( 1 , "村里有个姑娘叫小芳" ) ) ;
list. add ( new Book ( 2 , , "长得好看又善良" ) ) ;
}
long l = list. stream ( ) . distinct ( ) . count ( ) ;
System. out. println ( "去重之后的list长度 :" + l) ;
list. stream ( ) . distinct ( ) . forEach ( b - > {
newList. add ( b) ;
} ) ;
}
按条件去重
第一种方式
@Data
@EqualsAndHashCode
public class User implements Serializable {
private int id;
private String name;
private int age;
}
public static void main ( String[ ] args) {
List< User > newList= new ArrayList < > ( ) ;
List< User > list = new ArrayList < > ( ) ;
{
list. add ( new Book ( 1 , "村里有个姑娘叫小芳" , 20 ) ) ;
list. add ( new Book ( 1 , "还有个姑娘叫小薇" , 20 ) ) ;
list. add ( new Book ( 2 , "长得好看又善良" , 20 ) ) ;
}
list . stream ( ) . collect ( collectingAndThen ( toCollection ( ( ) - >
new TreeSet < > ( comparing ( n - > n. getId ( ) + n. getAge ( )
) ) ) , ArrayList: : new ) ) . forEach ( user- > {
newList. add ( user) ;
}
) ;
}
第二种方式
@Data
@EqualsAndHashCode
public class User implements Serializable {
private int id;
private String name;
private int age;
}
public static void main ( String[ ] args) {
List< User > newList= new ArrayList < > ( ) ;
List< User > list = new ArrayList < > ( ) ;
list. add ( new Book ( 1 , "村里有个姑娘叫小芳" , 20 ) ) ;
list. add ( new Book ( 1 , "还有个姑娘叫小薇" , 20 ) ) ;
list. add ( new Book ( 2 , "长得好看又善良" , 20 ) ) ;
listData. stream ( ) . filter ( distinctByKey ( n - > n. getId ( ) + n. getAge ( ) ) )
. forEach ( user- >
newList. add ( user)
) ;
}
private static < T> Predicate< T> distinctByKey ( Function< ? super T, ? > keyExtractor) {
Map< Object, Boolean> seen = new ConcurrentHashMap < > ( ) ;
return t - > seen. putIfAbsent ( keyExtractor. apply ( t) , Boolean. TRUE) == null;
}
排序
sorted() 默认使用自然序排序, 其中的元素必须实现Comparable 接口 sorted(Comparator<? super T> comparator) :我们可以使用lambada 来创建一个Comparator 实例。可以按照升序或着降序来排序元素。
public class Student implements Comparable < Student> {
private int id;
private String name;
private int age;
public Student ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
public int getId ( ) {
return id;
}
public String getName ( ) {
return name;
}
public int getAge ( ) {
return age;
}
@Override
public int compareTo ( Student ob) {
return name. compareTo ( ob. getName ( ) ) ;
}
@Override
public boolean equals ( final Object obj) {
if ( obj == null) {
return false ;
}
final Student std = ( Student) obj;
if ( this == std) {
return true ;
} else {
return ( this . name. equals ( std. name) && ( this . age == std. age) ) ;
}
}
@Override
public int hashCode ( ) {
int hashno = 7 ;
hashno = 13 * hashno + ( name == null ? 0 : name. hashCode ( ) ) ;
return hashno;
}
}
import java. util. ArrayList;
import java. util. Comparator;
import java. util. List;
import java. util. stream. Collectors;
public class SortList {
public static void main ( String[ ] args) {
List< Student> list = new ArrayList < Student> ( ) ;
list. add ( new Student ( 1 , "Mahesh" , 12 ) ) ;
list. add ( new Student ( 2 , "Suresh" , 15 ) ) ;
list. add ( new Student ( 3 , "Nilesh" , 10 ) ) ;
System. out. println ( "---Natural Sorting by Name---" ) ;
List< Student> slist = list. stream ( ) . sorted ( ) . collect ( Collectors. toList ( ) ) ;
slist. forEach ( e - >
System. out. println ( "Id:" + e. getId ( ) + ", Name: " + e. getName ( ) + ", Age:" + e. getAge ( ) )
) ;
System. out. println ( "---Natural Sorting by Name in reverse order---" ) ;
slist = list. stream ( ) . sorted ( Comparator. reverseOrder ( ) ) . collect ( Collectors. toList ( ) ) ;
slist. forEach ( e - >
System. out. println ( "Id:" + e. getId ( ) + ", Name: " + e. getName ( ) + ", Age:" + e. getAge ( ) )
) ;
System. out. println ( "---Sorting using Comparator by Age---" ) ;
slist = list. stream ( ) . sorted ( Comparator. comparing ( Student: : getAge) ) . collect ( Collectors. toList ( ) ) ;
slist. forEach ( e - >
System. out. println ( "Id:" + e. getId ( ) + ", Name: " + e. getName ( ) + ", Age:" + e. getAge ( ) )
) ;
System. out. println ( "---Sorting using Comparator by Age with reverse order---" ) ;
slist = list. stream ( ) . sorted ( Comparator. comparing ( Student: : getAge) . reversed ( ) ) . collect ( Collectors. toList ( ) ) ;
slist. forEach ( e - >
System. out. println ( "Id:" + e. getId ( ) + ", Name: " + e. getName ( ) + ", Age:" + e. getAge ( ) )
) ;
}
}