In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "java8 how to 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 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 (); / / Store 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 ("9.99"), 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 by ID MapMap 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 * it should be noted that: * toMap if the collection object has a duplicate key, it will report an error Duplicate key. * the id of apple1,apple12 is 1. * it can be set with (K11=Apple K2)-> K1. If there are duplicates of key, keep key1 and discard key2 * / Map appleMap = appleList.stream (). Collect (Collectors.toMap (Apple::getId, a-> a, (K1PowerK2)-> K1); print appleMap {1=Apple {id=1, name=' apple 1x, 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")) .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)
6. Deweighting
Import static java.util.Comparator.comparingLong;import static java.util.stream.Collectors.collectingAndThen;import static java.util.stream.Collectors.toCollection; / / remove weight according to id List unique = appleList.stream () .collect (collectingAndThen (toCollection (()-> new TreeSet (comparingLong (Apple::getId), ArrayList::new)
Thank you for reading, the above is the content of "how to achieve List to map, grouping, filtering and other operations in java8". After the study of this article, I believe you have a deeper understanding of how to achieve List to map, grouping, filtering and other operations in java8, and the specific use 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.