Wejście/wyjście i strumienie w Javie
Alex Liu
Software Development Engineer
HashSet używany w dalszej częściimport java.util.HashSet;public class SampleHashSetData { public static void main(String[] args) { HashSet<String> fruits = new HashSet<>();fruits.add("Apple"); fruits.add("Banana");} }
{Apple, Banana}
For-Eachfor (type x: collection){}List, Set, Map i innymifruits: [Apple, Banana].for (String x : fruits) {
System.out.println(x);
}
Apple
Banana
Iterator<>: obsługuje List, Set, Queue i inneIteratorimport java.util.Iterator;// Tworzenie obiektu `Iterator` o nazwie `it` dla zbioru `fruits` Iterator<String> it = fruits.iterator(); // Metoda `.hasNext()` sprawdza, czy istnieją kolejne elementy while (it.hasNext()) { // Metoda `.next()` pobiera element System.out.print(it.next());}
Apple Banana
.remove(), aby usunąć element za pomocą Iterator$$
Iterator<String> it = fruits.iterator();
while (it.hasNext()) {
String fruit = it.next();
if (fruit.startsWith("A")) {
// Usuwa element, jeśli zaczyna się od `A`
it.remove();
}
}
// fruits zawiera teraz tylko "Banana"
Przed usunięciem:
System.out.println(fruits);
[Apple, Banana]
Po usunięciu:
System.out.println(fruits);
[Banana]
Zapewnia bezpieczne usuwanie i zapobiega ConcurrentModificationException

For-EachList(ArrayList,LinkedList)Set(HashSet,TreeSet,LinkedHashSet)Queue(PriorityQueue)Stack(Stack)MapElement RemovalIteratorList(ArrayList,LinkedList)Set(HashSet,TreeSet,LinkedHashSet)Queue(PriorityQueue)Stack(Stack)Map (Wymagane: keySet().iterator, values().iterator lub entrySet().iterator)Element RemovalWejście/wyjście i strumienie w Javie