Created: 2023-09-03 20:54
Status: #concept
Subject: Programming
Tags: java.util Java Java Interface java.util.ArrayList java.util.Set java.util.List

java.util.Collection

It is a Java Interface that is implemented by many data structure classes.

  • all subclasses implementing it inherit the for-each loop behavior.

All Known Implementing Classes:
Map<String, String> translations = new HashMap<>();
translations.put("ganbatte", "good luck");
translations.put("hai", "yes");

Set<String> keys = translations.keySet();
Collection<String> keyCollection = keys;

System.out.println("Keys:");
for (String key: keyCollection) {
    System.out.println(key);
}

System.out.println();
System.out.println("Values:");
Collection<String> values = translations.values();

for (String value: values) {
    System.out.println(value);
}
Keys:
ganbatte
hai

Values:
yes
good luck

References