如何使用HashMap获取列表的字符串而不重复(How to get Strings of list without repetition using HashMap)

编程入门 行业动态 更新时间:2024-10-16 18:23:08
如何使用HashMap获取列表的字符串而不重复(How to get Strings of list without repetition using HashMap)

我有一个名为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; } }

我有一个外星人列表

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);

使用第一类的方法

所以我的问题是如何使用hashmap在'Nom'中从listPers中获取personnes而不重复?

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 ?

最满意答案

你不需要HashMap 。 Set应该做。

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

或更简单地说:

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

另外make equals and hashCode在Personne类中实现。 (另见这个问题的答案)

在您的评论之后更新并且知道这是作业后:

ArrayList (或者,通常是List )是一种数据结构,它存储项目列表并记住项目添加到列表中的顺序。 正如您已经发现的那样,它不能保证物品的唯一性。

HashMap将项存储在键值对中。 它不记得实现事物的顺序(实际上List和Map在意识形态上是不同的东西。列表的概念是按顺序存储东西,并使用索引(位置)来识别它们 ,map 通过键识别值

Map确保每个键只有一个值。 当您添加另一个值时,前一个值将被覆盖。

因此,您需要一个带有唯一标识对象的键的地图。 这是Personne的nom 。

使用key作为String (对于nom )和值创建HashMap - Personne本身:

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

循环遍历List ,获取Personne对象并将对象添加到Map 。 就像是:

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

personne是列表中的对象

Map现在只有具有唯一名称的Personne 。

注意:如果有两个具有相同名称的Personne ,则列表中稍后出现的Personne将覆盖映射中的早期Personne 。 如果你想避免这种情况,你可以使用containsKey方法来查看Map是否已经拥有密钥,并且只有在没有时才添加到地图中。

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.

更多推荐

本文发布于:2023-08-06 20:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455765.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   字符串   如何使用   列表   HashMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!