Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        在4个月前关闭。
                                                                                            
                
        
有一个类说AllOrderItems,在主方法中我们只有几个AllOrderItems,我必须检查几个标签{“ AttriuteL”,“ AttriuteG”,“ AttriuteR”}出现在任何订单中并收集它们,我已经写了在正常的for循环中,但需要使用Stream 8,Lambda表达式和doublecolon进行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表示三个标签是否都存在,也可以,这意味着有可能说每个标签中的一个标签可能为空或不存在订单项,如果存在带有除null或空白之外的其他任何值的标签,则将检查下一个标签。

最佳答案

我不确定您尝试使用Stream与原始for循环有何关系。如果要使用Stream实现循环逻辑,则需要类似以下内容:

List<String> orderItemValue =
    attributeList.stream()
                 .flatMap(att -> orderItems.stream()
                                           .map(o -> o.getInstanceCharacteristicValue(att)))
                 .filter(val -> val != null && !val.isEmpty())
                 .collect(Collectors.toList());

关于java - 我希望此代码使用Stream,Lambda表达式,双冒号运算符在Java8中编写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57447587/

10-10 16:24