Created
January 24, 2018 03:38
-
-
Save RICH0423/95b82f724050854a2975cf64c6c1c87e to your computer and use it in GitHub Desktop.
Converting Collection to Map with JDK 8 Collectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static java.lang.System.out; | |
import java.util.*; | |
import java.util.function.BinaryOperator; | |
import java.util.stream.Collectors; | |
public class Main { | |
private static final User[] users; | |
static { | |
users = new User[]{ | |
new User("u-1", "rich", 30), | |
new User("u-2", "tom", 20), | |
new User("u-1", "rich lee", 30), | |
new User("u-3", "Jackson", 45) | |
}; | |
} | |
private static Map<String, User> convertArrayToMap(final User[] users) { | |
return Arrays.stream(users).collect( | |
Collectors.toMap(User::getId, user -> user)); | |
} | |
private static Map<String, User> convertListToMap(final List<User> users) { | |
return users.stream().collect( | |
Collectors.toMap(User::getId, user -> user)); | |
} | |
/** | |
* Use initial value when the mapped keys contains duplicates. | |
* | |
* @param users | |
* @return | |
*/ | |
private static Map<String, User> convertListToMapAndMerge(final List<User> users) { | |
return users.stream().collect( | |
Collectors.toMap(User::getId, user -> user, usingInitialValue())); | |
} | |
private static Map<String, User> convertSetToMap(final Set<User> userSet) { | |
return userSet.stream().collect( | |
Collectors.toMap(User::getId, user -> user)); | |
} | |
private static <T> BinaryOperator<T> usingInitialValue() { | |
return (a,b) -> a; | |
} | |
private static <T> BinaryOperator<T> usingLastValue() { | |
return (a,b) -> b; | |
} | |
public static void main(String[] args) { | |
final List<User> usersList = Arrays.asList(users); | |
/* | |
If the mapped keys contains duplicates (according to Object.equals(Object)), an IllegalStateException is | |
thrown when the collection operation is performed. | |
If the mapped keys may have duplicates, use toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction) instead. | |
*/ | |
// out.println("LIST -> MAP:\n" + convertListToMap(usersList)); | |
out.println("LIST -> MAP:\n" + convertListToMapAndMerge(usersList)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
public class User { | |
final String id; | |
final String name; | |
final int age; | |
public User(String id, String name, int age) { | |
this.id = id; | |
this.name = name; | |
this.age = age; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getAge() { | |
return age; | |
} | |
@Override | |
public String toString() { | |
return "User{" + | |
"id='" + id + '\'' + | |
", name='" + name + '\'' + | |
", age=" + age + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment