有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java8流的java筛选列表

我有一个客户对象列表,如下所示:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CheckForResource {

    public static void main(String[] args) {
        Customer john = new Customer("111", "P_1", "daily",
                "create", "table_1", "03-05-2020",
                "03-05-2020", "140");

        Customer mary = new Customer("111", "P_1", "daily",
                "delete", "table_1", "03-05-2020",
                "03-05-2020", "30");

        Customer joseph = new Customer("222", "P_2", "weekly",
                "create", "table_2", "03-05-2020",
                "03-05-2020", "50");

        Customer jason = new Customer("222", "P_2", "daily",
                "update", "table_2", "03-05-2020",
                "03-05-2020", "40");

        Customer mario = new Customer("111", "P_1", "weekly",
                "create", "table_1", "03-05-2020",
                "03-05-2020", "20");

        Customer danny = new Customer("111", "P_1", "monthly",
                "update", "table_1", "03-05-2020",
                "03-05-2020", "100");

        List<CheckForResource.Customer> customers = Arrays.asList(john, mary, joseph, jason, mario, danny);

    }


    public static class Customer {

        final String Id;
        final String pCode;
        final String usageType;
        final String operation;
        final String resource; 
        final String startTime;
        final String endTime;
        final String value;

        public Customer(String id, String pCode, String usageType, String operation,
                        String resource, String startTime, String endTime, String value) {
            Id = id;
            this.pCode = pCode;
            this.usageType = usageType;
            this.operation = operation;
            this.resource = resource;
            this.startTime = startTime;
            this.endTime = endTime;
            this.value = value;
        }
    }
}

如果列表中至少有以下各子句的1个条目,我想返回true

  1. customerId=“111”,operation=“创建”,usageType=“每日”
  2. customerId=“111”,operation=“创建”,usageType=“每月”
  3. customerId=“111”,operation=“删除”,usageType=“每日”
  4. customerId=“111”,operation=“删除”,usageType=“每月”
  5. customerId=“111”,operation=“update”,usageType=“daily”
  6. customerId=“111”,operation=“update”,usageType=“monthly”

我如何使用蒸汽来实现这一点


共 (2) 个答案

  1. # 1 楼答案

    您可以使用.anyMatch()

    Predicate<Customer> p = c -> c.getId().equals("111") &&
                            (c.getUsageType().equals("daily") || c.getUsageType().equals("monthly")) &&
                            (c.getOperation().equals("create") || c.getOperation().equals("delete") || c.getOperation().equals("update"));
    boolean result = customers.stream().filter(p)
                              .map(c -> c.getId().concat(c.getOperation()).concat(c.getUsageType()))  // Map to create hash string for detect unique
                              .distinct().count() == 6;  // distinct and check the count.
    
  2. # 2 楼答案

    试着这样做:

    • 在过滤掉id==111之后,您有六种不同的可能性
    • 因此,如果构造谓词列表,可以执行以下操作
    • 如果有六个条目,则它满足条件

    基于operationusageType创建谓词列表

    List<Predicate<String>> preds = toPredicateList(
            new String[] { "create", "delete", "update" },
            new String[] { "daily", "monthly"});
    
    

    确定是否满足条件

    boolean result = 
            customers
                .stream()
                .filter(c -> c.Id.equals("111"))
                .map(c -> c.operation + c.usageType)
                .filter(preds.stream().reduce(k -> false,
                                    Predicate::or))
                .collect(Collectors.toSet()).size() == 6;
    
    System.out.println(result);
    

    构造谓词列表

    public List<Predicate<String>> toPredicateList(String[] ops,
                String[] types) {
    
            List<Predicate<String>> list = new ArrayList<>();
            for (String o : ops) {
                for (String t : types) {
                    list.add(s -> s.equals(o + t));
                }
            }
        }
        return list;
    }