In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Java8 how to quickly achieve List to map, grouping, filtering and other operations", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java8 how to quickly achieve List to map, grouping, filtering and other operations" bar!
With the new features of java8, you can use concise and efficient code to achieve some data processing.
Define 1 Apple object:
Public class Apple {
Private Integer id
Private String name
Private BigDecimal money
Private Integer num
Public Apple (Integer id, String name, BigDecimal money, Integer num) {
This.id = id
This.name = name
This.money = money
This.num = num
}
}
Add some test data:
List appleList = new ArrayList (); / / stores the collection of apple objects
Apple apple1 = new Apple (1, "Apple 1", new BigDecimal ("3.25"), 10)
Apple apple12 = new Apple (1, "Apple 2", new BigDecimal ("1.35"), 20)
Apple apple2 = new Apple (2, "banana", new BigDecimal ("2.89"), 30)
Apple apple3 = new Apple (3, "litchi", new BigDecimal), 40)
AppleList.add (apple1)
AppleList.add (apple12)
AppleList.add (apple2)
AppleList.add (apple3)
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))
System.err.println ("groupBy:" + groupBy)
{1 = [Apple {id= 1, name= 'Apple 1, money= 3.25, num= 10}, Apple {id= 1, name=' Apple 2, 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}]}
2. List to Map
Id is key,apple object is value, you can do this:
/ * *
* 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 appleMap
{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. Search the official account of Java bosom friend, reply to "back-end interview" and send you a treasure book of Java interview questions.
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)
6. Removing heavy 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)
);
The following table shows the static factory methods of the Collectors class.
Thank you for your reading, the above is "how to quickly achieve List to List to map, grouping, filtering and other operations" of the content, after the study of this article, I believe that Java8 how to quickly achieve List to map, grouping, filtering and other operations have a deeper understanding of this problem, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.