I have read some of the questions (one,two) regarding having duplicate keys in the LinkedHashMap
. But I have a specific requirement to handle a scenario in legacy code.
I have LinkedHashMap<String,String>
in the code, I need to modify the code to support duplicate Keys. I created below class
public class StringContainer {
private String data;
public StringContainer(String data) {
this.data = data;
}
@Override
public String toString() {
return this.data;
}
@Override
public int hashCode() {
return 10;
}
}
And modified the code to LinkedHashMap<StringContainer,String>
. Now my code works fine as I broke the equal()
method by not overriding. Is this approach is fine? Any hidden issues I will face (other than performance)?