entrySet遍历
Map< Employee. Status, List< Employee> > group = emps. stream ( )
. collect ( Collectors. groupingBy ( Employee: : getStatus) ) ;
for ( Map. Entry< Employee. Status, List< Employee> > entry : group. entrySet ( ) ) {
System. out. println ( "key = " + entry. getKey ( ) + ", value = " + entry. getValue ( ) ) ;
}
keySet或values遍历
Map< Employee. Status, List< Employee> > group = emps. stream ( )
. collect ( Collectors. groupingBy ( Employee: : getStatus) ) ;
for ( List< Employee> value : group. values ( ) ) {
System. out. println ( "value = " + value) ;
}
System. out. println ( "----------------------------" ) ;
for ( Employee. Status key : group. keySet ( ) ) {
System. out. println ( "key = " + key) ;
}
Iterator遍历
Map< Employee. Status, List< Employee> > group = emps. stream ( )
. collect ( Collectors. groupingBy ( Employee: : getStatus) ) ;
Iterator< Map. Entry< Employee. Status, List< Employee> >> it = group. entrySet ( ) . iterator ( ) ;
while ( it. hasNext ( ) ) {
Map. Entry< Employee. Status, List< Employee> > entry = it. next ( ) ;
System. out. println ( "key = " + entry. getKey ( ) + ", value = " + entry. getValue ( ) ) ;
}
forEach遍历(java8)
Map< Employee. Status, List< Employee> > group = emps. stream ( )
. collect ( Collectors. groupingBy ( Employee: : getStatus) ) ;
group. forEach ( ( key, value) - > {
System. out. println ( key + ":" + value) ;
} ) ;