简体   繁体   English

将对象添加到 stream 中的两个列表

[英]Adding objects to two lists from a stream

how can I add objects in two different lists from a stream in another way?如何以另一种方式将对象添加到 stream 的两个不同列表中?

here's an example这是一个例子

ArrayList<String> someList = new ArrayList();
someList.add("123QWE");
someList.add("QWE123");
//...
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
someList.stream().forEach(string -> {
    if (Character.isDigit(string.charAt(0))
    {
        list1.add(string);
    }
    else
    {
        list2.add(string);
    }
}

Well, using Streams, you could also achieve this with a single stream:那么,使用 Streams,您还可以使用单个 stream 实现此目的:

Map<Boolean, List<String>> partition = someList.stream()
    .collect(Collectors.partitioningBy(str -> Character.isDigit(str.charAt(0))));

Now现在

  • partition.get(true) returns the list with string starting with digits; partition.get(true)返回以数字开头的字符串列表;
  • partition.get(false) returns the others. partition.get(false)返回其他的。

Based on your updated question I would still go with a simple loop.根据您更新的问题,我仍然会使用一个简单的循环 go。 Streams are great for some operations but their internals are quite complex.流对于某些操作非常有用,但它们的内部结构非常复杂。 You can always call Collections.sort() on each list once the modifications to each list are complete.一旦对每个列表的修改完成,您始终可以在每个列表上调用Collections.sort()

This is not to say it can't be done with streams.这并不是说它不能用流来完成。 But imho, there is nothing gained by doing so.但是恕我直言,这样做没有任何好处。

ArrayList<Object> list1 = new ArrayList<>();
ArrayList<Object> list2 = new ArrayList<>();

for (String str : someList) {
     if (Character.isDigit(str.charAt(0)) {
         list1.add(str);
     } else {
         list2.add(str);
     }
}
Collections.sort(list1);
Collections.sort(list2);

But if you really want to use streams then here is one way.但是,如果您真的想使用流,那么这是一种方法。 It includes sorting (which you said you wanted in a comment) after you get done segregating the original list.在完成对原始列表的分离后,它包括排序(你在评论中说过你想要排序)。

  • define a sortedList collector to share for both streams.定义一个sortedList收集器以共享两个流。 Not necessary but reduces code.没有必要,但减少了代码。
  • the collector first collects them into a list.收集器首先将它们收集到一个列表中。
  • then sorts them.然后对它们进行排序。
Collector<String,?,List<String>>  sortedList = Collectors.collectingAndThen(
        Collectors.toList(),
        lst->{Collections.sort(lst); return lst;});
        
List<String> someList = List.of("B","3C","4E","A", "2B","C", "E", "1A");

List<String> list1 = someList.stream()
        .filter(str -> !Character.isDigit(str.charAt(0)))
        .collect(sortedList);

List<String> list2 = someList.stream()
        .filter(str -> Character.isDigit(str.charAt(0)))
        .collect(sortedList);

                
System.out.println(list1);
System.out.println(list2);

prints印刷

[A, B, C, E]
[1A, 2B, 3C, 4E]

Use stream.filter() and collect() like this:像这样使用stream.filter()collect()

list1 = someList.stream().filter(str -> Character.isDigit(str.charAt(0))).collect(Collectors.toList());
list2 = someList.stream().filter(str -> !Character.isDigit(str.charAt(0))).collect(Collectors.toList());

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

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