I am trying to achieve below logic using Lambda-Stream in Java 8. (See below sample method). I put down below logic in old fashion so that it will be easy to create solution instead of my version of Lambda - Stream setup which is more complex and wrong as not getting end result.
public static HashMap<EnumObj,String> getHashMapData(String…args){
int i = 0;
HashMap<EnumObj,String> hashObj = new HashMap<EnumObj,String>();
if(args.length <= 6){
for(String arg : args){
if(i == 0){
hashObj.put(EnumObj.FIRSTNAME,args[i]);
}else if(i == 1){
hashObj.put(EnumObj.LASTNAME,args[i]);
}else if{……….
………….
}else if(i == 4 && args.length < 5){
hashObj.put(EnumObj.COMPANY,args[i]);
hashObj.put(EnumObj.COMANYBOSS,args[i+1]);
}else if(i == 5 && args.length < 6){
hashObj.put(EnumObj.COMANYBOSS,args[i]);
}
}
}
}
Please ignore any lopping logic of if / else if as time ago lost touch of such looping logic. While I am keep trying on my logic thought Got to get some idea if someone else has done successfully or have suggestion to achieve it with simplest manner.
So far I use two Stream to achieve with Collections.toMap option for creating new map. But it has Stream with in a stream and more complex.
If I found solution by my self then will post that answer otherwise will vote for best solution.
iis always 0, since you never modify it. If you add ani++in the loop, then the conditionsif (i == 4 && args.length < 5)andif (i == 5 && args.length < 6)will always be false.{EnumObj.FIRSTNAME,EnumObj.LASTNAME, ...}, and the other comes fromargs. However, a quick search seems to indicate that Java doesn't provide a built-in way to process two streams in parallel (see stackoverflow.com/questions/24059837/…). So I don't think you're going to get a good solution that uses streams.HashMapcome from, when it is known that the key is anenum. Is it because, you think everyMaphas to be aHashMap? What is ruling out usingEnumMap?