I was playing with Java * Stream API and I had Following code in the Lagecy system:
Iterator itr = configMap.entrySet().iterator();
List<String> resultList = new ArrayList<String>();
while(itr.hasNext()){
Entry e = (Entry)itr.next();
Config rc = (Config)e.getValue();
if (rc != null && rc.getVisibleTo() != null && (rc.getVisibleTo().equalsIgnoreCase("0010") || rc.getVisibleTo().equalsIgnoreCase("0111") || rc.getVisibleTo().equalsIgnoreCase("0011"))){
resultList .add(rc.getName());
}
}
I wrote the Stream Equivalent of above code as below:
List<String> resultList = configMap.entrySet()
.parallelStream()
.map(r -> r.getValue())
.filter(r -> r.getVisibleTo() != null)
.filter(r -> {return
r.getVisibleTo().equalsIgnoreCase("0010")
|| r.getVisibleTo().equalsIgnoreCase("0111")
|| r.getVisibleTo().equalsIgnoreCase("0011");
})
.map(r -> r.getName())
.collect(Collectors.toList());
Both the way I am getting the desired results. My question is which performance wise is better way of writing in this case? Am I actually gaining any value if I chose either one over another? The map has around 1000 values in it.