Consider the following program and predict the output:
import java.util.HashSet; class Shape{ public Shape(int r) { rollNo = r;/*ww w . java 2s . c om*/ } int rollNo; } public class Main { public static void main(String[] args){ HashSet<Shape> students = new HashSet<>(); students.add(new Shape(5)); students.add(new Shape(10)); System.out.println(students.contains(new Shape(10))); } }
b)
Since methods equals()
and hashcode()
are not overridden for the Shape class, the contains()
method will not work as intended and prints false.