How to filter using Key on KeyValue objects using lambda expressions?

Given that I want to filter a list of objects with a key.

My (document) -object from the example below is as follows

{
    "attributeEntityList" : [
        {key: 'key1', value: 'somevalue1'},
        {key: 'key2', value: 'somevalue2'},
        {key: 'key3', value: 'somevalue3'}
    ]
}
  • When I pass a list of the following keys ["key1", "key2", "key3"], I expect my function to return the entire given list of attributes.

  • When I pass a list of the following keys ["key1", "key2"], I expect my function to return a list of Attributes with the specified key names.

  • When I pass a list of the following keys ["key1", "key2", "faultyKey"], I expect my function to return an empty list.

My imperative style solution looks and works fine:

private List<AttributeEntity> getAttributeEntities(List<String> keys, Document value) {
    final List<AttributeEntity> documentAttributeList = value.getAttributeEntityList();
    final List<AttributeEntity> resultList = new ArrayList<>();

    for(String configKey: keys){
        boolean keyInAttribute = false;
        for(AttributeEntity documentAttribute : documentAttributeList){
            if(configKey.equals(documentAttribute.getAttribute_key())){
                keyInAttribute = true;
                resultList.add(documentAttribute);
                break;
            }
        }
        if(!keyInAttribute){
            resultList.clear();
            break;
        }
    }

    return resultList;
}

(, , ) , , Java 8 streaming-api.


, , pre-Java8- Java8.

. , :/

. () , .

private List<AttributeEntity> getAttributeEntities(List<String> keys, Document value) {
    final List<AttributeEntity> documentAttributeList = value.getAttributeList();

    return documentAttributeList.stream()
            .filter(attribute ->
                    keys.contains(attribute.getAttribute_key())
            ).collect(Collectors.toList());
}

. , .

, ?


. , .

, .

private List<AttributeEntity> getAttributeEntities(List<String> keys, Document value) {
    final List<AttributeEntity> documentAttributeList = value.getAttributeList();

    return documentAttributeList.stream()
            .filter(attribute ->
                            keys.contains(attribute.getAttribute_key())
            )
            .collect(Collectors.collectingAndThen(Collectors.toList(), new Function<List<AttributeEntity>, List<AttributeEntity>>() {
                @Override
                public List<AttributeEntity> apply(List<AttributeEntity> o) {
                    System.out.println("in finisher code");
                    if (keys.stream().allMatch(key -> {
                        return o.stream().filter(attrbiute -> attrbiute.getAttribute_key().equals(key)).findAny().isPresent();
                    })) {
                        return o;
                    } else {
                        return new ArrayList<AttributeEntity>();
                    }
                }
            }));
}
+4
3

, , Java 8, . , .

:

public class Main {

    private static List<AttributeEntity> documentAttributeList;

    static {
        documentAttributeList = new ArrayList<>();
        documentAttributeList.add(new AttributeEntity("key1", "value1"));
        documentAttributeList.add(new AttributeEntity("key2", "value2"));
        documentAttributeList.add(new AttributeEntity("key3", "value3"));
    }

    public static void main(String[] args) {
        Main main = new Main();
        List<AttributeEntity> attributeEntities = main.getAttributeEntities(Arrays.asList("key1", "key2"));
        for (AttributeEntity attributeEntity : attributeEntities) {
            System.out.println(attributeEntity.getKey());
        }
    }

    private List<AttributeEntity> getAttributeEntities(List<String> keys) {
        if(hasInvalidKey(keys)){
            return new ArrayList<>();
        } else {
            return documentAttributeList.stream().filter(attribute -> keys.contains(attribute.getKey())).collect(toList());
        }
    }

    private boolean hasInvalidKey(List<String> keys) {
        List<String> attributeKeys = getAttributeKeys();
        return keys.stream().anyMatch(key -> !attributeKeys.contains(key));
    }

    private List<String> getAttributeKeys() {
        return documentAttributeList.stream().map(attribute -> attribute.getKey()).collect(toList());
    }

}
+3

, , ( , ):

Map<String, AttributeEntity> filteredMap=value.getAttributeEntityList().stream()
    .filter(at->keys.contains(at.getKey()))
    .collect(toMap(at->at.getKey(), at->at));

return filteredMap.keySet().containsAll(keys) 
    ? new ArrayList<>(filteredMap.values()) 
    : new ArrayList<>();

, groupingBy toMap. , , collectAndThen, , .

+1

- .

, , , .

private List<AttributeEntity> getAttributeEntities(List<String> keys, Document value) {
    final List<AttributeEntity> documentAttributeList = value.getAttributeList();

    boolean allKeysPresentInAnyAttribute = keys.stream()
            .allMatch(key ->
                    documentAttributeList.stream()
                            .filter(attrbiute ->
                                    attrbiute.getAttribute_key().equals(key)
                            )
                            .findAny()
                            .isPresent()
            );
    if (allKeysPresentInAnyAttribute) {
        return documentAttributeList.stream()
                .filter(attribute ->
                    keys.contains(attribute.getAttribute_key())
                )
                .collect(Collectors.toList());
    }
    return new ArrayList<>();
}

, .

0
source

All Articles