4

I have a small question about converting 2d arraylist to hashmap in java. I have a dataset looks like this after reading as 2d arraylist:

0 1

0 2

1 2

1 3

Which first column stands for id and second column stands for the item. I would like to create frequent itemset using hashmap in java, the output should look like

1 0

2 0 1

3 1

I use these codes but I have some trouble on them:

HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
for(Integer elem : data){
        map.put(elem[1], elem[0]);
}

Where data is my 2d arraylist.

The error message said that

incompatible types: ArrayList<Integer> cannot be converted to Integer
    for(Integer elem : data){
                       ^

Any help will be appreciated!

2 Answers 2

1

You go like this:

List<List<Integer>> inputData = ...

Map<Integer, List<Integer>> dataAsMap = new HashMap<>();
for(List<Integer> row : data){
  Integer id = row.get(0);
  Integer item = row.get(1);
  List<Integer> rowInMap = dataAsMap.get(item);
  if (rowInMap == null) {
    rowInMap = new ArrayList<>(); 
    dataAsMap.put(item, rowInMap);
  }
  rowInMap.add(id);
}

Some notes:

  1. You should only be using the interface types List, Map ... as types (you only specify the specific impl type such as HashMap when creating new objects!)
  2. Your problem is: when using for-each on List of Lists (as you did) ... you don't get the individual "cells" Instead, you get Lists [ iterating lists of lists ... results in one list per iteration!]

So, what is left then is to fetch the elements of that inner List, and push them into the Map. The one other part to pay attention to: you want to create a Map of List objects. And those List objects need to be created as well!

( I didn't run the above through a compiler, so beware of typos, but in general it should be telling you what you need to know. If you don't get what the code is doing, I suggest adding println statements or running it in a debugger)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I really appreciate it!
0

Here is one easy way of doing it:

  1. Take Map<Integer, List<Integer>>
  2. Iterate your Arraylist.
  3. See if key is already present in the map. Retrieve the list and add the value to the list if key is present or else create a new list with the value.

Program:

class FooBar {
    public static void main (String[] args) throws Exception {
        int[][] data = {{0,1}, {0,2}, {1,2}, {1,3}};
        Map<Integer, List<Integer>> myMap = new HashMap<>();

        for(int i = 0; i < 4; i++) {
            List<Integer> values = myMap.containsKey(data[i][0]) ?
                                   myMap.get(data[i][0]) : new ArrayList<>();
            values.add(data[i][1]);
            myMap.put(data[i][0], values);
        }

        System.out.println(myMap);
    }
}

Output:

{0=[1, 2], 1=[2, 3]}

This is just to illustrate the basic method. You can obviously modify it to suit your needs. For example, you can have String instead of List<Integer> and choose to append the values to the String instead of adding it to the List.

EDIT:

Here is a sample program with List<List<Integer>> as input. Here I'm assuming the name of this list as input.

Program:

class FooBar {
    public static void main (String[] args) throws Exception {
        /* Input Data */
        List<List<Integer>> input = new ArrayList<>();
        input.add(new ArrayList<Integer>(){{add(0); add(1);}});
        input.add(new ArrayList<Integer>(){{add(0); add(2);}});
        input.add(new ArrayList<Integer>(){{add(1); add(2);}});
        input.add(new ArrayList<Integer>(){{add(1); add(3);}});

        Map<Integer, List<Integer>> myMap = new HashMap<>();
        for(int i = 0; i < input.size(); i++) {
            List<Integer> values = myMap.containsKey(input.get(i).get(0)) ?
                                   myMap.get(input.get(i).get(0)) : new ArrayList<>();
            values.add(input.get(i).get(1));
            myMap.put(input.get(i).get(0), values);
        }

        System.out.println(myMap);
    }
}

Output:

{0=[1, 2], 1=[2, 3]}

3 Comments

Thanks a lot! But what if the input data is arraylist?
@lixx3013 If it is an ArrayList then you'll have to iterate it instead of int[][]. Have a look at the revised answer.
@lixx3013 Please accept the answer if you think this resolved your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.