Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:34:02+00:00 2026-06-04T16:34:02+00:00

I have class named Personne public class Personne implements java.lang.Comparable<Personne> { protected String nom;

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:34:04+00:00Added an answer on June 4, 2026 at 4:34 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class named Entry declared like this: class Entry{ string Id {get;set;}
I have a class named baseClass. From this class I inherit a class names
I have a class named Person and in this class is the property PersonName
I have a class named InvoiceLine which has the following properties. public class InvoiceLine
I have a class named Graph, in this class I have a member named
I have a class named MyPrimaryClass, this class has a button witch when pressed,
I have one class named handler and this class process the http request comming
I have a class named ActivityLog . This class holds a list of ActivityRecords
I have an class named Foo. This class contains a collection of child objects
I have a class named Person with multiple properties, for example: public class Person

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.