Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8基于多个条件和日期的对象排序Arraylist_Java_Lambda_Java 8_Java Stream_Comparator - Fatal编程技术网

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

Java 8基于多个条件和日期的对象排序Arraylist,java,lambda,java-8,java-stream,comparator,Java,Lambda,Java 8,Java Stream,Comparator,我有一个对象关系列表,其中有一个名为Contact的对象,它将包含elecType对象或postype对象 **Relationships:** Relationship: date :15/10/20 Contact : code : 1 usageCode : 1 elecType(object) :(ecode : 1, detail : ssss ) Relationship: date :14/10/20 Contact : code

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

**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类
在Java 8或更高版本中实现这一点的最佳方法是什么(使用lambda和streams是否可能实现)

您可以在类关系中实现Comparable,并使用

Collections.sort(testList);
如果要筛选列表,可以使用带筛选器的流

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

Use@Holger为这个问题添加了java类,充其量只是一个草图。不是真正的代码。
// filters a List with relationships with code 1
relationships.stream().filter(r -> r.getContacts().getCode() == 1).collect(Collectors.asList());