gpt4 book ai didi

java - 我希望这段代码使用 Stream、Lambda 表达式、双冒号运算符在 Java8 中编写

转载 作者:行者123 更新时间:2023-11-30 12:03:49 31 4
gpt4 key购买 nike

<分区>

有一个类说 AllOrderItems,在主要方法中我们有几个 AllOrderItems,我必须检查几个标签 {"AttriuteL"、"AttriuteG"、"AttriuteR"} 出现在任何订单中并收集它们,我已经用普通的 for 循环编写了它,但需要使用 Stream、Lambda 表达式和双冒号对 Java 8 进行增强

完整代码:

public class AllOrderItems {

private String OrderId;
private String CustomerName;
private int Quantity;
private String AttriuteL;
private String AttriuteG;
private String AttriuteR;

public String getAttriuteL() {
return AttriuteL;
}

public void setAttriuteL(String AttriuteL) {
this.AttriuteL = AttriuteL;
}

public String getAttriuteG() {
return AttriuteG;
}

public void setAttriuteG(String AttriuteG) {
this.AttriuteG = AttriuteG;
}

public String getAttriuteR() {
return AttriuteR;
}

public void setAttriuteR(String AttriuteR) {
this.AttriuteR = AttriuteR;
}

public String getOrderId() {
return OrderId;
}

public void setOrderId(String orderId) {
OrderId = orderId;
}

public String getCustomerName() {
return CustomerName;
}

public void setCustomerName(String customerName) {
CustomerName = customerName;
}

public int getQuantity() {
return Quantity;
}

public void setQuantity(int quantity) {
Quantity = quantity;
}


public String getTagValue(String instanceCharacterstics){
if(instanceCharacterstics.equals("AttriuteG"))
return getAttriuteG();
else if("AttriuteL".equals(instanceCharacterstics))
return getAttriuteL();
else
return getAttriuteR();
}
}


import java.util.*;
import java.util.stream.Collectors;

public class StreamPractise {

public static void main(String[] args) {

List<Student> studetnList = setStudentDetails();
List<AllOrderItems> orderItems = setCustomerOrders();
List<String> tagList = Arrays.asList(new String[]{"AttriuteL", "AttriuteG", "AttriuteR"});

List<String> orderItemValue = new ArrayList<>();
for(String tag: tagList){
for(AllOrderItems orders: orderItems){
String val= orders.getTagValue(tag);
if(val != null && !val.isEmpty()){
orderItemValue.add(val);
}
}
}

System.out.println(orderItemValue);
}


private static List<AllOrderItems> setCustomerOrders() {
AllOrderItems c1 = new AllOrderItems();
AllOrderItems c2 = new AllOrderItems();
AllOrderItems c3 = new AllOrderItems();


c1.setOrderId("Ord4521");
c1.setCustomerName("Ontereo");
c1.setQuantity(3);

c2.setOrderId("Ord2134");
c2.setCustomerName("Rio");
c2.setQuantity(5);
c2.setAttriuteG("00100");
c2.setAttriuteL("");
c2.setAttriuteR("OF010");

c3.setOrderId("Ord8475");
c3.setCustomerName("Jack");
c3.setQuantity(10);

List<AllOrderItems> orderItems = new ArrayList<>();
orderItems.add(c1);
orderItems.add(c2);
orderItems.add(c3);

return orderItems;
}
}

想要一个值列表,其中存在三个标签,或者如果它返回 true 或 false 说明是否所有三个标签都存在也很好,这意味着有可能说每个订单项中的一个标签可能为空出现在任何订单项上,如果任何标 checkout 现除 null 或空白以外的任何值,则将检查下一个标签。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com