java8 Stream API过滤器基于arrayList的Map

时间:2018-05-11 20:28:41

标签: java

Class Person {
     Map<String, String> personNature = new HashMap<String,String>();
     // Getter and Setter of Map

     public void setBehaviour(String key, String value) {
        personNature.put(key, value);
     }
}

main(String[] abc) { 

    Person p = new Person();
    Map<String, String> personAllNature = new HashMap<String, String>();
    allNatures.put("personName", "Raj");
    allNatures.put("personAggresive", "8");
    allNatures.put("personShyness", "5");
    allNatures.put("personCourage", "9");
    allNature.put("personFakness", "2");


    List<String> personVisibleAttributes= new ArrayList<String>();
    personVisibleAttributes.add("personAggresive");
    personVisibleAttributes.add("personShyness");

} 

现在我想以这样一种方式过滤Map(personAllNature),当我想设置Person's Map时,我只会获得List中的那些值(personVisibleAttributes)

如下所示:

personAllNature.entrySet().parallelStream()
        .filter(map -> personVisibleAttributes.contains(map.getKey()))
        .forEach(entry -> p.setBehaviour( entry.getKey(), entrygetValue())

总体而言:

根据List中的值过滤Map 映射键位于List中 然后在Person实例中设置所有这些映射值。

1 个答案:

答案 0 :(得分:0)

因为您只需添加from __future__ import print_function import numpy as np from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.rank p=1 gn_forecast =np.empty((4*p,1), dtype=np.float64) if (rank == 0): gn_forecast = np.ones((4*p,1), dtype=np.float64)*5.0 print("Rank: ", rank, ". gn_forecast is:\n", gn_forecast) comm.Bcast( [gn_forecast, MPI.DOUBLE] ,root=0) print("Rank: ", rank, ". gn_forecast is:\n", gn_forecast) attributes,您可以直接从中进行迭代:

List