Java 8流:条件收集器 [英] Java 8 streams: conditional Collector
问题描述
我想使用Java 8流将List of String值转换为单个String。像A,B这样的值列表应返回类似值:'A','B'添加的字符串。这工作正常,但我想根据值的数量更改前缀和后缀。例如,如果我有一个只有A的列表,我希望生成的字符串为值'A'已添加。
I want to use Java 8 streams to convert a List of String values to a single String. A List of values like "A", "B" should return a String like "Values: 'A', 'B' added". This works fine, however I want to change the Pre- and Postfix depending on the amount of values. For example, if I have a List of only "A" I want the resulting String to be "Value 'A' added".
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.List;
public class HelloWorld
{
public static void main(String[] args)
{
List<String> values = new ArrayList<>();
values.add("A");
values.add("B");
values.add("C");
List<String> value = new ArrayList<>();
value.add("A");
System.out.println(log(values));
System.out.println(log(value));
}
public static String log(List<String> values){
return values.stream()
//.filter(...)
.map(x -> "'" + x + "'")
.collect(Collectors.joining(",","values:"," added"));
}
}
有没有办法改变Collctor,取决于结果列表的大小?然后我可以做类似的事情
Is there a way to change the Collctor, depending on the size of the resulting List? Then I could do something like
.collect(Collectors.joining(",", size = 1 ? "Value " : "Values: "," added"));
我更喜欢没有中间List结果的单流操作。我之前也不知道结果,因为我过滤了流。
I would prefer a single stream operation without an intermediate List result. I also do not know the result beforehand, because I filter the stream.
编辑:我最终使用了Eugene的建议。我想要做的是找到两个列表之间的差异,并返回人类可读形式的差异。很好用!
I ended up using Eugene's suggestion. What I wanted to do is find the differences between two Lists and return the differences in human readable form. Works nicely!
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.List;
public class HelloWorld
{
public static void main(String[] args)
{
List<String> oldValues = new ArrayList<>();
oldValues.add("A");
oldValues.add("B");
oldValues.add("C");
List<String> newValues = new ArrayList<>();
newValues.add("A");
newValues.add("C");
newValues.add("D");
newValues.add("E");
System.out.println(HelloWorld.<String>log(oldValues, newValues, " deleted"));
System.out.println(HelloWorld.<String>log(newValues, oldValues, " added"));
}
public static <T> String log(List<T> first, List<T> second, String postfix) {
return (String) first
.stream()
.filter(x -> !second.contains(x))
.map(x -> "'" + x.toString() + "'").
collect(Collectors.collectingAndThen(Collectors.toList(),
list -> {
if (list.size() == 1) {
return "Value " + list.get(0) + postfix;
}
if (list.size() > 1) {
List<String> strings = new ArrayList<>(list.size());
for (Object object : list) {
strings.add(object.toString());
}
return "Values: " + String.join(",", strings) + postfix;
}
return "";
}));
}
}
输出:
Value 'B' deleted
Values: 'D','E' added
推荐答案
你可以通过以下方式完成:
You could do it via:
return values.stream()
.map(x -> "'" + x + "'")
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> {
if (list.size() == 1) {
return "value" + list.get(0);
}
if (list.size() > 1) {
return String.join(",", list);
}
return "Nothing found";
}));
这篇关于Java 8流:条件收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!