Welcome Guest | Sign in | Register
Collections - Discussion Page For Q.2561 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Collections > Questions and Answers

Q: What is the output of below code?

public class Bean{
private String str;
Bean(String str ){
this.str = str;
}
public String getStr() {
return str;
}
public boolean equals(Object o){
if (!(o instanceof Bean)) {
return false;
}
return ((Bean) o).getStr().equals(str);
}

public int hashCode() {
return 12345;
}
public String toString() {
return str;
}
}

import java.util.HashSet;
public class Test {
public static void main(String ... sss) {
HashSet myMap = new HashSet();
String s1 = new String("das");
String s2 = new String("das");
Bean s3 = new Bean("abcdef");
Bean s4 = new Bean("abcdef");
myMap.add(s1);
myMap.add(s2);
myMap.add(s3);
myMap.add(s4);
System.out.println(myMap);
}
}
A. das abcdef B. das abcdef das abcdef
C. das das abcdef abcdef D. das

Answer and Explanation

Answer:das abcdef

Explanation:
Implement 'equals' and 'hashCode' methods to get unique result in Set.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.