2. Sets: Working with unique elements
The main implementations are: HashSet
, LinkedHashSet
and TreeSet
.
The main advantage of using sets is efficiency in preventing duplicates and their integration with Lambda expressions for data filtering and processing.
Examples with lambdas:
<code class="language-java">Set<Integer> numeros = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); // Iteração com lambda numeros.forEach(n -> System.out.print(n + " ")); // 1 2 3 4 5 // Remover elementos com base em uma condição numeros.removeIf(n -> n % 2 == 0); System.out.println(numeros); // [1, 3, 5]</code>
The above is the detailed content of Set