0

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 ?

Adel Bachene
  • 974
  • 1
  • 14
  • 34

3 Answers3

4

You don't need a HashMap for that. A Set should do.

Set uniquePers = new HashSet(listPers);
List listUniquePers = new ArrayListSet(uniquePers);

or more simply:

List listUniquePers = new ArrayListSet(new HashSet(listPers));

Also make equals and hashCode are 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 HashMap stores the items in key value pairs. It does not remember the order in which thing were implemented (actually List and Map are 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 Map guarantees 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 nom of the Personne.

Create HashMap with key as a String (for the nom) and value - the Personne itself:

HashMap<String, Personne> uniquePersonne = new HashMap<String, Personne>();

Loop through the List, get a Personne object and add the object to the Map. Something like:

uniquePersonne.put(personne.getNom(), personne)

where personne is the object from your list

The Map will now have only the Personnes 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 the containsKey method to see whether the Map already has the key, and add to the map only if not.

Community
  • 1
  • 1
Nivas
  • 18,126
  • 4
  • 62
  • 76
3

Create a hash set of the names that you have seen, add names to it as you go through the list, and keep only the items that do not have an entry in the seen hash set:

Set<String> seen = new HashSet<String>();
List<Personne> unique = new ArrayList<Personne>();
for (Personne p : listPers) {
    if (seen.add(p.nom)) {
        unique.add(p);
    }
}

Note that this approach ignores prenom altogether, because you wanted people without repetitions in nom only. This keeps the first person with the given name. You can also do it with a hash map:

Map<String,Personne> byName = new HashMap<String,Personne>();
for (Personne p : listPers) {
    byName.put(p.nom, p);
}
List<Personne> unique = new ArrayList<Personne>(byName.values());
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Thinking about inserting them and ordering by name, you have to define which ones will you want to keep. If you want to keep the first appearance of a Personne, the code is as follows:

Map<String, Personne> personneMap = new HashMap<String, Personne>();
for(Personne p : listPers) {
    if(!personneMap.contains(p.getNom())) {
        personneMap.put(p.getNom(), p);
    }
}

Otherwise, just add the Personnes without the contains check. That way you'll only save the last Personne with a registered name on the list.

Note that I'm using the Map interface to represent the map, but you can create a HashMap directly.

Fritz
  • 9,987
  • 4
  • 30
  • 49