为什么set.size()为String和Java对象显示不同的值?
为什么为什么set.size()为String和Java对象显示不同的值?,java,collections,set,size,hashset,Java,Collections,Set,Size,Hashset,为什么set.size()为字符串和Java对象显示不同的值,而它们是为重复的元素显示的 如果我在String中插入重复值并运行set.size(),它只计算唯一的元素,但在Object中,当我插入重复值时,它计算总数,包括重复本身。为什么会这样 FIRST PROGRAM ============== /** Insert Duplicate in String **/ public class SetDemo { public static void main(String[] a
set.size()
为字符串和Java对象显示不同的值,而它们是为重复的元素显示的
如果我在String中插入重复值并运行set.size(),它只计算唯一的元素,但在Object中,当我插入重复值时,它计算总数,包括重复本身。为什么会这样
FIRST PROGRAM
==============
/** Insert Duplicate in String **/
public class SetDemo {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
// Insert duplicate values
set.add("Rai");
set.add("Rai");
set.add("Tama");
// show set size
System.out.println(set.size());
}
}
Output: 2
=======================================================================
SECOND PROGRAM
====================
/* Insert Duplicate in Object */
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id,String name,String author,String publisher,int quantity)
{
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class HashSetExample {
public static void main(String[] args) {
HashSet<Book> set=new HashSet<Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications &
Networking","Forouzan","Mc Graw Hill",4);
// Insert duplicate values
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
Book b4=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to HashSet
set.add(b1);
set.add(b2);
set.add(b3);
set.add(b4);
//Traversing HashSet
for(Book b:set){
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+"
"+b.quantity);
}
// show set size
System.out.println(set.size());
}
}
Output: 4
第一个程序
==============
/**在字符串中插入重复项**/
公共类SetDemo{
公共静态void main(字符串[]args){
Set=newhashset();
//插入重复的值
设置。添加(“Rai”);
设置。添加(“Rai”);
集合。添加(“Tama”);
//显示集大小
System.out.println(set.size());
}
}
产出:2
=======================================================================
第二程序
====================
/*在对象中插入重复项*/
课堂用书{
int-id;
字符串名称、作者、发布者;
整数;
公共图书(整数id、字符串名称、字符串作者、字符串出版商、整数数量)
{
this.id=id;
this.name=名称;
this.author=作者;
this.publisher=publisher;
这个。数量=数量;
}
}
公共类HashSetExample{
公共静态void main(字符串[]args){
HashSet=newhashset();
//创作书籍
b1册=新书(101本,“让我们来C”,“亚什旺·卡内卡”,“BPB”,8);
图书b2=新书(102,“数据通信与
“网络”、“福鲁赞”、“麦格拉山”,4);
//插入重复的值
书b3=新书(103,“操作系统”、“高尔文”、“威利”,6);
书b4=新书(103,“操作系统”、“高尔文”、“威利”,6);
//向哈希集添加书籍
增加(b1);
增加(b2);
增加(b3);
增加(b4);
//遍历哈希集
(b册:集){
System.out.println(b.id+“”+b.name+“”+b.author+“”+b.publisher+“”)
“+b.数量);
}
//显示集大小
System.out.println(set.size());
}
}
产出:4
如果我在字符串中插入重复值并运行
set.size()
它只计算唯一的元素,但在对象中,当我插入重复值时,它计算总数,包括重复本身。为什么会这样?在第一个程序中不允许重复值,但在第二个程序中set.size()
应为3,但它显示4个set
仅存储唯一的元素。它使用对象的equals
和hashCode
方法来检查对象是否等于另一个对象。
String
已经编写了正确的equals
和hashCode
方法。
您自己的对象可能没有。您应该重写从
对象
类派生的方法equals
和hashCode
,然后您自己的对象的行为将与字符串相同。要使HashSet正常工作,您需要重写hashCode()。当您创建包含多个属性的自定义类时,应该相应地重写hashcode以正常工作。这是因为HashSet使用hashcode()从HashSet添加和获取值。在Java中,hashcode()和equal()方法之间有一个约定,即如果两个对象相等,它应该返回相同的hashcode。如果您遵循此契约,这些实现将不仅对哈什集有效,而且HashMaps也会生效。您的<代码> < <代码> >类应该重写<代码> hash码< /COD>和<代码>等于 >以便<代码> hShSET 知道您认为哪个代码> < <代码> >实例是相同的。