1

I am studying programming using Java, and I facing a problem with java.util.HashSet. How to show the size() in HashSet? This my code?

package name;
public class Student
{
   private String name;
   public String getName()
   {
      return this.name;
   }
   public void setName(String name)
   {
     this.name = name;
   }
}
// Entry Point
package client;
import name.Student;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
public class Client1
{
    public static void main (String[] args)
    {
       Scanner scan = new Scanner(System.in);
       Set<Student> students = new HashSet<Student>();
       Student student = new Student();

       int totalStudent = 0;
       System.out.print("TypeTotal Student : ");
       totalStudent = Integer.parseInt(scan.nextLine());
       
       for(int i = 0; i < totalStudent ; i++)
       {
           System.out.print("Name : ");
           String name = scan.nextLine();
           student.setName(name);
           students.add(student);
       }
       System.out.println("Element Total In Set :" students.size());
       for(Student std: students)
       {
           System.out.println(std.getName());
       }
    }
}

If I run this code in terminal, student.size() is not increasing. I need advice.

6
  • 4
    you are instantiating Student outside the for loop, so you end up adding many times the very same instance and changing its name Commented May 1, 2018 at 14:14
  • sorry sir i edit the question please look at again Commented May 1, 2018 at 14:17
  • 2
    The HashSet has a single Student instance (in fact you invoked new Student() only once). Move it into the for loop. Commented May 1, 2018 at 14:21
  • What exactly are you asking? Sorry but i am not getting it clearly. Commented May 1, 2018 at 14:25
  • thanks @DavideCavestro for your tips Commented May 1, 2018 at 14:28

3 Answers 3

2

That's because you're adding the same student everytime in hashset. And HashSet doesn't allow duplicate values.

Try this way.

// class Student

package name;
public class Student
{
   private String name;
   public String getName()
   {
      return this.name;
   }
   public void setName(String name)
   {
     this.name = name;
   }
}
// Entry Point
package client;
import name.Student;
import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;
public class Client1
{
    public static void main (String[] args)
    {
       Scanner scan = new Scanner(System.in);
       Set<Student> students = null;
       students = new HashSet<Student>();
       Student student;

       int totalStudent = 0;
       System.out.print("TypeTotal Student : ");
       totalStudent = Integer.parseInt(scan.nextLine());

       for(int i = 0; i < totalStudent ; i++)
       {
           System.out.print("Name : ");
           String name = scan.nextLine();
           student = new Student();
           student.setName(name);
           students.add(student);
       }
       System.out.println("Element Total In Set :" students.size());
       for(Student std: students)
       {
           System.out.println(std.getName());
       }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

please explain about it Student student;
It is always a good practice to declare a reference outside the loop. So the reader is aware what all objects are used in the class without getting into the core implementation of class.
@Abhishek I disagree. IMHO this habit is error prone, cause you end up with some side effects among which: 1. the student variable has a wider scope than needed 2. reassigning the same variable many times prevents you from using the final keyword (which is very useful)
1

In your code, Student student is a reference to exactly one object (ever). Since you do not allocate a new Student each time you add an object to the students set, you are, instead, adding the same object to the Set multiple times.

Note that I used the words "same object" and "Set". A Set allows no more than one instance of an object.

Your code is pretending to add multiple students, but is actually only adding one student.

The solution is as follows:

 // really add a student to the set.
 System.out.print("Name : ");
 String name = scan.nextLine();
 student = new Student();// note this line.
 student.setName(name);
 students.add(student);

Comments

1

If you use this block:

{
     System.out.print("Name : ");
     String name = scan.nextLine();
     student.setName(name);
     students.add(student);
}

you are using always same object and Hashset does not allow duplicate (even if you change one property of the object).

Using this block it works fine, because you are creating a new object every interaction:

{
    System.out.print("Name : ");
    String name = scan.nextLine();
    student = new Student();
    student.setName(name);
    students.add(student);
}

even if the variable is the same (student) you create a new instance with new

1 Comment

my pleasure! Happy coding!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.