package day6;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Fruit{
String name;
public Fruit(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Fruit [name=" + name + "]";
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object obj) {
Fruit target = (Fruit) obj ;
return this.name.equals(target.name);
}
}
public class HashSetDemo2 {
public static void main(String[] args) {
Set<Fruit> fruits = new HashSet<Fruit>();
fruits.add(new Fruit("apple"));
fruits.add(new Fruit("apple"));
fruits.add(new Fruit("banana"));
System.out.println(fruits.size());
fruits.forEach(x -> System.out.println(x));
Fruit f1 = new Fruit("pineapple");
Fruit f2 = new Fruit("pineapple");
System.out.println(f1.equals(f2));
System.out.println(f1.hashCode());
System.out.println(f2.hashCode());
List<Object> lists = Arrays.asList(fruits.toArray());
System.out.println(lists.get(0).equals(lists.get(1)));
}
}