【问题标题】:Java 8 sorting Arraylist of object based on multiple conditions and dateJava 8基于多个条件和日期对对象的Arraylist进行排序
【发布时间】:2020-12-28 15:27:51
【问题描述】:

我有一个对象关系列表,其中有一个名为 Contact 的对象,它将包含 elecType 对象或 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 )

我需要根据以下条件对关系、联系人对象进行排序 如果代码为 2,我需要从每个具有不同代码的联系人中获取最新的关系对象 ie : 将是上述示例的最终输出

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

同样,如果代码为 1,我需要从每个具有不同 usageCode、ecode 的联系人记录中获取最新的日期关系

ie : 从上面的数据,输出将是

    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 类

    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 和流来实现)

【问题讨论】:

  • @Holger 为问题添加了 java 类
  • 这是一个草图,充其量。不是真正的代码。

标签: java lambda java-8 java-stream comparator


【解决方案1】:

你可以在你的类关系中实现 Comparable 并使用

Collections.sort(testList);

如果要过滤列表,可以使用带有过滤器的流。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 2017-09-17
    • 2020-08-23
    • 1970-01-01
    • 2018-03-22
    • 2014-11-09
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多