How do you search through a map?

Posted by Jack Null on Stack Overflow See other posts from Stack Overflow or by Jack Null
Published on 2010-04-23T15:20:17Z Indexed on 2010/04/23 15:23 UTC
Read the original article Hit count: 189

Filed under:

I have a map:

Map<String, String> ht = new HashMap();

and I would like to know how to search through it and find anything matching a particular string. And if it is a match store it into an arraylist. The map contains strings like this:

1,2,3,4,5,5,5

and the matching string would be 5.

So for I have this:

  String match = "5";
  ArrayList<String> result = new ArrayList<String>();

 Enumeration num= ht.keys();
     while (num.hasMoreElements()) {
        String number = (String) num.nextElement();

        if(number.equals(match))
        {
           result.add(number);
        }

     }

© Stack Overflow or respective owner

Related posts about java