-4

I'm trying to translate a Boolean Retrieval Model search engine and can't translate this piece of code to Java.

C#:

foreach(KeyValuePair<string ,List<string>> p in documentCollection){}

Java:

for(Map<String, ArrayList<String>> p : documentCollection){}
            

Unfortunately Java giving me this error:

for-each not applicable to application type

Thanks in advance!

Community
  • 1
  • 1
Rapharel
  • 33
  • 5

1 Answers1

6

There is a difference between the way a collection interface is implemented by C#'s IDictionary<K,V> and Java's Map<K,V>. In C#, the collection itself can be enumerated for key-value pairs; in Java, you must call a method to obtain a key-value set, which is called entrySet():

for (Map.Entry<String,ArrayList<String>> p : documentCollection.entrySet()) {
    ...
}

Note that in Java you iterate Map.Entry<K,V> objects instead of C#'s KeyValuePair<K,V>.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523