Take the 2-minute tour ×

I have a class Group. In the class I have two fields, idGroup IdGroupGroup. Groups may be part of other groups. My class Group is defined in a HashMap<Integer,Integer>; the key is IdGroupGroup and value is idGroup. I want to search the map for a particular idGroup; can I use recursion to do this?

class Group

    int idGroupe
    String word

HashMap<Integer,Integer> GroupeGroupe = new HashMap<Integer,Integer>();
GroupeGroupe.put(idGroupeGroupe, idGroupe)
share|improve this question
Can you show the definition of the class? –  Phil May 12 '10 at 15:02
If you're mapping IdGroupGroup to idGroup and you need to search for idGroup, you're probably using the wrong data structure –  Michael Mrozek May 12 '10 at 15:03
class Group { int idGroup int idGroupGroup } –  Mercer May 12 '10 at 15:04
Example i have a Group A and Group B in the Group A i can add an another Group B –  Mercer May 12 '10 at 15:05
That's not what you posted above when you gave the definition of Group. Can you just edit your question with all relevant code (both classes, and your HashMap, etc)? –  Phil May 12 '10 at 15:10

2 Answers 2

up vote 1 down vote accepted

Given a vague question, I have to guess a lot, but perhaps it's something like this you're looking for:

import java.util.*;

public class Test {

    static Map<Integer, Integer> groups = new HashMap<Integer, Integer>();

    public static void main(String... args) {
        groups.put(1, 2);
        groups.put(2, 3);
        groups.put(2, 4);
        groups.put(4, 5);
        System.out.println(searchFor(1, 5));

    private static String searchFor(int from, int target) {

        // Target found?
        if (from == target) return "" + target;

        // Dead end?
        if (!groups.containsKey(from)) return null;

        // Recurse and try to find it from here.
        String path = searchFor(groups.get(from), target);
        return path == null ? null : from + " -> " + path;

Output: 1 -> 2 -> 4 -> 5

Or something like this:

    static Map<Integer, Group> groups = new HashMap<Integer, Group>();

    public static void main(String... args) {
        groups.put(0, new Group(1, "hello")); // (0: -)       -> (1: "hello")
        groups.put(2, new Group(9, "!"));     // (2: "world") -> (9, "!")
        groups.put(3, new Group(5, "bye"));   // (3: -)       -> (5, "bye")
        groups.put(1, new Group(2, "world")); // (1: "hello") -> (2: "world")

    private static String traverse(int from) {
        if (!groups.containsKey(from)) return "";

        String path = traverse(groups.get(from).idGroupe);
        return path == null ? null : groups.get(from).word + " " + path;

Which prints:

hello world ! 
share|improve this answer
just search for the first element (key) of yur map example search for 1 Output->1 have 2 , 2 is a Key and have 3 and stop because 3 is not a key of map –  Mercer May 12 '10 at 15:16
woho, I won the guessing game... –  aioobe May 12 '10 at 15:24

I'm not sure I understand your question, but I'll try to answer. Do I understand you correctly if you have a java HashMap with entries ? In that case you only have one entry for each idGroupGroup (java api):

   public V put(K key,V value)

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

if this is fine, and you just want to do a recursion of all element you can use:

public Collection<V> values()


have a look at: http://java.sun.com/javase/6/docs/api/java/util/HashMap.html

cheers, Jørgen

share|improve this answer

Your Answer


