Home > front end >  Adding to double Arraylist in HashMap
Adding to double Arraylist in HashMap

Time:12-31

I am trying to add pairs that add up to a certain number in java and one of the ways I am trying to attempt this is to create a double ArrayList within my HashMap. If I add 1 and 2 to my list, I will get 3 as my key. For example:

    HashMap<Integer, ArrayList<ArrayList<Integer>>> map = new HashMap<>();
    ArrayList<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    map.put(list.get(0)   list.get(1), new ArrayList<>(list));

Output looks like this

   Key: 3 Value: [[1,2]]

If I were to add one more pair

  Key: 3 Value: [[1,2],[0,3]]

but I keep getting a 'method is not applicable in the type HashMap<Integer,ArrayList<ArrayList>> is not applicable for the arguments (int, new ArrayList<>(list))'

I've also tried

    new ArrayList<>(new ArrayList<>(list))

thinking that I might need to initialize the bigger matrix first but I end up with the same error sadly.

CodePudding user response:

This line:

new ArrayList<>(list)

creates a flat ArrayList<Integer>, while the HashMap is expecting ArrayList<ArrayList<Integer>>. By the same token, new ArrayList<>(new ArrayList<>(list)) also creates a flat Integer list because you are just doing the same thing twice. See the API document for ArrayList

This is one way that would work given the 2-D list setup:

HashMap<Integer, List<List<Integer>>> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
List<List<Integer>> outer = new ArrayList<>();
outer.add(list);
map.put(list.get(0)   list.get(1), outer);

CodePudding user response:

You could also create some lambdas which may facilitate this. For example.

Map<Integer, List<List<Integer>>> map1 = new HashMap<>();

Create a function to sum the elements of a list.

Function<List<Integer>, Integer> sum =
        list -> list.stream()
        .mapToInt(Integer::intValue).sum();

Then create a BiConsumer to take the list pair and existing map and add them if need be. The computeIfAbsent, enters a value for the key if the key was null or absent. The list is returned so the the pair can be added to the newly created list.

BiConsumer<List<Integer>, Map<Integer,
           List<List<Integer>>>> addToMap =
        (pair, map) -> {
            map.computeIfAbsent(sum.apply(pair),
                    v -> new ArrayList<>()).add(pair);
        };

Putting it all together.

addToMap.accept(List.of(1,2), map1);
addToMap.accept(List.of(0,4), map1);
addToMap.accept(List.of(1,5), map1);
addToMap.accept(List.of(0,3), map1);
addToMap.accept(List.of(-1,5), map1);
addToMap.accept(List.of(-1,2,3),map1);

map1.entrySet().forEach(System.out::println);

prints

3=[[1, 2], [0, 3]]
4=[[0, 4], [-1, 5], [-1, 2, 3]]
6=[[1, 5]]

As you can see, this doesn't enforce any size limitations on the "pairs."

This may be overkill for what you want but there may be some elements you can put to use. Also note that List.of above is immutable.

  • Related