欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 15:59:58
1、list去重 people类 public class people { private int id;...

1、list<object>去重

people类

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方法

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

public class mycomparator implements comparator<people> { 
 
  @override 
  public int compare(people p1, people p2) { 
    return p1.getcreatedate().compareto(p2.getcreatedate()); 
  } 
 
} 

main方法

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对象去重和按照某个字段排序的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。