简体   繁体   English

分区流

[英]Partitioning a Stream

I am working with a Stream of Person data and I've run into a partitioning issue related to the data. 我正在使用Person数据Stream ,我遇到了与数据相关的分区问题。

I have a Stream of data which I'll represent below in a table: 我有一个数据Stream ,我将在下表中表示:

ID  Name Ticket IsEmployee
1   A      Y        Y
2   B     
3   C      Y
4   D

I am trying to return a List that is sorted by: 我想返回一个按以下顺序排序的List

  1. whether or not they're an Employee 他们是否是员工
  2. if they have any Tickets 如果他们有任何门票
  3. then by Name 然后按名称

I've looked into Collections.groupBy and Collections.partitioningBy , but so far haven't been able to come up withe the correct result. 我查看了Collections.groupByCollections.partitioningBy ,但到目前为止还没有能够得到正确的结果。

My expectations are to return a list in the following order (by ID): 我的期望是按以下顺序返回一个列表(按ID):

1   [name="A",Ticket="**[100,101]**", IsEmployee="**Y**"],
3   [name="C",Ticket="**[200,201]**", IsEmployee=""],
2   [name="**B**",Ticket="", IsEmployee=""],
4   [name="D",Ticket="", IsEmployee=""]

Any thoughts on how this might be accomplished without having to totally break apart the Stream? 有关如何实现这一点的任何想法,而不必完全分裂流?

Below is what my Person looks like: 以下是我的Person的样子:

public class Person {

   private long id;
   private String name;
   private List<Ticket> tickets;
   private String employeeType;  // This is just a 'Y'/'N' value.  This property has morphed into something else but I'm stuck using it.

   public long getId(){
    return id;
   }
   public String getName(){
    return name;
   }
   public List<Ticket> getTickets(){
    return tickets;
   }
   public String getEmployeeType(){
    return id;
   }
   // Setters are the exact same as getters, meaning I have no transient methods
}

Since you are saying “ I am trying to return a List that is sorted by …”, it's not clear why you started looking for grouping or partitioning, instead of aiming exactly at, what your problem description is about, get a List and sort it by your criteria: 因为您说“ 我正在尝试返回按 ... 排序的列表 ”,所以不清楚为什么您开始寻找分组或分区,而不是准确地瞄准您的问题描述是什么,获取List并对其进行排序根据您的标准:

// collect into a mutable List, if it isn’t a mutable List in the first place
List<Person> list=stream.collect(Collectors.toCollection(ArrayList::new));

// sort by your specified criterie (note: false < true)
list.sort(Comparator.comparing((Person p) -> !p.getEmployeeType().equals("y"))
                    .thenComparing(p -> p.getTickets().isEmpty())
                    .thenComparing(Person::getName) );

You can also specify the operation as part of the Stream operation, eg 您还可以将操作指定为Stream操作的一部分,例如

List<Person> list=stream
    .sorted(               
        Comparator.comparing((Person p) -> !p.getEmployeeType().equals("y"))
                  .thenComparing(p -> p.getTickets().isEmpty())
                  .thenComparing(Person::getName) )
    .collect(Collectors.toList());

but there is no technical benefit. 但没有技术上的好处。 The Stream implementation has to collect the entire contents into a temporary buffer internally, to sort it before it is collected (ie copied) into the resulting List . Stream实现必须在内部将整个内容收集到临时缓冲区中,以便在将其收集(即复制)到生成的List之前对其进行排序。 The in-place sorting of ArrayList may get away with lesser or even without data copying (not that it matters for four elements, but in general). ArrayList的就地排序可能会在较少甚至没有数据复制的情况下消失(对于四个元素而言,这一点并不重要,但一般而言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM