繁体   English   中英

从Map中的键列表中删除元素后获取键和值对 <List<String> ,IInterface&gt;

[英]Get Keys and Value pair after removing an element from the key's list in a Map<List<String>, IInterface>

在我的用例中,有一个接口IInterface和一个List作为键和IInterface作为值的映射。

public interface IInterface{
   ....
}

public MyClass{
    Map<List<Integer>, IInterface> interfaceByStringList;
    MyClass(){
        interfaceByStringList = new HashMap<List<Integer>,IInterface>();
    }

    //Method to remove one element from key list
    public Map<List<Integer>, IInterface> myMethod(IntegerelementToRemove){
        ................
    }
}

在上面的场景中,我必须从Map的keySet()中存在的键列表中删除“elementToRemove”。 然后我必须返回更新的地图。

例如,Map的KeySet是这样的:

{[1,2,3],[4,7,5],[67],[23,41]}
and corresponding values: 
{IInterface1, IInterface2,IInterface3,IInterface4]

假设我想要删除4然后我更新的地图返回:更新的Map KeySet:

{[1,2,3],[7,5],[67],[23,41]}
and corresponding values: 
{IInterface1, IInterface2,IInterface3,IInterface4]

接受:

interfaceByStringList
     .entrySet()
     .removeIf(entry -> entry.getKey().contains(elementToRemove));

这对你有用。

public Map<List<Integer>, IInterface> myMethod(int integerElementToRemove){
    interfaceByStringList
            .keySet()
            .stream()
            .filter(integers -> integers.contains(integerElementToRemove))
            .forEach(integers -> integers.remove(Integer.valueOf(integerElementToRemove)));
    return interfaceByStringList;
}

暂无
暂无

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

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