Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you search through a map?

Tags:

java

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);
        }

     }
like image 311
Bramble Avatar asked Dec 23 '22 03:12

Bramble


1 Answers

Not quite sure if I understand you, but I guess you are looking for containsKey?

ht.containsKey("5");
like image 146
RoToRa Avatar answered Jan 05 '23 02:01

RoToRa