-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColecoes.java
64 lines (49 loc) · 2.29 KB
/
Colecoes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Conhecendo Collections em Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Colecoes {
public static void main(String[] args) {
/***** LISTS *****/
List<Double> notas = new ArrayList<>();
System.out.println("Lista vazia: "+ notas);
notas.add(1d); //adiciona um elemento
System.out.println("Lista com 1 elemento: "+notas);
notas.add(1, 2.5); //adiciona elemento na posição indicada
System.out.println("Lista com adição de um elemento no index 1: " + notas);
System.out.println("Posição do elemento 2.5: " + notas.indexOf(2.5));
notas.add(1,8d); //toma o lugar do anterior e empurra
System.out.println("Lista com adição de um elemento no index 1: " +notas);
notas.set(2,3d); //substitui o valor nesta posição
System.out.println("Lista com substituição do elemento no index 2: " +notas);
//verifica se contém um valor
System.out.println("Contém elemento 2.5: " + notas.contains(2.5));
System.out.println("Contém elemento 3: " + notas.contains(3d));
System.out.println("Elemento na posição 2: "+ notas.get(2));
System.out.println("Valor Mínimo: " + Collections.min(notas)); //só funciona para interface comparable
//Soma:
Iterator<Double> iterator = notas.iterator();
Double soma=0d;
while (iterator.hasNext()){
Double next = iterator.next();
soma += next;
}
System.out.println("Soma dos elementos via iterator: "+ soma);
//Remover os elementos < 2
Iterator<Double> iterator2 = notas.iterator(); //avalia elemento a elemento
while (iterator2.hasNext()){
Double next = iterator2.next();
if (next < 2)
iterator2.remove();
}
System.out.println("Exclui valores <2: " + notas);
/***** SETS *****/
//Os métodos são parecidos
Set<Double> notas2 = new HashSet<>(Arrays.asList(10.0,5d,3.5,7d,7d,2.2));
System.out.println("HashSet: "+ notas2); //observe que não fica na ordem e não repete o 7
}
}