Skip to content

Instantly share code, notes, and snippets.

@dheysonalves
Created October 8, 2018 18:52
Show Gist options
  • Save dheysonalves/6edc34786d192b4eaee11714f5c17fd2 to your computer and use it in GitHub Desktop.
Save dheysonalves/6edc34786d192b4eaee11714f5c17fd2 to your computer and use it in GitHub Desktop.
Collection Implementation
package TestArea;
/**
*
* @author DHEYSON
*/
public class Aluno implements Comparable<Aluno> {
private String nome;
private String matricula;
private int idade;
public Aluno(String nome, String matricula, int idade) {
this.nome = nome;
this.matricula = matricula;
this.idade = idade;
}
@Override
public int compareTo(Aluno o) {
if(this.getNome().equals(o.getNome())){
return 0;
}
if (this.getIdade() > o.getIdade()) {
return 1;
}
return -1;
}
public String getNome() {
return nome;
}
public int getIdade() {
return idade;
}
@Override
public String toString() {
return "nome:" +getNome() + " | " + "Matricula:" +matricula + " | " + "Idade:" +getIdade() + " | ";
}
}
package TestArea;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
*
* @author DHEYSON
*/
public class TestTime {
public static void main(String[] args){
Set<Aluno> collection = new TreeSet<Aluno>();
collection.add(new Aluno("Jane","172091891",22));
collection.add(new Aluno("Brock","314210320",41));
collection.add(new Aluno("Gwen","698547432",12));
Iterator<Aluno> it = collection.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment