单项选择题
public class Person {
private name;
public Person(String name) {
this.name = name;
}
public boolean equals(Object o) {
if( !o instanceof Person ) return false;
Person p = (Person) o;
return p.name.equals(this.name);
}
}
Which is true?()
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same name.
C. All Person objects will have the same hash code because the hashCode method is not overridden.
D. If a HashSet contains more than one Person object with name=”Fred”, then removing another person, also with name=”Fred”, will remove them all.
相关考题
-
多项选择题
public class Key { private long id1; private long 1d2; // class Key methods } A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key?()
A. public int hashCode()
B. public boolean equals(Key k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k) -
单项选择题
public class Person { private String name, comment; private int age; public Person(String n, int a, String c) { name = n; age = a; comment = c; } public boolean equals(Object o) { if(! (o instanceof Person)) return false; Person p = (Person)o; return age == p.age && name.equals(p.name); } } What is the appropriate definition of the hashCode method in class Person?()
A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() /2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3; -
单项选择题
Aprogrammerhasanalgorithmthatrequiresajava.util.Listthatprovidesanefficientimplementationofadd(0,object),butdoesNOTneedtosupportquickrandomaccess.Whatsupportstheserequirements?()
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
