In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the method of converting List to map in java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the method of converting List to map in java".
Method 1:
@ Override
Public String toString () {
Return "User {" +
"id=" + id +
", age='" + age +'\'+
'}'
}
Map maps = new HashMap ()
For (User user: userList) {maps.put (user.getId (), user);}
Method 2: use guava
Map maps = Maps.uniqueIndex (userList, new Function () {
@ Override
Public Long apply (User user) {
Return user.getId ()
}
});
Can be further simplified
ImmutableMap map = Maps.uniqueIndex (users,WebUser::getNickname)
Method 3: use jdk1.8
Map maps = userList.stream () .collect (Collectors.toMap (User::getId,Function.identity ()
Map maps = userList.stream () .collect (Collectors.toMap (User::getId,Function.identity ()
It seems that it is more convenient to use JDK 1.8. In addition, when converting to map, the same situation may occur in key. If you do not specify an override rule, the above code will report an error. When converting to map, it is best to use the following methods:
Map maps = userList.stream () .collect (Collectors.toMap (User::getId, Function.identity (), (key1, key2)-> key2))
Map maps = userList.stream () .collect (Collectors.toMap (User::getId,Function.identity (), (key1,key)-> key2))
Sometimes, if the value of the map you want is not an object, but a property of the object, you can use the following ways:
Map maps = userList.stream () .collect (Collectors.toMap (User::getId, User::getAge, (key1, key2)-> key2))
Mapmaps = userList.stream () .collect (Collectors.toMap (User:getId,User::getAge, (key1,key2)-> key2))
1. Grouping
Object elements in List, grouped by certain attributes, for example, grouped by id, put the same id together:
/ / List is grouped into Map by ID
Map groupBy = appleList.stream () .collect (Collectors.groupingBy (Apple::getId))
Map groupBy = appleList.stream () .collect (Collectors.groupingBy (Apple::getId))
System.err.println ("groupBy:" + groupBy)
{1 = [Apple {id=1, name=' apple 1x, money=3.25, num=10}, Apple {id=1, name=' apple 2mm, money=1.35, num=20}], 2 = [Apple {id=2, name=' banana', money=2.89, num=30}], 3 = [Apple {id=3, name=' litchi', money=9.99, num=40}]}
/ * *
* List-> Map
* note that:
* toMap if the collection object has a duplicate key, it will report an error Duplicate key.
* the id of apple1,apple12 is 1.
* you can set it with (K1)-> K1. If there is a duplicate key, keep the key1 and discard the key2
, /
Map appleMap = appleList.stream () .collect (Collectors.toMap (Apple::getId, a-> a, (K1Magol K2)-> K1))
Print result: {1=Apple {id=1, name=' Apple 1, money=3.25, num=10}, 2=Apple {id=2, name=' Banana', money=2.89, num=30}, 3=Apple {id=3, name=' Litchi, money=9.99, num=40}}
3. Filter Filter
Filter out eligible elements from the collection:
/ / filter out eligible data
List filterList = appleList.stream () .filter (a-> a.getName () .equals ("banana")) .equals (Collectors.toList ())
System.err.println ("filterList:" + filterList)
[Apple {id=2, name=' Banana', money=2.89, num=30}]
4. Summation
Sum the data in the collection according to a property:
/ / calculate the total amount
BigDecimal totalMoney = appleList.stream () .map (Apple::getMoney) .reduce (BigDecimal.ZERO, BigDecimal::add)
System.err.println ("totalMoney:" + totalMoney); / / totalMoney:17.48
5. Find the maximum and minimum value in the stream
Collectors.maxBy and Collectors.minBy to calculate the maximum or minimum values in the flow.
Optional maxDish = Dish.menu.stream ().
Collect (Collectors.maxBy (Comparator.comparing (Dish::getCalories)
MaxDish.ifPresent (System.out::println)
Optional minDish = Dish.menu.stream ().
Collect (Collectors.minBy (Comparator.comparing (Dish::getCalories)
MinDish.ifPresent (System.out::println)
Deweighting
Import static java.util.Comparator.comparingLong
Import static java.util.stream.Collectors.collectingAndThen
Import static java.util.stream.Collectors.toCollection
/ / weight removal according to id
List unique = appleList.stream () .collect (
CollectingAndThen (
ToCollection (()-> new TreeSet (comparingLong (Apple::getId), ArrayList::new)
);
At this point, I believe you have a deeper understanding of "the method of converting List to map in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.