1

How do I scroll a LinkedHashMap to a specific key? Something like this:

LinkedHashMap<String,String> queque = new LinkedHashMap<String,String>();
queque.put("uno","uno");
queque.put("due","due");
queque.put("tre","tre");
queque.put("quattro","quattro");
queque.put("cinque","cinque");
Iterator i = queque.entrySet().iterator();
while(i.next().getKey().equals(quattro)) {
    System.out.print(i.getKey() + ": ");
    System.out.println(i.getValue());
    i.next();
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
alexander
  • 31
  • 5
  • 1
    What's wrong about your current approach (besides that there are syntax errors and you didn't read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java yet)? – zapl Nov 27 '14 at 23:48
  • does not work,eclipse suggests to me a cast in the iterator , but how? – alexander Nov 27 '14 at 23:56
  • As per your last edit: you probably want `while(!i.next().getKey().equals(quattro))` instead because with the condition you have now you will only print entry for "quattro". – striving_coder Nov 28 '14 at 00:20

3 Answers3

2

You don't have to explicitly iterate (unless you really want to) to get the value by key: just use get() method:

System.out.println("quattro" + ": " + queque.get("quattro"));

If you want to print all the values up to the certain one, you can do the following:

Iterator i = queque.entrySet().iterator();
    while(i.hasNext()) {
        Map.Entry<String, String> me = i.next();
        if (me.getKey() == "quattro") break;
        System.out.println(me.getKey() + ": " + me.getValue());
    }

Or, a little more elegant:

for (Map.Entry<String, String> me : queque.entrySet()) {
    if (me.getKey() == "quattro") break;
    System.out.println(me.getKey() + ": " + me.getValue());
}

Couple more points:

  1. If you do not need to store the elements in the order they were added, use HashMap rather than LinkedHashMap, since former is faster. If you want store elements sorted, use TreeMap(but beware it is slower than the other types of Map).

  2. When you create instance of container in Java, you are better off using interface (like Map, List or Set) in the left part of assignment and implementation (like HashMap, ArrayList etc.) in the right part since it gives you much more flexibility: in case you later on decide to change the implementation of the same interface (e.g. use HashMap instead of LinkedHashMap as I suggested above), you only need to change one line of your code where you create this container, rather than change all places where this container is used.

striving_coder
  • 798
  • 1
  • 5
  • 7
  • I want to print the list up to a specific value , not print a single value – alexander Nov 27 '14 at 23:44
  • @alexander That is crucial information which should be part of your question. – VGR Nov 27 '14 at 23:51
  • String me = i.next(); in this line i have an error of mismatch. – alexander Nov 28 '14 at 00:05
  • @alexander: If you're satisfied with the answer, would you consider accepting it? Thanks! – striving_coder Nov 28 '14 at 00:45
  • @alexander: So can you accept it then? (Seeing you're new here, a quick explanation just in case: it's the custom here at StackOverflow to "accept" the answer which the question author has found the most useful. Such accepted answer is marked by green tick-mark and shows up at the very top, right below the answer.) Thanks! – striving_coder Nov 29 '14 at 00:04
  • Ah ok sorry I did not realize , now okay? – alexander Nov 29 '14 at 00:15
  • @alexander: Yeah, thanks! You might want to do the same thing to your other question threads too. – striving_coder Nov 29 '14 at 00:16
1

If you want to do it right with Iterator you would do

    Iterator<Entry<String, String>> i = queque.entrySet().iterator();
    while (i.hasNext()) {
        Entry<String, String> entry = i.next();
        if ("quattro".equals(entry.getKey()))
            break;
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }

But that is equivalent to using an enhanced for loop like so:

    for (Entry<String, String> entry : queque.entrySet()) {
        if ("quattro".equals(entry.getKey()))
            break;
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }

with Iterator you'll have to check hasNext(), only then call next() once(!) per loop or your iterator would advance by two elements. You should also never compare Strings with ==, that's just not working.

zapl
  • 63,179
  • 10
  • 123
  • 154
1

Sounds like you should be using TreeMap to me. Then you just use TreeMap.headMap().

user207421
  • 305,947
  • 44
  • 307
  • 483