In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to check whether a property of the List object is duplicated by java8". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to check whether a property of the List object is duplicated by java8".
Catalogue
Use Stream to check whether there are duplicates in a property of the List object
Practice some usage of stream
Five ways of removing weight from list
Method 1: use stream, a new feature of java8, to remove weight from List
Method 2: double for cycle weight removal
Method 3: set set to judge to remove duplicates without disturbing the order
Method 4: after traversal, determine whether it is assigned to another list set
Method 5: set and list conversion to remove weight
Use Stream to check whether there are duplicates in a property of the List object
In Java8 development, it is often necessary to determine whether an attribute has a duplicate value for a collection of List objects. The result can be easily obtained by using Stream stream processing.
Practice some usage of stream
Test the sample Java code
@ Test public void T2 () {List list = new ArrayList (); User user1 = new User ("zhangsan", "beijing", 30); User user2 = new User ("zhangsan", "beijing", 40); User user3 = new User ("lisi", "shanghai", 35); User user4 = new User ("lisi", "shanghai", 28); User user5 = new User ("lisim", "shanghai", 32) List.add (user1); list.add (user2); list.add (user3); list.add (user4); list.add (user5); System.out.println ("Raw data:" + list) / / to determine whether the name is duplicated, practice using java8's stream method / / method 1. Distinct, compare the size directly, and only know whether there is a repetition List collect1 = list.stream (). Map (User::getName). Distinct (). Collect (Collectors.toList ()); System.out.println (collect1.size ()! = list.size ()? " Method 1-duplicate names ":" No duplicates "); / / method 2. User name count Map collect2 = list.stream (). Collect (Collectors.groupingBy (User::getName, Collectors.counting ()); System.out.println ("name repeat count:" + collect2); / / screen out duplicate names List collect3 = collect2.keySet (). Stream (). Filter (key-> collect2.get (key) > 1) .names (Collectors.toList ()); / / you can know which names have duplicate System.out.println ("method 2-duplicate names:" + collect3); / / method 3, keep the count of duplicate names List collect4 = collect2.keySet (). Stream (). Filter (key-> collect2.get (key) > 1) .map (key-> {Map map = new HashMap (); map.put ((String) key, collect2.get (key)); return map;}) .names (Collectors.toList ()); System.out.println ("method 3-duplicate names and counts:" + collect4);}
Run the results to make it easy to verify whether it is needed.
Raw data: [User (name=zhangsan, address=beijing, age=30), User (name=zhangsan, address=beijing, age=40), User (name=lisi, address=shanghai, age=35), User (name=lisi, address=shanghai, age=28), User (name=lisim, address=shanghai, age=32)]
Method 1-duplicate names
Duplicate name count: {lisi=2, zhangsan=2, lisim=1}
Method 2-duplicate name: [lisi, zhangsan]
Method 3-duplicate name and count: [{lisi=2}, {zhangsan=2}]
Five ways of removing weight from list
How to repeat the list that is often asked in the interview, which is usually dictated and does not need to be reflected in the code. At this time, you must think clearly and list the centralized methods to remove the weight, in order to show your mastery of the list data structure and related methods, and whether your basic knowledge of java is solid.
Next, I will show the five methods one by one.
Create a new list array:
List list = new ArrayList (); list.add (26); list.add (39); list.add (5); list.add (40); list.add (39); list.add (25); System.out.println (list); method 1: List weight removal using java8's new feature stream: List newList = list.stream (). Distinct (). Collect (Collectors.toList ()) System.out.println ("java8's new feature stream weight removal:" + newList); list.add (39); method 2: double for cycle weight removal for (int I = 0; I < list.size (); iTunes +) {for (int j = 0; j < list.size (); int +) {if (iDeduplication jacks weight list.get (I) = list.get (j)) {list.remove (list.get (j);}
After the study of the above method, there is indeed a small problem, the optimized method is put below (not recommended, the speed is too slow)
For (int I = 0; I < list.size ()) {for (int j = 0; j < list.size ();) {/ / System.out.println (I + "-" + list.get (I) + "-" + j + "!" + list.get (j)); if (I! = j & & list.get (I) = = list.get (j)) {/ / System.out.println (j + ":" + list.get (j)); list.remove (j);} else {jacks + } System.out.println ("double for cycle weight removal:" + list); list.add (39); method 3: set set judgment weight removal without disturbing the order Set set1 = new HashSet (); List newList1 = new ArrayList (); for (Integer integer: list) {if (set1.add (integer)) {newList1.add (integer);} System.out.println ("set set judgment weight removal:" + list) List.add (39); method 4: determine after traversal to assign to another list set List newList2 = new ArrayList (); for (Integer integer: list) {if (! newList2.contains (integer)) {newList2.add (integer);}} System.out.println ("assign new list weight removal:" + newList2); list.add (39); method 5: set and list transform to deduplicate Set set2 = new HashSet (); List newList3 = new ArrayList (); set2.addAll (list); newList3.addAll (set2) System.out.println ("set and list conversion de-duplicate:" + newList3); at this point, I believe you have a better understanding of "how java8 checks whether a property of a List object is duplicated". 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.