如下所示://去重public class User { private int id; private String name; private int age; public User(){} public
如下所示:
//去重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 age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }public class ListTest { //list有序可重复、set无序不可重复、mapkey不允许重复,key相同的后面的value会把前面的覆盖掉 //List存放的数据,默认是按照放入时的顺序存放的,比如依次放入A、B、C,则取得时候,则也是A、B、C的顺序 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)); 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) 该方法判断Map集合对象中是否包含指定的键名。如果Map集合中包含指定的键名,则返回true,否则返回false// containsValue(Object value) value:要查询的Map集合的指定键值对象.如果Map集合中包含指定的键值,则返回true,否则返回false if(tempMap.containsKey(key)){ User tempUser = new User(key, tempMap.get(key).getName() + user.getName(), tempMap.get(key).getAge());//user.getAge();//HashMap是不允许key重复的,所以如果有key重复的话,那么前面的value会被后面的value覆盖 tempMap.put(key, tempUser); }else{ tempMap.put(key, user); } } List<User> tempList = new ArrayList<>(); for(int key : tempMap.keySet()){ tempList.add(tempMap.get(key)); } return tempList; } }//排序=============================================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 = 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>(); //创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中 Student s1 = new Student(); s1.setAge(20); s1.setName("葛大"); Student s2 = new Student(); s2.setAge(19); s2.setName("张杰"); Student s3 = new Student(); s3.setAge(21); s3.setName("宝爷"); list.add(s1); list.add(s2); list.add(s3); System.out.println("排序前:"+list); Collections.sort(list, new Comparator<Student>(){ public int compare(Student o1, Student o2) { //按照学生的年龄进行升序排列 ;<是降序// // return o1.getAge()-o2.getAge();//升序// return o2.getAge()-o1.getAge();//降序 return o1.getName().compareTo(o2.getName()) ;// 按照姓名升序// return o2.getName().compareTo(o1.getName()) ;// 按照姓名降序 } }); System.out.println("排序后:"+list); } }
--结束END--
本文标题: 根据list中对象的属性去重和排序小结(必看篇)
本文链接: https://lsjlt.com/news/226195.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0