0

HashMap has only one key and Value

Map<String, String> hm = new HashMap<>();
hm.put("one","ele");

Now how to get key and value from this and assign to string?

LaurentG
  • 11,128
  • 9
  • 51
  • 66
pals
  • 5
  • 4
  • 5
    Why do you use `HashMap` if you have one key and value? – spoko Dec 08 '14 at 08:59
  • By the way, don't you find it adorable that this... no offense OP, but mediocre question gets seven similar answers while much more complex and overall better ones get one or none? – spoko Dec 08 '14 at 09:18
  • @spoko It also depends on the time it takes for an answer. And why mention that over here? – Emz Dec 08 '14 at 09:23
  • @Emz just my observation and I'm sure OP doesn't mind as he's already got his answer anyway. It feels like "reputation hunting", and not really "good question, I want to help you and the whole community by answering it" approach. – spoko Dec 08 '14 at 09:28
  • @spoko "reputation hunting", as the site is built a higher reputation let's you do more good. "I can't help person X, I can at least format his code and correct his spelling now." for example. That wasn't doable for me before. – Emz Dec 08 '14 at 09:37

6 Answers6

2

Just iterate through them

for (Entry<String, String> entry : hm.entrySet())
        {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + ":" + value);
        }

And you dont need to use HashMap also, as you have only 1 key value pair.

USB
  • 6,019
  • 15
  • 62
  • 93
1

Just create a simple classe to hold your data:

public class Pair {

private String key;
private String value;

public Pair( String k, String v ) {
   key = k
   value = v;
}

public String getKey() {
    return key;
}

public String getValue() {
    return value;
}



}

And the you just need this to use it:

Pair pair  = new Pair( "one", "ele );
StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • If the `Map<>` only has one value then you should not really use a `Map<>` but instead create a container, like this. Most elegant solution by far. Kudos to @StephaneM. I'd make use of `this` in the constructor though. And indent the class code correctly! – Emz Dec 08 '14 at 09:22
  • Sometimes I use this approach, the problem here is that you need to be very sure this will not evolve through your application, because by the time evolving you will be reinventing the wheel over and over again, writting dependant classes that holds a pair, then a collection of pairs, then you'd not be sure whether it has one pair ot a collection, then you will invent your own collection class ... :D – Royal Bg Dec 08 '14 at 09:24
1
if (map.entrySet().size() == 1) {
    Entry<String, String> next = map.entrySet().iterator().next();
}
AdrianS
  • 1,980
  • 7
  • 33
  • 51
0

Need to use Iterator on HashMap

public static void printMap(Map hm) {
     String key = "";
     String value = "";
    for (Entry<String, String> entry : hm.entrySet())
    {
        key = entry.getKey();
        value = entry.getValue();
        System.out.println(key + ":" + value);
    }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

You need to iterate over map value. Since map can not provide you the indexed access to the values.

Map<String, String> hm = new HashMap<String, String>();
hm.put("one","ele");

Set<Entry<String, String>> entrySet = hm.entrySet();
for (Entry<String, String> entry : entrySet)
{
    System.out.println(entry.getKey()+ " " + entry.getValue());
}

or if you have key then you can use

String value = hm.get(key);

it will return you value object.

eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
-1
Map.Entry<String, String> entry = hm.entrySet().iterator().next();
String result = entry.getKey() + " = " + entry.getValue()
iozee
  • 1,339
  • 1
  • 12
  • 24