Home » Java » java – add a SortedSet directly to a Map-Exceptionshub

java – add a SortedSet directly to a Map-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

i want to add a SortedSet directly into a map.

my Map looks as follows:

Map<Integer, SortedSet<String>> categorizedResult = new HashMap<>();

currently i’m adding the SortedSet as follows:

SortedSet<String> sSOne = new TreeSet<>();
SortedSet<String> sSTwo = new TreeSet<>();

sSOne.add("Some String in Set one"); 
sSTwo.add("Some String in Set two"); 


categorizedResult.put(1, sSOne); 
categorizedResult.put(2, sSTwo); 

// return Map or something...

Now imagine a Map with atleast 10 SortedSets. Obviously the process of adding all of these 10 SortedSets gets confusing and annoying quite fast.
Is there a way to directly add the SortedSet into the Map without the more or less redundant SortedSets(sSOne, sSTwo)? Overwriting the previous value is not desired. The above code is executed in a loop.
Im thinking of something like this:

Map<Integer, SortedSet<String>> categorizedResult = new HashMap<>();

categorizedResult.put(1, addDirectlyToMap("Some String in Set one"); 
categorizedResult.put(2, addDirectlyToMap("Some String in Set two"); 

Thanks in advance

How to&Answers:

You can write something like this:

categorizedResult.put(1, new TreeSet<String>(Set.of("Some String in Set one"))); 

Or, if you’re using an older Java version that doesn’t have Set.of():

categorizedResult.put(1, new TreeSet<String>(Arrays.asList("Some String in Set one"))); 

To avoid overwriting an existing value, use computeIfAbsent():

categorizedResult.computeIfAbsent(1, x -> new TreeSet<String>()).add("Some String in Set one"); 
categorizedResult.computeIfAbsent(1, x -> new TreeSet<String>()).add("Another String in Set one"); 

This will result in a Map having a single key (1) mapped to a SortedSet having two elements.

Answer:

You need to check if there is an existing Set already present.

categorizedResult.put(1, new TreeSet<String>() {{
  addAll(categorizedResult.getOrDefault(1, new TreeSet<>()));
  add("some value");
}});

Or more at a time

categorizedResult.put(1, new TreeSet<String>() {{
  addAll(categorizedResult.getOrDefault(1, new TreeSet<>()));
  add("some value 1");
  add("some value 2");
}});

My initial response which is NOT working as a one liner:

categorizedResult.put(1, categorizedResult.getOrDefault(1, new TreeSet<>()).add("some value"))

But I recommend @Eran’s solution:

categorizedResult.computeIfAbsent(1, x -> new TreeSet<String>()).add("Some String in Set one"); 

Answer:

Another option would be to create a Facade like structure for your Map e.g.

public class SortedMapFacade extends HashMap<Integer, SortedSet> {


    public SortedSet<String> put(Integer key, String value) {
        SortedSet sortedSet = get(key);
        if (sortedSet == null) {
            sortedSet = new TreeSet();
        }
        sortedSet.add(value);
        return put(key, sortedSet);
    }

}

This will provide you with an extra helper method for your Map and can be used as follows

SortedMapFacade mapFacade = new SortedMapFacade();
mapFacade.put(1, "A value");
mapFacade.put(1, "Another value");
mapFacade.put(2, "Yet another value");