Created
October 8, 2018 18:52
-
-
Save dheysonalves/6edc34786d192b4eaee11714f5c17fd2 to your computer and use it in GitHub Desktop.
Collection Implementation
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 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() + " | "; | |
} | |
} |
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 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