使用java流转换特定于过滤器的逻辑

z9gpfhce  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(211)

我正在尝试将工作代码转换为java流,但无法做到这一点。
我已经写了一个代码,它过滤地址id,名称,代码和指标
我写的逻辑如下
地址详细信息列表图像
从图像中
有两行具有相同的地址id。
我有优先权根据条件筛选唯一的地址id。
条件是检查地址id和行的名称是否相同。
在满足上述条件后,当代码字段为a且指示器为y时,给予高优先级。
最不重要的是检查代码字段是否为a。
创建一个Map,其中address id作为键,并检查代码字段是否为,同时迭代将该地址id添加到Map中。
稍后检查该地址id是否存在于Map中。如果是这样,则将其与地址表中的当前和上一个元素(这是一个列表)进行比较,并检查当前元素是否有a的代码和指示器y,然后将其添加到Map中。
请找到密码

class Address {

    private final String id;
    private final String name;
    private final String code;
    private final String indicator;

}

public class Test {

    public static void main(String[] args) {
        Address address0 = new Address("1", "name1", "N", "Y");
        Address address1 = new Address("1", "name1", "A", "N");
        Address address2 = new Address("2", "name1", "A", "N");
        Address address3 = new Address("2", "name1", "A", "Y");
        Address address4 = new Address("3", "name1", "C", "M");
        Address address5 = new Address("3", "name1", "A", "Y");
        List<Address> addrList = Arrays.asList(address0, address1, address2, address3, address4, address5);

        Map<String, Address> map = new HashMap<>();

        for (Address currentAddressElement : addrList) {
            String currentId = currentAddressElement.getId();
            String currentName = currentAddressElement.getName();
            String currentCode = currentAddressElement.getCode();
            String currentInd = currentAddressElement.getIndicator();
            Address previousAddressElement = map.get(currentAddressElement.getId());

            // Adding in the map if the code is A
            if (!map.containsKey(currentAddressElement.getId()) && "A".equals(currentCode))
                map.put(currentAddressElement.getId(), currentAddressElement);
            else if (previousAddressElement != null) {
                String prevId = previousAddressElement.getId();
                String prevName = previousAddressElement.getName();
                String prevCode = previousAddressElement.getCode();
                String prevInd = previousAddressElement.getIndicator();

                // adding to map if current code is A and current indicator Y  and previousCode is A and previous indicator is not Y
                if (prevId.equals(currentId)
                        && prevName.equals(currentName)
                        && "A".equals(prevCode)
                        && !"Y".equals(prevInd)
                        && "A".equals(currentCode)
                        && "Y".equals(currentInd))
                    map.put(currentId, currentAddressElement);
            }
        }

        System.out.println(new ArrayList<>(map.values()));
    }

}
yzuktlbb

yzuktlbb1#

public static void main(String[] args) {
    final BinaryOperator<Address> MERGE_BY_PRIORITY_RULE = (existed, replacement) -> {
        boolean existedHasCodeA = "A".equalsIgnoreCase(existed.getCode());
        boolean existedHasIndicatorY = "Y".equalsIgnoreCase(existed.getIndicator());
        boolean replacementHasCodeA = "A".equalsIgnoreCase(replacement.getCode());
        boolean replacementHasIndicatorY = "Y".equalsIgnoreCase(replacement.getIndicator());

        if (existedHasCodeA && existedHasIndicatorY)
            return existed;
        if (replacementHasCodeA && replacementHasIndicatorY)
            return replacement;
        if (existedHasCodeA)
            return existed;
        if (replacementHasCodeA)
            return replacement;

        return existed;
    };

    List<Address> addresses = Arrays.asList(
            new Address("1", "name1", "N", "Y"),
            new Address("1", "name1", "A", "N"),
            new Address("2", "name2", "A", "N"),
            new Address("2", "name2", "A", "Y"),
            new Address("3", "name3", "C", "M"),
            new Address("3", "name3", "A", "Y"));

    System.out.println(addresses.stream()
                                .collect(Collectors.toMap(Address::getId, Function.identity(), MERGE_BY_PRIORITY_RULE))
                                .values());
}

相关问题