I have class named Personne
public class Personne implements java.lang.Comparable<Personne> {
protected String nom;
protected String prenom;
public Personne(String nom,String prenom){
this.nom=nom;
this.prenom=prenom;
}
public String toString(){
return nom+", "+prenom;
}
@Override
public int compareTo(Personne pers) {
// TODO Auto-generated method stub
if(!(pers instanceof Personne))
throw new ClassCastException();
Personne p = pers;
int comparaison;
if((comparaison = nom.compareTo(p.getNom())) != 0)
return comparaison;
else if((comparaison = prenom.compareTo(p.getPrenom())) != 0)
return comparaison;
return comparaison;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
}
and i have an outher class List of Personne
public class ListePersonne {
protected static ArrayList <Personne> listPers = new ArrayList <Personne>();
public static void main(String[] ards){
Personne p1 = new Personne("Bachene","Adel");
listPers.add(p1);
Personne p2 = new Personne("Bourada","Amine");
listPers.add(p2);
Personne p3 = new Personne("Bachene","Amine");
listPers.add(p3);
Personne p4 = new Personne("Benouda-zouaui","Khalil");
listPers.add(p4);
Personne p5 = new Personne("Bachene","Hakim");
listPers.add(p5);
Personne p6 = new Personne("Mazachi","Oussama");
listPers.add(p6);
Personne p7 = new Personne("Bachene","Issam");
listPers.add(p7);
Personne p8 = new Personne("Allel","Mohamed");
listPers.add(p8);
Personne p9 = new Personne("Bachene","Mohamed");
listPers.add(p9);
Personne p10 = new Personne("Yank","Maher");
listPers.add(p10);
using the methods of the 1st class
so my question is how to get personnes from listPers without repetition in ‘Nom’ using hashmap ?
You don’t need a
HashMapfor that.A
Setshould do.or more simply:
Also make
equals and hashCodeare implemented in the Personne class. (See also answers to this question)Update after your comments and after knowing this is homework:
ArrayList(or, in general,List) is a data structure that stores list of items and remember the order the items were added to the list. As you have found out already it does not guarantee uniqueness of items.A
HashMapstores the items in key value pairs. It does not remember the order in which thing were implemented (actuallyListandMapare ideologically different things. The concept of a list is storing stuff sequentially, and using the index (position) to identify them and map is identifying values via keys.A
Mapguarantees that there will be only one value per key. When you add another value the previous one is overwritten.So you need a map with a key that uniquely identify your object. Here it is the
nomof thePersonne.Create
HashMapwith key as aString(for thenom) and value – thePersonneitself:Loop through the
List, get aPersonneobject and add the object to theMap. Something like:where
personneis the object from your listThe
Mapwill now have only thePersonnes with unique names.Note: If there are two
Personnes with the same name, the one coming later in the list will overwrite the earlier one, in the map. If you want to avoid that you can use thecontainsKeymethod to see whether the Map already has the key, and add to the map only if not.