Entrada/Saída e Streams em Java
Alex Liu
Software Development Engineer
HashSet para o restante do vídeoimport 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-Each loopfor (type x: collection){}List, Set, Map e maisfruits: [Apple, Banana].for (String x : fruits) {
System.out.println(x);
}
Apple
Banana
Iterator<>: oferece suporte a List, Set, Queue e maisIteratorimport java.util.Iterator;// Crie o objeto `Iterator` chamado `it` para o conjunto `fruits` Iterator<String> it = fruits.iterator(); // O método `.hasNext()` verifica se há mais elementos while (it.hasNext()) { // O `.next()` recupera o elemento System.out.print(it.next());}
Apple Banana
.remove() para remover elemento com Iterator$$
Iterator<String> it = fruits.iterator();
while (it.hasNext()) {
String fruit = it.next();
if (fruit.startsWith("A")) {
// Remove o elemento se ele começar com `A`
it.remove();
}
}
// fruits agora contém apenas "Banana"
Antes de remover:
System.out.println(fruits);
[Apple, Banana]
Depois de remover:
System.out.println(fruits);
[Banana]
Garante exclusão segura e evita ConcurrentModificationException

For-Each loopList(ArrayList,LinkedList)Set(HashSet,TreeSet,LinkedHashSet)Queue(PriorityQueue)Stack(Stack)MapRemoção de elementosIteratorList(ArrayList,LinkedList)Set(HashSet,TreeSet,LinkedHashSet)Queue(PriorityQueue)Stack(Stack)Map (Use keySet().iterator, values().iterator ou entrySet().iterator)Remoção de elementosEntrada/Saída e Streams em Java