3

I'm very new to Java and I am trying to group by objects based on the number but I'm unable to make it. Here is the example:

SomeCollection<Integer,String> t=new SomeCollection<Integer,String>();
t.put("1","a");
t.put("1","b");
t.put("2","c");

output:
1 - a,b
2 - c

Basically, when numbers are same then value needs to be grouped under that same number. It's all about asking how to perform this kind of strategical output to achieve by using any collections. Any help is appreciated.

7
  • 1
    use a SomeCollectiona<Integer, List<String>> instead, and add the values to the list. Commented Dec 2, 2015 at 7:54
  • 1
    Can i use TreeMap to achieve this TreeMap<Integer,List<String>>? Commented Dec 2, 2015 at 7:56
  • yeah, you just need to check if the specific integer has a list yet, if not initialize it and then add the value to the list. Commented Dec 2, 2015 at 7:57
  • i have retrieved the keys and values. Output : keys: 1,2 and values a,b,c. But how to relate which belongs to which? Sorry to ask you more questions. ' for(Map.Entry<Integer,ArrayList<String>> entry:tm.entrySet()){ Integer i= entry.getKey(); System.out.println(i); ArrayList<String> s= entry.getValue(); System.out.println(s); }' Commented Dec 2, 2015 at 8:07
  • This is also a map, not a collection. Maps map a key to a value. You want to map an Integer to a collection of Strings. TreeMap<Integer,List<String>> is a sensible idea! Commented Dec 2, 2015 at 8:09

3 Answers 3

3

As suggested by others, you can use a Map<Integer, List<Object>> if you want to stick only to JDK collections.

However, there are Multi Value Maps out there which will do all the work for you for free. Check out this question what java collection that provides multiple values for the same key (see the listing here https://stackoverflow.com/a/22234446/3114959 in particular).

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

Comments

1
    Map<String, Integer> map = new HashMap<>();
    map.put("a", 1);
    map.put("b", 1);
    map.put("c", 2);
    map.put("d", 1);
    map.put("e", 3);
    map.put("f", 3);
    map.put("g", 3);

    //Using Java 7
    Set<Integer> set = new HashSet<Integer>();
    Map<Integer, List<String>> finalList = new HashMap<Integer, List<String>>();
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        set.add(entry.getValue());
        finalList.put(entry.getValue(), new ArrayList<String>());
    }
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        for (Integer value : set) {
            if (value.equals(entry.getValue())) {
                List<String> values = finalList.get(value);
                values.add(entry.getKey());
            }
        }
    }
    System.out.println("Result : " + finalList.toString());

1 Comment

Do you think, grouping the way you are doing is effective??
0

There is even a construct which will help you do this.

Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 1);
map.put("c", 2);

Map<Integer, List<String>> groupedMap = map.keySet().stream()
        .collect(Collectors.groupingBy(map::get));

Comments

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.