HashMap<String, Integer> Search for part of an key?

58,933

Solution 1

Iterate is your only option unless you create a custom data structure:

for (Entry<String, Integer> e : map.entrySet()) {
    if (e.getKey().startsWith("xxxx")) {
        //add to my result list
    }
}

If you need something more time efficient then you'd need an implementation of map where you are tracking these partial keys.

Solution 2

It seems like a use case for TreeMap rather than HashMap. The difference is that TreeMap preserves order. So you can find your partial match much quicker. You don't have to go through the whole map.

Check this question Partial search in HashMap

Solution 3

You cannot do this via HashMap, you should write your own implementation for Map for implementing string length based searching in a map.

Solution 4

Map<String, Integer> result = new HashMap<String, Integer>;
for(String key : yourMap.keySet()) {
    if(key.length() == 4){
        result.put(key, yourMap.get(key);
    }
}

After executing this code you have all key/value pairs with 4 letter keys in result.

Solution 5

Set<Entry<String, Integer>> s1 = map.entrySet();
    for (Entry<String, Integer> entry : s1) {
          if(entry.getKey().length == 4)
          //add it to a map;
}

First get the entry set to your hashmap. Iterate through the set and check the length of each key and add it to a map or use it as u want it.

Share:
58,933

Related videos on Youtube

VoidStar
Author by

VoidStar

Updated on December 03, 2020

Comments

  • VoidStar
    VoidStar over 3 years

    I am currently using HashMap<String, Integer> which is filled with keys of type String which are all, let's say, 5 chars long. How can I search for an specific key of 4 chars or less, which is part and at the beginning of some other keys and get all hits as a collection of <Key, Value>?

    • Javier Diaz
      Javier Diaz almost 11 years
      You have to iterate through all the HashMap, use getKey.length() and add or not (depending on the condition) to a List<>
    • vikingsteve
      vikingsteve almost 11 years
      Have you looked at keySet() ?
    • Juned Ahsan
      Juned Ahsan almost 11 years
      Is it too tough to solve, if yes for you, then at least share what have u tried so far?
    • Thomas Jungblut
      Thomas Jungblut almost 11 years
      What you are looking for is called a Trie.
    • Menelaos
      Menelaos almost 11 years
      Question: he is looking for entries that have 4 characters instead of 5 characters? Or he is looking for a series of 4 characters which may be within 5 characters?
    • VoidStar
      VoidStar almost 11 years
      I edited my question. It should be clear now
    • Menelaos
      Menelaos almost 11 years
      @Machtl Is the 4 characters at the beginning or end of string)? Can they be anywhere in the string?
  • Menelaos
    Menelaos almost 11 years
    Unless ofcourse, he changes the requirements, uses the key with only the 4 characters, and stores the other data in an object in value. Another option is to use a custom Key Object, with custom hashCode() method.
  • Michael Aaron Safyan
    Michael Aaron Safyan almost 11 years
    I wouldn't advise rolling one's own Map<>. That's probably overkill (and, besides, composition is often better than inheritance). However, I agree that the data structure, as-is, is not designed to do this.
  • cyborg
    cyborg almost 11 years
    You don't need to implement all of Map. It is pretty easy to add extra stuff if you use the decorator pattern. So for example I want to log out every key added to a map? Fine, I make my LoggingMap implement Map and then you have to supply the actual instance of the Map it's going to log out. That would work perfectly well in this case except instead of logging you'd keep track of the keys you want in a list or something along those lines.
  • harsh
    harsh almost 11 years
    Yes right Map implementation can be extension (extends) of HashMap or other exiting implementations, override only what is required. My point was for this question Map seems to be a right data-structure but with some modified get (for length based retrieval) and put (for optimal get say O(1) again). As all other answers shows external logic as a solution, having a specific Map would enforce encapsulating that logic inside map itself.