-
-
Save wprotheus/d4e401ed46e50ee6d615f37fbbb70ca0 to your computer and use it in GitHub Desktop.
Api Java Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masterdev; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class ExemplosMap { | |
public static void main(String[] args) { | |
Map<Long, Pessoa> mapa = new HashMap<>(); | |
Pessoa joao = new Pessoa(1L, "Joao"); | |
mapa.put(1L, joao); | |
Pessoa joao2 = mapa.get(1L); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masterdev; | |
import java.util.HashSet; | |
import java.util.Set; | |
public class ExemplosSet { | |
public static void main(String[] args) { | |
Set<Pessoa> pessoas = new HashSet<>(); | |
Pessoa joao = new Pessoa(1L, "João"); | |
pessoas.add(joao); | |
for (Pessoa pessoa: pessoas) { | |
System.out.println(pessoa.getNome()); | |
} | |
System.out.println("--------"); | |
boolean adicionou = pessoas.add(new Pessoa(1L, "João")); | |
if (adicionou) | |
System.out.println("adicionou "); | |
else | |
System.out.println("Não adicionou "); | |
for (Pessoa pessoa: pessoas) { | |
System.out.println(pessoa.getNome()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masterdev; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ExemplosList { | |
public static void main(String[] args) { | |
List<Pessoa> pessoas = new ArrayList<>(); | |
pessoas.add(new Pessoa(1L, "João")); | |
Pessoa a = pessoas.get(0); | |
for (Pessoa pessoa: pessoas) { | |
System.out.println(pessoa.getNome()); | |
} | |
System.out.println("-----------"); | |
pessoas.add(a); | |
for (Pessoa pessoa: pessoas) { | |
System.out.println(pessoa.getNome()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package masterdev; | |
import java.util.Objects; | |
public class Pessoa { | |
private Long id; | |
private String nome; | |
public Pessoa(Long id, String nome) { | |
this.id = id; | |
this.nome = nome; | |
} | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof Pessoa)) return false; | |
Pessoa pessoa = (Pessoa) o; | |
return id.equals(pessoa.id) && nome.equals(pessoa.nome); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(id, nome); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment