Converting Collection to Map is a common requirement, especially when processing data requires quick searches or deduplication.
Here are a few common methods, including three ways to use Google, Hutool, and Java Stream APIs.
Google's
/** * Use #uniqueIndex(, ) method * * @author Fu Cong * @time 2025-03-13 09:29:51 */ @Test public void testGoogleMapsUniqueIndex() { User user1 = new User(); (1); ("Zhang San"); User user2 = new User(); (2); ("Li Si"); User user3 = new User(); // Repeat id (2) (2); ("Wang Wu"); List<User> userList = (); (user1); (user2); //(user3); Set<User> userSet = (); (user1); (user2); //(user3); // If the key is repeated, an exception will be reported. Map<Integer, User> userListMap = (userList, user -> ()); (("userListMapyes:{}", ())); Map<Integer, User> userSetMap = (userSet, user -> ()); (("userSetMapyes:{}", ())); }
———————————————————————— Start testing a single method ———————————————————————— userListMapyes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Li Si, remark=null)} userSetMapyes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Li Si, remark=null)} ———————————————————————— End testing a single method ————————————————————————
Note: If the key is repeated, an exception will be reported.
Hutool's
/** * Use #toMap(, , .Func1, .Func1) * * @author Fu Cong * @time 2025-03-12 09:29:35 */ @Test public void testHutoolCollUtilToMap() { User user1 = new User(); (1); ("Zhang San"); User user2 = new User(); (2); ("Li Si"); User user3 = new User(); // Repeat id (2) (2); ("Wang Wu"); List<User> userList = (); (user1); (user2); (user3); Set<User> userSet = (); (user1); (user3); (user2); // If the key is repeated, one of them will be taken. I am not sure which one is currently available. Map<Integer, User> userListMap = (userList, (), user -> (), user -> user); (("userListMapyes:{}", ())); // If the key is repeated, one of them will be taken. I am not sure which one is currently available. Map<Integer, User> userSetMap = (userSet, (), user -> (), user -> user); (("userSetMapyes:{}", ())); }
———————————————————————— Start testing a single method ———————————————————————— userListMapyes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Wang Wu, remark=null)} userSetMapyes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Wang Wu, remark=null)} ———————————————————————— End testing a single method ————————————————————————
Note: If the key is repeated, one of them will be taken. I am not sure which one is currently available.
Java Stream API
/** * Use #toMap(, , ) * * @author Fu Cong * @time 2025-03-12 09:29:35 */ @Test public void testJavaStreamAPICollectorsToMap() { User user1 = new User(); (1); ("Zhang San"); User user2 = new User(); (2); ("Li Si"); User user3 = new User(); // Repeat id (2) (2); ("Wang Wu"); List<User> userList = (); (user1); (user2); (user3); Set<User> userSet = (); (user1); (user2); (user3); // If the key is repeated, an exception will be reported. //Map<Integer, User> userListMap1 = ().collect((user -> (), user -> user)); //(("userListMap1 is: {}", ())); // (key1, key2) -> key1: If the key is repeated, take the first one. Map<Integer, User> userListMap2 = ().collect((user -> (), user -> user, (key1, key2) -> key1)); (("userListMap2yes:{}", ())); // If the key is repeated, an exception will be reported. //Map<Integer, User> userSetMap1 = ().collect((user -> (), user -> user)); //(("userSetMap1 is: {}", ())); // (key1, key2) -> key2: If the key is repeated, take the second one. Map<Integer, User> userSetMap2 = ().collect((user -> (), user -> user, (key1, key2) -> key2)); (("userSetMap2yes:{}", ())); }
———————————————————————— Start testing a single method ———————————————————————— userListMap2yes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Li Si, remark=null)} userSetMap2yes:{1=User(id=1, name=Zhang San, remark=null), 2=User(id=2, name=Wang Wu, remark=null)} ———————————————————————— End testing a single method ————————————————————————
Difference comparison
Is it supported to handle duplicate keys | What happens if the key is repeated | |
---|---|---|
Google's | no | Report an error |
Hutool's | no | Take one of them |
Java Stream API | yes | Values can be obtained according to the merge function |
Other Instructions
If it does not existStitch multiple field values into keysIn the case of this, you can also change [user -> ()] to [User::getId], but if it existsStitch multiple field values into keysIn the case of , you can use the form of [->] to splice it.
Map<String, User> userListMap2 = ().collect((user -> ("{}_{}", (), ()), user -> user, (key1, key2) -> key1));
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.