如何使用 Java 8 个流创建默认列表?
How to create a default list using Java 8 streams?
我有一个对象 Shop 如下。在主要功能中,我正在尝试处理一个列表。如果它的结果是一个空列表,那么我正在创建一个默认列表,然后进一步处理它。我不确定如何创建默认列表,然后使用 java 8 个流一起进一步处理它。
在第 1 节中,我正在处理一个列表。在第 2 节中,我正在创建第 1 节 returns 空列表的默认列表。而在section 3中,我正在进一步处理section 1和section 2的结果。
class Shop {
String name;
String value;
public Shop(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class Product {
List<Shop> list = new ArrayList<>();
public List<Shop> getList() {
return list;
}
public void setList(List<Shop> list) {
this.list = list;
}
}
public class Test {
public static void main(String[] args) {
Shop s1 = new Shop("zip", "32684");
Shop s2 = new Shop("loc", "abc");
Shop s3 = new Shop("zip", "32684");
Shop s4 = new Shop("brand", "lemom");
List<Shop> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Product p = new Product();
p.setList(list);
List<String> attributes = new ArrayList<>();
attributes.add("zip");
attributes.add("loc");
attributes.add("brand");
attributes.add("price");
//How can I create a default list (section 2) if section 1 returns empty list. And then combine section 1 ,2 and 3 togethr using Java 8 Streams
for (String attribute : attributes) {
//section 1
List<Shop> values = p.getList().stream()
.filter(shop -> attribute.equals(shop.getName()))
.collect(Collectors.toList());
//section 2
if (values.isEmpty()) {
values.add(new Shop("zip", "3728392"));
}
//section 3
List<String> list3 = new ArrayList<>();
for (Shop shop : values) {
list3.add(shop.getValue());
}
String result = String.join(",", list3);
}
}
}
String a = p.getList().stream()
.filter(shop -> attribute.equals(shop.getName()))
.map(Shop::getValue)
.collect(Collectors.collectingAndThen(Collectors.joining(","),
str -> str.isEmpty() ? "3728392" : str));
.map()
使用 Java 的双冒号运算符将您的对象 shop
转换为字符串 shop.getValue()
- 这是您的第 3.
部分
自定义 .collect()
收集上一个操作的所有值并将它们转换为逗号分隔的字符串。如果结果字符串为空,则它 returns 是一个单独的值 -- 这是您的第 2 部分。
输出:
32684,32684
abc
lemom
3728392
我有一个对象 Shop 如下。在主要功能中,我正在尝试处理一个列表。如果它的结果是一个空列表,那么我正在创建一个默认列表,然后进一步处理它。我不确定如何创建默认列表,然后使用 java 8 个流一起进一步处理它。 在第 1 节中,我正在处理一个列表。在第 2 节中,我正在创建第 1 节 returns 空列表的默认列表。而在section 3中,我正在进一步处理section 1和section 2的结果。
class Shop {
String name;
String value;
public Shop(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class Product {
List<Shop> list = new ArrayList<>();
public List<Shop> getList() {
return list;
}
public void setList(List<Shop> list) {
this.list = list;
}
}
public class Test {
public static void main(String[] args) {
Shop s1 = new Shop("zip", "32684");
Shop s2 = new Shop("loc", "abc");
Shop s3 = new Shop("zip", "32684");
Shop s4 = new Shop("brand", "lemom");
List<Shop> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Product p = new Product();
p.setList(list);
List<String> attributes = new ArrayList<>();
attributes.add("zip");
attributes.add("loc");
attributes.add("brand");
attributes.add("price");
//How can I create a default list (section 2) if section 1 returns empty list. And then combine section 1 ,2 and 3 togethr using Java 8 Streams
for (String attribute : attributes) {
//section 1
List<Shop> values = p.getList().stream()
.filter(shop -> attribute.equals(shop.getName()))
.collect(Collectors.toList());
//section 2
if (values.isEmpty()) {
values.add(new Shop("zip", "3728392"));
}
//section 3
List<String> list3 = new ArrayList<>();
for (Shop shop : values) {
list3.add(shop.getValue());
}
String result = String.join(",", list3);
}
}
}
String a = p.getList().stream()
.filter(shop -> attribute.equals(shop.getName()))
.map(Shop::getValue)
.collect(Collectors.collectingAndThen(Collectors.joining(","),
str -> str.isEmpty() ? "3728392" : str));
.map()
使用 Java 的双冒号运算符将您的对象 shop
转换为字符串 shop.getValue()
- 这是您的第 3.
自定义 .collect()
收集上一个操作的所有值并将它们转换为逗号分隔的字符串。如果结果字符串为空,则它 returns 是一个单独的值 -- 这是您的第 2 部分。
输出:
32684,32684
abc
lemom
3728392