A HashSet of objects without equals and hashCode
Predict the outputMedium-HardJava onlyAsked in: Cognitree, Deloitte
import java.util.*;
class Tag {
String name;
Tag(String name) {
this.name = name;
}
}
record Label(String name) {}
public class Main {
public static void main(String[] args) {
Set<Tag> tags = new HashSet<>();
tags.add(new Tag("smoke"));
tags.add(new Tag("smoke"));
System.out.println(tags.size());
Set<Label> labels = new HashSet<>();
labels.add(new Label("smoke"));
labels.add(new Label("smoke"));
System.out.println(labels.size());
System.out.println(tags.contains(new Tag("smoke")));
}
}
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.