Skip to content

Commit 9a1e4f5

Browse files
committed
Create HashSetTest2.java
1 parent bed0f23 commit 9a1e4f5

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Collections/HashSetTest2.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//version 11.1
2+
3+
import java.util.*;
4+
5+
class Student
6+
{
7+
int rollno,marks;
8+
char grade;
9+
String name;
10+
Student(int rollno,int marks,String name)
11+
{
12+
this.rollno=rollno;
13+
this.marks=marks;
14+
this.name=name;
15+
}
16+
public String toString()
17+
{
18+
return(rollno+" "+name+" "+marks);
19+
}
20+
public int hashCode()
21+
{
22+
return(rollno);
23+
}
24+
public boolean equals(Object ob)
25+
{
26+
Student st=(Student)ob;
27+
return(rollno == st.rollno && marks == st.marks);
28+
}
29+
}
30+
class HashSetTest2
31+
{
32+
public static void main(String s[])
33+
{
34+
HashSet hs=new HashSet();
35+
hs.add(new Student(101,85,"abc"));
36+
hs.add(new Student(102,75,"xyz"));
37+
hs.add(new Student(103,65,"pqr"));
38+
hs.add(new Student(101,85,"hij"));
39+
40+
Iterator itr=hs.iterator();
41+
while(itr.hasNext())
42+
{
43+
System.out.println(itr.next());
44+
}
45+
}
46+
}
47+

0 commit comments

Comments
 (0)