Java 8基于多个条件和日期对对象的ArrayList进行排序

huangapple go评论84阅读模式
英文:

Java 8 sorting Arraylist of object based on multiple conditions and date

问题

以下是翻译好的内容:

我有一个对象关系列表其中包含一个名为Contact的对象它将包含elecType对象或postType对象

**关系**
关系
日期15/10/20
联系人
  代码1
  使用代码1
  电子类型对象):(电子代码1详细信息ssss
关系
日期14/10/20
联系人
  代码1
  使用代码2
  电子类型对象):(电子代码2详细信息yyy
关系
日期10/10/20
联系人
  代码1
  使用代码2
  电子类型对象):(电子代码2详细信息eee
关系
日期13/10/20
联系人
  代码1
  使用代码2
  电子类型对象):(电子代码1详细信息zzz
关系
日期15/10/20
联系人
  代码1
  使用代码1
  电子类型对象):(电子代码2详细信息ttt
关系
日期12/10/20
联系人
  代码1
  邮政类型对象):(详细信息xxx
关系
日期11/10/20
联系人
  代码2
  邮政类型对象):(详细信息yyy
关系
日期13/10/20
联系人
  代码2
  邮政类型对象):(详细信息zzz

我需要根据以下条件对关系和联系人对象进行排序
如果代码为2我需要获取每个具有不同代码的联系人的最新日期关系对象
从上面的示例中将得到最终输出

关系
日期12/10/20
联系人
  代码1
  邮政类型对象):(详细信息xxx
关系
日期13/10/20
联系人
  代码2
  邮政类型对象):(详细信息zzz

类似地如果代码为1我需要获取每个具有不同usageCodeecode的联系记录的最新日期关系
从上面的数据中输出将是

关系
日期15/10/20
联系人
  代码1
  使用代码1
  电子类型对象):(电子代码1详细信息ssss
关系
日期15/10/20
联系人
  代码1
  使用代码1
  电子类型对象):(电子代码2详细信息ttt
关系
日期13/10/20
联系人
  代码1
  使用代码2
  电子类型对象):(电子代码1详细信息zzz
关系
日期14/10/20
联系人
  代码1
  使用代码2
  电子类型对象):(电子代码2详细信息yyy

Java类
------------

public class Relationship {
  private Date date;
  private Contact contact;
}

public class Contact{
  private Integer code;
  private Integer usageCode;
  private PostalType postalType;
  private ElecType elecType;
}

public class PostalType{
  private String detail;
}

public class ElecType{
  private String detail;
  private Integer eCode;
}

在Java 8或更高版本中最佳实现方法是什么是否可以使用lambda和流来实现)?
英文:

I have a list of object Relationships and inside that there is a object named Contact it will contain either elecType object or postType

**Relationships:**
Relationship:
date :15/10/20
Contact :
code : 1
usageCode : 1
elecType(object) :(ecode : 1, detail : ssss )
Relationship:
date :14/10/20
Contact :
code : 1
usageCode : 2
elecType(object) :(ecode : 2, detail :yyy )
Relationship:
date :10/10/20
Contact :
code : 1
usageCode : 2
elecType(object) :(ecode : 2, detail :eee )
Relationship:
date :13/10/20
Contact :
code : 1
usageCode : 2
elecType(object) :(ecode : 1, detail :zzz )
Relationship:
date :15/10/20
Contact :
code : 1
usageCode : 1
elecType(object) :(ecode : 2, detail :ttt )
Relationship:
date:12/10/20
Contact :
code : 1
postType(object) : ( detail :xxx )
Relationship:
date:11/10/20
Contact :
code : 2
postType(object) : (detail :yyy )
Relationship:
date:13/10/20
Contact :
code : 2
postType(object) : (detail :zzz )

i need to sort the Relationship,Contacts objects based on the below conditions
if the code is 2, i need to get the latest dated Relationship objects from each Contacts which having different code
ie : The will be final output from the above example

Relationship:
date:12/10/20
Contact :
code : 1
postType(object) : (detail :xxx )
Relationship:
date:13/10/20
Contact :
code : 2
postType(object) : (detail :zzz )

Similarly if the code is 1 , i need to get the latest dated Relationship from each Contacts records which having different usageCode,ecode

ie : from the above data,the output will be

    Relationship:
date :15/10/20
Contact :
code : 1
usageCode : 1
elecType(object) :(ecode : 1, detail : ssss )
Relationship:
date :15/10/20
Contact :
code : 1
usageCode : 1
elecType(object) :(ecode : 2, detail :ttt )
Relationship:
date :13/10/20
Contact :
code : 1
usageCode : 2
elecType(object) :(ecode : 1, detail :zzz )
Relationship:
date :14/10/20
Contact :
code : 1
usageCode : 2
elecType(object) :(ecode : 2, detail :yyy )

Java Classes

    public class Relationship {
private Date date;
private Contact contact;
}
public class Contact{
private Integer code;
private Integer usageCode;
private PostalType postalType;
private ElecType elecType;
}
public class PostalType{
private String detail;
}
public class ElecType{
private String detail;
private Integer eCode
}

What is the best way to implement this in Java 8 or higher (is it possible to achieve using lambda and streams)

答案1

得分: 1

你可以在你的Relationship类中实现Comparable<Relationship>,然后使用以下代码对列表进行排序:

Collections.sort(testList);

如果你想筛选列表,可以使用带有筛选条件的流操作。

// 筛选出代码为1的关系列表
relationships.stream().filter(r -> r.getContacts().getCode() == 1).collect(Collectors.toList());
英文:

You can just implement Comparable<Relationship> in your class Relationship and sort a list with

Collections.sort(testList);

If you want to filter your list, you can use a stream with filter.

// filters a List with relationships with code 1
relationships.stream().filter(r -&gt; r.getContacts().getCode() == 1).collect(Collectors.asList()); 

huangapple
  • 本文由 发表于 2020年9月10日 15:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63824483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定