Turn Java8 in turn map summarizes the method list

Background
in the development of recent work, a lot of getting used to the Java8 Stream usage, it is convenient and can be executed in parallel to the stream, here to write about a list of turn map the scene yesterday encountered.
list in turn map the stream of applications Java8
usual way
1. The method of using the conversion Collectors.toMap

the Map public <Long, String> getIdNameMap (List <the Account> Accounts) {
return accounts.stream () the collect ((:: getId the Account, the Account :: the getUsername) Collectors.toMap);.
}

The first parameter is the can, the second parameter is the value of value.

2. Collection object entity itself
- we need sometimes to their own list of entities according to a field where the packet (such as id -> List) in the development process, this time to set the value of the value map is an entity in itself.

the Map public <Long, the Account> getIdAccountMap (List <the Account> Accounts) {
return accounts.stream () the collect (Collectors.toMap (the Account :: getId, Account -> Account));.
}

Account -> Account is itself a return lambda expressions, in fact, can also use a default method Function.identity Function interface (), which returns the object itself, more concise

The key duplication.
In the list into map, as the value of the key is likely to be repeated, this time the process will flow throw exceptions: Java.lang.IllegalStateException: Duplicate key. It is necessary to specify the time when the key when the key conflict in toMap selection process. (This is the first choice and the second key covers a key)
public the Map <String, the Account> getNameAccountMap (List <the Account> Accounts) {
return accounts.stream (). The collect (Collectors.toMap (the Account :: the getUsername, Function. Identity (), (key1, key2) -> key2));
}

grouped with groupingBy or partitioningBy
The attribute field or a packet can be directly used groupingBy method, it is convenient.
Map <Integer, List <>> personGroups the Person = Stream.generate (new new PersonSupplier ()).
Limit (100).
The collect (Collectors.groupingBy (the Person :: getAge));
. IT = personGroups.entrySet the Iterator () Iterator ( );
the while (it.hasNext ()) {
of Map.Entry <Integer, List <>> the Person = persons (of Map.Entry) it.next ();
System.out.println (. "Age" + persons.getKey () + "=" + persons.getValue () size ());
}

partitioningBy can be understood as a special groupingBy, key values true and false, of course, at this time the method of determining a parameter statement (for determining a function interface)
the Map <Boolean, List <>> Children = Stream.generate the Person (PersonSupplier new new ()).
limit (100).
the collect (Collectors.partitioningBy (P -> p.getAge () <18 is));
System.out.println ( "Children Number:" + children.get (to true) .size ());
System.out.println ( "Adult Number:" + Children. get (false) .size ()) ;

on good text stream using recommended:
here to see the ibm an article on stream in, get to a lot of stream have not encountered before usage. Old iron can go to learn about. [https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/]

Guess you like

Origin www.cnblogs.com/xyfaneast/p/12093502.html