17

I've a dictionary in java:

protected Dictionary<String, Object> objects;

Now I want to get the keys of the dictionary, so that I can get the value of the key with get() in a for loop:

for (final String key : this.objects) {
    final Object value = this.objects.get(key);

But this doesn't work. :( Any idea?

Thx Thomas

PS: I need the key & the value both in a variable.

Thomas Flynn
  • 361
  • 2
  • 4
  • 12
  • 1
    The same question is answered in the following link :http://stackoverflow.com/questions/9371667/foreach-loop-in-java-for-dictionary – vinay Aug 16 '13 at 19:09
  • 1
    For starters, avoid using `Dictionary`; it's a legacy class. Use `HashMap` instead. – chrylis -cautiouslyoptimistic- Aug 16 '13 at 19:20
  • The difference in performance between HashMap and Dictionary is relevant for C#, NOT for Java. They're completely different languages, in Java we've been using HashMap for over 15 years, and that's recommended over Dictionary. Dictionary doesn't even implement the Map interface, it's way obsolete. – Óscar López Aug 16 '13 at 19:22

4 Answers4

42

First things first. The Dictionary class is way, way obsolete. You should be using a Map instead:

protected Map<String, Object> objects = new HashMap<String, Object>();

Once that's fixed, I think this is what you meant:

for (String key : objects.keySet()) {
    // use the key here
}

If you intend to iterate over both keys and values, it's better to do this:

for (Map.Entry<String, Object> entry : objects.entrySet()) {
    String key = entry.getKey();
    Object val = entry.getValue();
}
Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    He's not using a `Map` but a `Dictionary`... http://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html#keys%28%29... It may or may not be better to use a `Map` depending on what he's trying to do but it's an important distinction – StormeHawke Aug 16 '13 at 19:11
  • @StormeHawke yep, I just realized that. I updated my answer accordingly – Óscar López Aug 16 '13 at 19:12
  • But a Map is quit slow, if you are using a lot of objects (100,000 or more) – Thomas Flynn Aug 16 '13 at 19:12
  • 3
    @ThomasFlynn where did you hear such a thing? that's not correct. `Dictionary` is so obsolete that it uses `Enumeration` instead of `Iterator` for iterating over its entries. People have been using classes implementing `Map` for more than a decade without complaining of bad performance. And besides, 100.000 objects is a very small number of objects – Óscar López Aug 16 '13 at 19:14
  • So what should I use? – Thomas Flynn Aug 16 '13 at 19:15
  • 3
    @ThomasFlynn that link is for C# !!!, this question is about Java, the referenced data structures are not comparable, _at all_. Use `Map` as I suggested, that's the way to go in Java – Óscar López Aug 16 '13 at 19:17
  • @ThomasFlynn without question use `Map`, and its main implementor, `HashMap`. If you ever need to preserve the order in which the contents are added, use `LinkedHashMap`. Unless you're writing multithreaded programs, that is all you'll ever need to store data this way – StormeHawke Aug 16 '13 at 19:24
9

If you have to use a dictionary (for example osgi felix framework ManagedService) then the following works..

public void updated(Dictionary<String, ?> dictionary) 
    throws ConfigurationException {

    if(dictionary == null) {
        System.out.println("dict is null");
    } else {
        Enumeration<String> e = dictionary.keys();
        while(e.hasMoreElements()) {
            String k = e.nextElement();
            System.out.println(k + ": " + dictionary.get(k));
        }
    }
}
Tim Shockley
  • 91
  • 2
  • 2
2

java.util.Map is the Dictionary equvivalent and below is an example on how you can iterate through each entry

Map<String, Object> map = new HashMap<String, Object>();
//...

for ( String key : map.keySet() ) {
}

for ( Object value : map.values() ) {
}

for ( Map.Entry<String, Object> entry : map.entrySet() ) {
    String key = entry.getKey();
    Object value = entry.getValue();
}
rahul0705
  • 41
  • 7
1

You can get the values as

for(final String key : this.objects.keys()){
  final Object value = this.objects.get(key);
}
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37