2

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.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
PreTri
  • 33
  • 6
  • 2
    Kind of hard to tell what you're doing. In the above code, `i` is always 0, since you never modify it. If you add an `i++` in the loop, then the conditions `if (i == 4 && args.length < 5)` and `if (i == 5 && args.length < 6)` will always be false. – ajb Oct 30 '15 at 05:55
  • 1
    In any case, what I _think_ you want is to process two streams in parallel, where one comes from the array `{EnumObj.FIRSTNAME`, `EnumObj.LASTNAME, ...}`, and the other comes from `args`. However, a quick search seems to indicate that Java doesn't provide a built-in way to process two streams in parallel (see http://stackoverflow.com/questions/24059837/iterate-two-java-8-streams-together). So I don't think you're going to get a good solution that uses streams. – ajb Oct 30 '15 at 05:59
  • 2
    That code looks horrible no matter which fashion it's written in. Instead of trying to convert it to a lambda stream for no good reason, you should try to fix your logic. It looks like you're trying to parse variable length data, but your solution looks incredibly brittle. – Kayaman Oct 30 '15 at 06:22
  • 1
    The first thing which came to my mind when reading the title, was, where does the requirement to use a `HashMap` come from, when it is known that the key is an `enum`. Is it because, you think every `Map` has to be a `HashMap`? What is ruling out using `EnumMap`? – Holger Oct 30 '15 at 12:00
  • Guys...I have converted my lambda to something more generic. I have mention in my comment that I have not such programming like using i and for loop since long time. I did this to put question in understand manner. Apologies if you feel brittle (which I agree it is). this is just for understanding of logic to achieve as end result. hope this explains my point of view – PreTri Oct 30 '15 at 23:10

1 Answers1

1

As already said in the comments, this code is incorrect. I'll assume that you have a variable length array of arguments, and that each argument corresponds to an enum constant of EnumObj.

If that's the case, all you need is a simple for loop:

Map<EnumObj, String> result = new HashMap<>();
EnumObj[] enums = EnumObj.values();
for (int i = 0; i < args.length; i++) {
    result.put(enums[i], args[i]);
}

You could do the same with streams, but you wouldn't gain anything, IMO:

Map<EnumObj, String> result = new HashMap<>();
EnumObj[] enums = EnumObj.values();
IntStream.range(0, args.length)
         .forEach(i -> result.put(enums[i], args[i]));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    You are assuming that the `enum`’s intrinsic order matches the order of the `args` elements. Unfortunately, there is nothing in the OP’s code/question proving it. – Holger Oct 30 '15 at 12:05
  • Yes, indeed, those are my assumptions. I'll be happy to delete my answer if the OP comes back and makes it clear that this is not what he wants. Or he can just create an array/list of enum constants in the right order, and then use the above code. – JB Nizet Oct 30 '15 at 12:14
  • I just want to confirm my understanding that I am not gaining any new thing with Lambda. My original code base is similar to your first option (obvious reason can't put exact code base due to critical information in it). but its more near to my solution and understanding. Appreciated all for your help. – PreTri Oct 30 '15 at 23:13