Summary of deduplication and sorting according to the properties of objects in the list (must read)
As shown below:
//Do notpublic class User { private int id; private String name; private int age; public User(){} public User(int id, String name, int age) { super(); = id; = name; = age; } public int getId() { return id; } public void setId(int id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { = age; } @Override public String toString() { return "User [, name=" + name + ", age=" + age + "]"; } } public class ListTest { /** * There is a List<User> list that places five objects: user1, user2, user3, user4, user5 User has three attributes: Id, name, and age The record of user2 is roughly like this: "100", "abc", 20; The record of user3 is roughly like this: "100", "def", 20; How can I only keep one object in user2 and user3 and merge the name in it into a new object? New objects such as "100", "abcdef", 20 This is just an example. In reality, it is possible that user4 and user5 are similar to this. If there are two objects with the same id, then do it Merge, only one object is retained, and a common method can be used to filter out two objects with the same ID in the object collection and merge them. Still retained in the original list * @param args */ // List is ordered and repeatable, set is unordered and cannot be repeated, mapkey is not allowed to be repeated, the value after the same key will overwrite the previous one //The data stored in List is stored in the order when it is placed by default. For example, if A, B, and C are placed in sequence, then when it is obtained, it is also the order of A, B, and C. public static void main(String[] args) { List<User> list = new ArrayList<>(); (new User(1,"a",20)); (new User(1,"a",20)); (new User(2,"a",20)); (new User(3,"b",20)); (new User(1,"c",20)); (new User(4,"d",20)); (new User(2,"e",20)); (new User(1,"a",20)); /* for (User user : list) { (()); } ();*/ list = mySort(list); for (User user : list) { (()); } } public static List<User> mySort(List<User> list){ HashMap<Integer,User> tempMap = new HashMap<>(); for (User user : list) { int key = (); // containsKey(Object key) This method determines whether the specified key name is included in the Map collection object. Return true if the Map collection contains the specified key name, otherwise false// containsValue(Object value) value: the specified key-value object of the Map collection to be queryed. Return true if the specified key value is contained in the Map collection, otherwise return false if((key)){ User tempUser = new User(key, (key).getName() + (), (key).getAge());//(); //HashMap does not allow key duplication, so if there is a key duplication, the previous value will be overwritten by the subsequent value (key, tempUser); }else{ (key, user); } } List<User> tempList = new ArrayList<>(); for(int key : ()){ ((key)); } return tempList; } } //Sorting==========================================================================================================================================================================================================================================================public class Student { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { = age; } public String getName() { return name; } public void setName(String name) { = name; } @Override public String toString() { return "Student [age=" + age + ", name=" + name + "]"; } } public class ListSort { public static void main(String[] args) { List<Student> list = new ArrayList<Student>(); //Create 3 student objects, ages 20, 19, 21, and put them in the List in turn. Student s1 = new Student(); (20); ("Ge Da"); Student s2 = new Student(); (19); ("Zhang Jie"); Student s3 = new Student(); (21); ("Bao Ye"); (s1); (s2); (s3); ("Before sorting:"+list); (list, new Comparator<Student>(){ /* * int compare(Student o1, Student o2) Returns an integer of the basic type, * Return negative number: o1 is less than o2, * Return to 0 means: o1 and o2 are equal, * Return positive number to indicate: o1 is greater than o2. */ public int compare(Student o1, Student o2) { //Arrange ascending order according to the age of the students; < is descending order// /*if(() > ()){ // return 1; // } // if(() == ()){ // return 0; // } // return -1; */ // return ()-();// Ascending order// return ()-();// descending order return ().compareTo(()) ;// Ascending order by name// return ().compareTo(());// descending order by name } }); ("After sorting:"+list); } }
The above article is the summary of repetition and sorting of the attributes of objects in the list (must-read article) which I share with you. I hope it can give you a reference and I hope you can support me more.
Related Articles
Discussing the implementation principle of ServiceLoader in detail
Below, the editor will bring you an article detailing the implementation principle of ServiceLoader. The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor2017-02-02Detailed explanation of several ways to use jar packages in Java
This article mainly introduces several ways to make Java jar packages. This article introduces you in very detailed steps and has certain reference value. Friends who need it can refer to it.2019-11-11Problems and solutions for failure to use Gradle to generate dependency packages
This article mainly introduces the problems and solutions of failed use of Gradle to use dependency packages. It has good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2023-04-04Netty’s solution to unpacking and dipping problem
This article mainly introduces a detailed explanation of Netty’s unpacking and packaging problem solution. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase as soon as possible.2022-11-11Spring boot explains in detail how to solve the problem of fastjson filtering field as null value
This article mainly introduces the problem of solving the problem that the fastjson filter field in Spring boot is null value. It has good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2022-07-07Implementation of JMeter custom logs and log analysis
JMeter, like Java programs, can record event logs. This article introduces the implementation of JMeter custom logs and log analysis, which has certain reference value. Interested friends can refer to it.2021-12-12Example of Elasticsearch statistics based on Spring Data Jest
This article mainly introduces the example of Elasticsearch data statistics based on Spring Data Jest. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor2018-02-02Java implements simple word segmentation function
The search function is an important feature and function of a system with database functions. Common search functions in life basically have word segmentation search function. However, although the ES function is powerful, it is too labor-intensive for students or small projects. If you write a word segmentation tool, it will add icing on the cake to the project, making it not just a system that can only search for keywords. Friends who need it can refer to it.2021-06-06CopyOnWriteArrayList source code analysis of java collection
This article mainly introduces the source code analysis of the java collection CopyOnWriteArrayList. The container array is modified by volatile, that is, the set and get methods are thread-safe. The entire addition process is locked, so the overall thread safety is ensured through volatile and lock. Friends who need it can refer to it.2023-12-12Java implements file renaming
This article mainly introduces the Java implementation file renaming in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.2020-03-03