服务器之家

服务器之家 > 正文

List对象去重和按照某个字段排序的实现方法

时间:2020-10-20 10:24     来源/作者:脚本之家

1、List<Object>去重

People类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class People {
 
  private int id;
  private String name;
  private Date createDate;
 
  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 Date getCreateDate() {
    return createDate;
  }
 
  public void setCreateDate(Date createDate) {
    this.createDate = createDate;
  }
 
}

main方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void main(String[] args) throws Exception{
    List<People> list = new ArrayList<People>();
    People p1 = new People();
    p1.setId(1);
    p1.setName("赵");
    list.add(p1);
    People p2 = new People();
    p2.setId(2);
    p2.setName("钱");
    list.add(p2);
    People p3 = new People();
    p3.setId(3);
    p3.setName("孙");
    list.add(p3);
    People p4 = new People();
    p4.setId(4);
    p4.setName("李");
    list.add(p4);
    People p5 = new People();
    p5.setId(5);
    p5.setName("周");
    list.add(p5);
    list.add(p1);
    for(People p : list){
      System.out.println(p);
    }
    System.out.println("==========================");
    Set<People> set = new HashSet<People>(list);
    list.clear();
    list.addAll(new ArrayList<People>(set));
    for(People p : list){
      System.out.println(p);
    }
  }

2、List<Object>按照日期字段排序

自定义Comparator

?
1
2
3
4
5
6
7
8
public class MyComparator implements Comparator<People> {
 
  @Override
  public int compare(People p1, People p2) {
    return p1.getCreateDate().compareTo(p2.getCreateDate());
  }
 
}

main方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public static void main(String[] args) throws Exception{
    List<People> list = new ArrayList<People>();
    People p1 = new People();
    p1.setId(1);
    p1.setName("赵");
    p1.setCreateDate(new Date(new Date().getTime() + 1000));
    list.add(p1);
    People p2 = new People();
    p2.setId(2);
    p2.setName("钱");
    p2.setCreateDate(new Date(new Date().getTime() + 8000));
    list.add(p2);
    People p3 = new People();
    p3.setId(3);
    p3.setName("孙");
    p3.setCreateDate(new Date(new Date().getTime() + 7000));
    list.add(p3);
    People p4 = new People();
    p4.setId(4);
    p4.setName("李");
    p4.setCreateDate(new Date(new Date().getTime() + 5000));
    list.add(p4);
    People p5 = new People();
    p5.setId(5);
    p5.setName("周");
    p5.setCreateDate(new Date(new Date().getTime() + 6000));
    list.add(p5);
    for(People p : list){
      System.out.println(p.getId());
    }
    //1,2,3,4,5
    System.out.println("=======================");
    Collections.sort(list, new MyComparator());
    //1,4,5,3,2
    for(People p : list){
      System.out.println(p.getId());
    }
  }

以上这篇List对象去重和按照某个字段排序的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部