Summary of de-weighting and sorting based on the attributes of objects in the list, list Summary

Source: Internet
Author: User

Summary of de-weighting and sorting based on the attributes of objects in the list, list Summary

// Remove public class User {private int id; private String name; private int age; public User () {} public User (int id, String name, int age) {super (); this. id = id; this. name = name; this. age = age;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return Ge;} public void setAge (int age) {this. age = age ;}@ Override public String toString () {return "User [id =" + id + ", name =" + name + ", age = "+ age +"] ";}} public class ListTest {/*** has a List <User> list with five objects: user1, user2, user3, user4, and user5 users have three attributes: Id, name, and age. The record of user2 is like this: "100", "abc", 20; the record of user3 is like this: "100", "def", 20; how can I keep only one object in user2 and user3, and merge the name into the new object, new objects such as "100", "abcdef", 20. Example: in practice, it is possible that user4 and user5 are similar. If there are two objects with the same id, merge them, retain only one object, and find a common method, you can filter out two objects with the same ID in the object set, merge them into the original list. * @ param args * // The list is ordered and repeatable, the set is unordered, And the mapkey is not allowed to be repeated, the values after the same key will overwrite the data stored in the // List. By default, the data is stored in the order when it is put, such as A, B, and C, the public static void main (String [] args) {List <User> list = new ArrayList <> (); list. add (new User (1, "a", 20); list. add (new User (1, "a", 20); list. add (new User (2, "a", 20 )); List. add (new User (3, "B", 20); list. add (new User (1, "c", 20); list. add (new User (4, "d", 20); list. add (new User (2, "e", 20); list. add (new User (1, "a", 20);/* for (User user User: list) {System. out. println (user. toString ();} System. out. println (); */list = mySort (list); for (User user: list) {System. out. println (user. toString () ;}} public static List <User> mySort (List <User> list) {HashMap <Integer, User> TempMap = new HashMap <> (); for (User user: list) {int key = user. getId (); // containsKey (Object key) This method determines whether the Map set Object contains the specified key name. If the Map set contains the specified key name, true is returned; otherwise, false // containsValue (Object value) value is returned: the specified key value Object of the Map set to be queried. if the Map set contains the specified key value, true is returned; otherwise, false if (tempMap. containsKey (key) {User tempUser = new User (key, tempMap. get (key ). getName () + user. getName (), tempMap. get (key ). getAge (); // user. getAge (); // HashMap does not allow duplicate keys. Therefore, if duplicate keys exist, the previous value will be overwritten by the subsequent value. put (key, tempUser);} else {tempMap. put (key, user) ;}list <User> te MpList = new ArrayList <> (); for (int key: tempMap. keySet () {tempList. add (tempMap. get (key) ;}return tempList ;}} // sort = ========= public class Student {private int age; private String name; public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = nam E ;}@ 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 three Student objects, age: 20, 19, and 21 respectively, and put them into the List in sequence. Student s1 = new Student (); s1.setAge (20); s1.setName ("Ge da "); student s2 = new Student (); s2.setAge (19); s2.setName ("Zhang Jie"); Student s3 = new Student (); S3.setAge (21); s3.setName (""); list. add (s1); list. add (s2); list. add (s3); System. out. println ("Before sorting:" + list); Collections. sort (list, new Comparator <Student> () {/** int compare (Student o1, Student o2) returns an integer of the basic type, * returns a negative number indicating that o1 is less than o2, * 0 indicates that o1 is equal to o2. * positive number indicates that o1 is greater than o2. */Public int compare (Student o1, Student o2) {// sort by Student age in ascending order; <yes in descending order // * if (o1.getAge ()> o2.getAge () {// return 1; //} // if (o1.getAge () = o2.getAge () {// return 0; ///} // return-1; * // return o1.getAge ()-o2.getAge (); // return o2.getAge ()-o1.getAge (); // return o1.getName () in descending order (). compareTo (o2.getName (); // sort by name in ascending order // return o2.getName (). compareTo (o1.getName (); // sort by name in descending order}); System. out. println ("sorted:" + list );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.