4

Are there any containers in Java which allows us to use iterator by key and which can go both to the next element and to the previous one (like maps in c++)?

p.s. sorry for my English

AlexMalinin
  • 77
  • 2
  • 5
  • Does C++'s `map` guarantee any iteration ordering? This will have an influence here – fge Mar 02 '14 at 15:18
  • @fge: Yes. `std::map` iterates in an order as if sorted by the key, based on a given ordering criterion (the default of which is a 'less than' comparison). – Benjamin Lindley Mar 02 '14 at 15:26
  • OK, then @nosid's answer fits the bill – fge Mar 02 '14 at 15:27
  • possible duplicate of [Java equivalent of C++ std::map?](http://stackoverflow.com/questions/2258449/java-equivalent-of-c-stdmap) – juanchopanza Mar 02 '14 at 15:29
  • 1
    @fge: Though there is also `std::unordered_map` which is hash based and has no guaranteed iteration order. (and the OP said "maps" not `std::map`, so he may also be including `std::unordered_map` in that) – Benjamin Lindley Mar 02 '14 at 15:30
  • @BenjaminLindley uhm, OK, so what is what? `map` or `unordered_map`? – fge Mar 02 '14 at 15:34
  • @fge: `std::map` is sorted, `std::unordered_map` is hashed. But I don't know what the OP means by "map". I don't know if he is using it to specifically refer to `std::map`, or if he is using it as a generalized term for a dictionary data structure, in which case it could be `std::map` or `std::unordered_map`, or even `std::multimap` or `std::unordered_multimap`. – Benjamin Lindley Mar 02 '14 at 15:38

3 Answers3

9

A NavigableMap comes closest to std::map. The most commonly used implementation is TreeMap.

The NavigableMap contains a lot of methods to iterate in sorted order over the entries. The first loop iterates over all elements, whereas the second loop is restricted to a sub-range.

NavigableMap<String, Integer> map = ...;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    // ...
}
for (Map.Entry<String, Integer> entry
   : map.subMap("bar", "foo").entrySet()) {
    // ...
}

It is also possible to iterate from the beginning to a certain entry, or from a certain entry to the end. Take a look at the following methods:

In C++, iterators are also used for a lot of other use cases. The Java iterators are very different, and can not be used in combination with mutating operations. In many cases, you have to use lookups by instead of iterations. For example, the following methods return the previous and next entry (in sorted order), if the key of the current entry is used:

nosid
  • 48,932
  • 13
  • 112
  • 139
3

You can have a look at the official documentation here

I think you can use an iterator to traverse on the map's keyset to achieve what you want. Sample code here:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Test {

public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<Integer, String>() {
        {
            put(1, "One");
            put(2, "Two");
            put(3, "Three");
            put(4, "Four");
            put(5, "Five");
        }
    };


    Iterator<Integer> iter = map.keySet().iterator();

    while(iter.hasNext()){
        System.out.println(iter.next());
    }
}
Pravat Panda
  • 1,060
  • 2
  • 13
  • 27
-1

The LinkedList class has a listIterator method which helps you to traverse in both directions

nitnamby
  • 404
  • 2
  • 8