Collection ou Map pra incluir em ordem

3 respostas
Rubem_Azenha

ae...
tem alguma collection ou map que inclui objetos em ordem, sem eu precisar fazer metodos para incluir, excluir, etc em ordem?

o objeto que eu quero manipular é esse:
public class Contato {
	
	private String telefone ="",
					nome = "",
					endereco ="",
					email = "";
	private StringBuffer anotacoes = null;
	public Contato(String tel, String nom)
	{	this.telefone = tel;
		this.nome = nom;
		}
	public Contato(String tel, String nom, String ema)
	{		this.telefone = tel;
			this.nome = nom;
			this.email = ema;
	}
	
	public Contato(String tel, String nom, String ema, String end)
	{		this.telefone = tel;
			this.nome = nom;
			this.email = ema;
			this.endereco = end;
	}
	
	public Contato(String tel, String nom, String ema, String end, StringBuffer anotac)
	{		this.telefone = tel;
			this.nome = nom;
			this.email = ema;
			this.endereco = end;
			this.anotacoes = anotac;
	}
	
	public String getTelefone()
	{	return this.telefone;
		}
	public String getNome()
	{	return this.nome;
		}
	public String getEmail()
	{	return this.email;
		}
	public String getEndereco()
	{	return this.endereco;
		}
	public StringBuffer getAnotacoes()
	{	return this.anotacoes;
		}
	
	public void setTelefone(String tel)
	{	this.telefone = tel;
		}
	
	public void setNome(String tel)
	{	this.nome = tel;
		}
	
	public void setEmail(String tel)
	{	this.email = tel;
		}
	
	public void setEndereco(String tel)
	{	this.telefone = tel;
		}
	
	public void setAnotacoes(StringBuffer anotac)
	{	this.anotacoes = anotac;
		}
	public int compareTo(Contato cont )
	{
		return this.telefone.compareTo(cont.telefone);
		}
}

agradeço ja a ajuda...
estou fazendo uma agenda virtual...

3 Respostas

kuchma

“microfilo”:
ae…
tem alguma collection ou map que inclui objetos em ordem, sem eu precisar fazer metodos para incluir, excluir, etc em ordem?

Voce quer que fique na ordem de insercao ou em alguma ordem especifica? Procure por LinkedHashMap e TreeMap.

Marcio Kuchma

TedLoprao

Vc inclusive dá uma olhada na classe Comparator que é quem irá dizer a ordem em que devem ficar os objetos…

T

Para usar direitinho com TreeMap você precisa fazer duas coisas:

a) … implements Comparable
na definição da classe

b)

public int compareTo(Object cont ) { return this.telefone.compareTo(((Contato)cont).telefone); }

Para funcionar direitinho você tem de definir como (Object cont) e não como (Contato cont). Isso é devido à definição de interfaces.

Criado 18 de agosto de 2004
Ultima resposta 19 de ago. de 2004
Respostas 3
Participantes 4