0

Can I have a hash map in Java that looks like this?

HashMap<String, String, Integer> hmap = new HashMap<String, String, Integer>()

My question is similar to this one hereQuestion

I'm a newbie to Java. So what I want to know is, what would be the best data structure to use if I need something like above, if that is not valid?

Community
  • 1
  • 1
rowana
  • 718
  • 2
  • 8
  • 21

1 Answers1

2

Create a simple class holding two String objects:

public class MyKey {
    private String a;
    private String b;

    // ... accessors, mutators etc.
}

And then use it's objects as keys in your map:

HashMap<MyKey, Integer> hmap = new HashMap<>()

Later, to add a new entry:

hmap.put(new MyKey("a", "b"), 2);
Jezor
  • 3,253
  • 2
  • 19
  • 43
  • I need to search for one of the strings, if there is a pattern match, extract the other. How could I do that here? – rowana Sep 13 '16 at 02:08
  • @rowana I think that's not in the scope of your original question, please post a new one. But I'd probably try to iterate through map's entry set, check key part and then extract the other in the same loop. – Jezor Sep 13 '16 at 02:27
  • @Jezor but this time using map structure wont be meaningful. He can just use a list. – Alperen Üretmen Jul 17 '19 at 10:39