Operation of Java8 to convert an Listless thanTgreater than to Mapless thanString Tgreater than
- 2021-08-28 20:23:18
- OfStack
Convert List to Map < String, T >
public class AnswerApp {
public static void main(String[] args) throws Exception {
List<String> names = Lists.newArrayList("Answer", "AnswerAIL", "AI");
Map<String, Integer> map = names.stream().collect(Collectors.toMap(v -> v, v -> 1));
System.out.println(map);
}
}
Program running output
{Answer=1, AnswerAIL=1, AI=1}
Convert List to Map < K, V >
public static void main(String[] args) throws Exception {
List<User> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
users.add(new User("answer" + i, new Random().nextInt(100)));
}
System.out.println(JSON.toJSONString(users));
System.out.println();
Map<String, Integer> map = users.stream().collect(Collectors.toMap(User::getName, User::getAge));
System.out.println(map);
}
Program running output
[{"age":78,"name":"answer0"},{"age":89,"name":"answer1"},{"age":72,"name":"answer2"}]
{answer2=72, answer1=89, answer0=78}
Convert List to Map < String, T >
Implementation Mode 1
public class AnswerApp {
public static void main(String[] args) throws Exception {
List<User> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
// Change to this code , Turn map Errors will be reported when Duplicate key User
// users.add(new User("answer", new Random().nextInt(100)));
users.add(new User("answer" + i, new Random().nextInt(100)));
}
System.out.println(JSON.toJSONString(users));
System.out.println();
Map<String, User> map = users.stream().collect(Collectors.toMap(User::getName, Function.identity()));
System.out.println(JSON.toJSONString(map));
}
}
In this way, if key of map (such as the value of User:: getName in the above example) is duplicated, java. lang. IllegalStateException: ES40key User will be thrown incorrectly
Program running output
[{"age":22,"name":"answer0"},{"age":79,"name":"answer1"},{"age":81,"name":"answer2"}]
{"answer2":{"age":81,"name":"answer2"},"answer1":{"age":79,"name":"answer1"},"answer0":{"age":22,"name":"answer0"}}
Implementation 2
public class AnswerApp {
public static void main(String[] args) throws Exception {
List<User> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
users.add(new User("answer", new Random().nextInt(100)));
}
System.out.println(JSON.toJSONString(users));
System.out.println();
// If key Repetition , Then according to Conflict method · (key1, key2) -> key2 · Judge . Explanation : key1 key2 When in conflict Take key2
Map<String, User> map = users.stream().collect(Collectors.toMap(User::getName, Function.identity(), (key1, key2) -> key2));
System.out.println(JSON.toJSONString(map));
}
}
Program running output
[{"age":24,"name":"answer"},{"age":89,"name":"answer"},{"age":68,"name":"answer"}]
{"answer":{"age":68,"name":"answer"}}
If changed to (key1, key2)- > key1 outputs {"answer": {"age": 24, "name": "answer"}}
User entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
public User(String name) {
this.name = name;
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
Supplement: Lambda expression is used in java8 to transfer two fields of entity class in list to Map
Code:
List<Entity> list = new ArrayList<>();
Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType));
Common lambda expressions:
{Answer=1, AnswerAIL=1, AI=1}
0