-2

I have a function which returns an array list in the following way

[java.lang.String=name, int=parameters, byte=paramOne, java.lang.String=format, byte=paramTwo]

and is stored in a variable like : List<Entry<String, String>> dataTypes

Now I want to make a map that stores the key-value pairs like this list. I have tried to make separate lists of keys and values and I'm trying to use a multimap to map it against each other but it does not work. (I know hashmaps can't store duplicate keys and will just take the last duplicate key with its respective value, that's why I'm trying to use multimaps) This is the code I'm trying:

        List<String> dataTypeValues = new ArrayList<>();
        List<String> dataTypeKeysList = new ArrayList<>();
        String keyString = null;
        String valueString = null;
                            
        for(Entry<String, String> dt : dataTypes) {
            dataTypeKeys.add(dt.getKey());
            keyString = dataTypeKeys.toString();
            
        }
        for(Entry<String, String> dt : dataTypes) {
            dataTypeValues.add(dt.getValue());
            valueString = dataTypeValues.toString();
        }
        Multimap<String, String> multiMap = ArrayListMultimap.create();         
        multiMap.put(keyString, valueString);
        System.out.println(multiMap);
        

And this is the output I'm getting:

{[java.lang.String, int, byte, java.lang.String, byte]=[[name, parameters, paramOne, format, paramTwo]]}    

And this is the output I'm trying to get:

{java.lang.String=[name], int=[parameters], byte=[paramOne,paramTwo], java.lang.String=[format]}

Any advice would be really helpful. Thanks in advance!

EDIT : The answers provided by @User - Upvote don't say Thanks and @Louis Wasserman works perfectly. I accepted the answer from @User - Upvote don't say Thanks because of the discussion. If I could I would accept both these answers. Thank you

R2D2
  • 23
  • 1
  • 7
  • Hey, based on your activity, it seems you've a habit of not accepting an answer to the questions you ask. People *voluntarily* help others here, the least you can do is show some appreciation by accepting an answer. – Abhijit Sarkar Aug 07 '20 at 19:01
  • Your current data structure is perfect for your expected output I think `List` – Eklavya Aug 07 '20 at 19:12
  • Could you please elaborate? @User-Upvotedon'tsayThanks – R2D2 Aug 07 '20 at 19:13
  • For the accepted answer you can't get the expected output you mention in question. Your current data structure is perfect for store duplicate key data with value. I don't understand why you want to use multimap – Eklavya Aug 07 '20 at 19:15
  • Yes the existing DS stores it perfectly but it comes from the function which returns an ArrayList and has to be stored in that particular way. I need to use a proper map so that I can access functions like keySet() or values(). In the current scenario, I can not do so. @User-Upvotedon'tsayThanks – R2D2 Aug 07 '20 at 19:20
  • Then how can you know for `byte` which value should return `paramOne` or `paramTwo` from map? and you can get keys and values in one-liner using steam API from ArrayList – Eklavya Aug 07 '20 at 19:23
  • `byte` should have both the values so that later I can check if it is mapped to one of these values or both. I was trying to make the map as: `Multimap> multiMap = ArrayListMultimap.create(); ` so that a key can have a list of values. @User-Upvotedon'tsayThanks – R2D2 Aug 07 '20 at 19:27
  • Then update your question's expected output so. – Eklavya Aug 07 '20 at 19:29

3 Answers3

2

You can use groupingBy which is the ideal case for you I think

Map<String, List<String>> res =
                dataTypes.stream().collect(Collectors.groupingBy(Map.Entry::getKey,
                             Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Eklavya
  • 17,618
  • 4
  • 28
  • 57
1

take the last duplicate key

That is the exact purpose of Collectors.toMap.

Map<String, list<String> multiMap = dataTypes.stream.collect(toMap(
  Map.Entry::getKey,
  e -> singletonList(e.getValue()),
  (v1, v2) -> {
    List<String> values = new ArrayList<>();
    values.addAll(v1);
    values.addAll(v2);

    return values;
  }
))
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
1

It's not clear to me why you're trying to put the entries into the multimap like that. At minimum, what you want is

for (Entry<String, String> dt : dataTypes) {
  multiMap.put(dt.getKey(), dt.getValue());
}

Then, you probably want to print multimap.entries(). And if you care about the order of the entries, you probably want LinkedListMultimap.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413