Casey B.
Casey B.

Reputation: 279

Seeking Optimization Suggestions for Map-to-Map Conversion

I'm seeking feedback as to whether there's a more efficient approach than what I'm doing in my code shown at the bottom.

Basically, given this map:

        Set<String> A_Set = new HashSet<>(Arrays.asList("1111", "2222", "5555"));
        Set<String> B_Set = new HashSet<>(Arrays.asList("3333", "4444"));
        Set<String> C_Set = new HashSet<>(Arrays.asList("6666"));
        Set<String> D_Set = new HashSet<>(Arrays.asList("2222", "5555", "6666"));

        Map<String, Set<String>> values = new HashMap<>();
        values.put("A", A_Set);
        values.put("B", B_Set);
        values.put("C", C_Set);
        values.put("D", D_Set);

which looks like this:

enter image description here

How do I create a Map<String, List<Boolean> map such that it looks like this:

enter image description here

In the most efficient way possible. My real Map has thousands of values per Set, but there are only ever 4 Sets (A, B, C, D).

Here's my current code. Can you think of a more efficient approach?

import java.util.*;

public class MapToMap {

    public static void main(String[] args) {
        Set<String> A_Set = new HashSet<>(Arrays.asList("1111", "2222", "5555"));
        Set<String> B_Set = new HashSet<>(Arrays.asList("3333", "4444"));
        Set<String> C_Set = new HashSet<>(Arrays.asList("6666"));
        Set<String> D_Set = new HashSet<>(Arrays.asList("2222", "5555", "6666"));

        Map<String, Set<String>> values = new HashMap<>();
        values.put("A", A_Set);
        values.put("B", B_Set);
        values.put("C", C_Set);
        values.put("D", D_Set);

        Map<String, List<Boolean>> exists = new HashMap<>();

        for (Map.Entry<String, Set<String>> v : values.entrySet()) {
            for (String val : v.getValue()) {
                if (exists.containsKey(val)) {
                    List<Boolean> list = exists.get(val);
                    list = addValue(v.getKey(), list);
                    exists.put(val, list);
                } else {
                    List<Boolean> newList = new ArrayList<>(Arrays.asList(false, false, false, false));
                    newList = addValue(v.getKey(), newList);
                    exists.put(val, newList);
                }
            }
        }
        for (Map.Entry<String, List<Boolean>> s : exists.entrySet()) {
            System.out.println(s);
        }
    }

    private static List<Boolean> addValue(String key, List<Boolean> listToUse) {
        List<Boolean> newList = new ArrayList<>();
        if (Objects.equals("A", key)) {
            newList.addAll(Arrays.asList(true, listToUse.get(1), listToUse.get(2), listToUse.get(3)));
        } else if (Objects.equals("B", key)) {
            newList.addAll(Arrays.asList(listToUse.get(0), true, listToUse.get(2), listToUse.get(3)));
        } else if (Objects.equals("C", key)) {
            newList.addAll(Arrays.asList(listToUse.get(0), listToUse.get(1), true, listToUse.get(3)));
        } else if (Objects.equals("D", key)) {
            newList.addAll(Arrays.asList(listToUse.get(0), listToUse.get(1), listToUse.get(2), true));
        }
        return newList;
    }
}

Upvotes: 0

Views: 50

Answers (1)

shmosel
shmosel

Reputation: 50726

Here's a solution using streams:

Map<String, List<Boolean>> exists = values.values()
        .stream()
        .flatMap(Set::stream)
        .distinct()
        .collect(Collectors.toMap(v -> v, v -> Stream.of("A", "B", "C", "D")
                .map(k -> values.get(k).contains(v))
                .collect(Collectors.toList())));

Ideone Demo

Upvotes: 1

Related Questions