简体   繁体   English

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

[英]Converting a Filter specific Logic with usage of Java streams

I am trying to convert the working code to java streams but not able to do so.我正在尝试将工作代码转换为 java 流,但无法这样做。

I have written a code where it filters Address id, name , Code, and indicator我写了一个代码,它过滤地址 id、名称、代码和指标

The logic which i wrote is as follows我写的逻辑如下

address details list image地址详细信息列表图像

from the image从图像

There are two rows with same address id .有两行具有相同的地址 id 。

I have priority that to filter unique address id based on condition.我优先考虑根据条件过滤唯一地址 ID。

  1. The condition is to check if address id and names of the rows are same.条件是检查地址id和行名称是否相同。
  2. After the above condition is met then high priority is given when code field is A and indicator is Y.满足上述条件后,当代码字段为 A 且指示符为 Y 时,给予高优先级。
  3. The least priority is to check atleast the Code field is A.最低优先级是至少检查代码字段为 A。

A Map is created where address id as the key and checking if code field is A while iterating that address id is added that to map.创建一个映射,其中地址 id 作为键并检查代码字段是否为 A,同时迭代地址 id 将其添加到映射中。

later checking if that address id exists in the map .稍后检查该地址 id 是否存在于地图中。 If so then compare it with current and previous element in the address table which is a list and checking if current has Code of A and indicator as Y then add it to the map .如果是,则将其与地址表中的当前和前一个元素进行比较,地址表是一个列表,并检查当前是否具有代码 A 和指示符为 Y,然后将其添加到地图中。

Please find the code请找到代码

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()));
    }

}
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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM