2

I want to override String classes Hashcode / Equals methods as I know the String class is final and the method inside that can not be overridden. I have a scenario where I want to apply the same. for instance code is given below,

public static void main(String[] args) {

        Map<String,Integer> map = new HashMap();
        map.put("Mukul", 1);
        map.put("Aditya", 1);

        System.out.println(map);


    } 

As I am passing string as a key and map is gonna call String classes hashcode method implicitly. Now I want to declare that the given keys are same in my way. Please suggest if there is any way to do so?

2
  • 10
    Create your own class (MyString) that contains a String member and implements equals and hashCode as you see fit. Commented Oct 26, 2016 at 6:55
  • 1
    @Eran IMHO you should put this as an answer instead of a comment. Commented Oct 26, 2016 at 7:05

3 Answers 3

3

You can create your own custom String class that contains a String member and implements equals and hashCode as you see fit, and use a Map<MyString,Integer> :

public class MyString {

    private String value;

    public MyString (String value) {this.value = value;}
    public int hashCode () {
        ...
    }
    public boolean equals (Object other) {
        ...
    }

    public static void main(String[] args) {

        Map<MyString,Integer> map = new HashMap<>();
        map.put(new MyString("Mukul"), 1);
        map.put(new MyString("Aditya"), 1);

        System.out.println(map);
    } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Eran for the answer. but here also we are creating our own custom object. I want to override the methods hashcode , equals written in String class. Please suggest if there is any way to do so. Many thanks for showing interest answering question.
@MukulSharma No, String is a final class, so you can't extend it and therefore can't override any of its methods.
2

As a comment already state to make a wrapper of the String class with its own hashcode and equals, here is another solution: you can use a TreeMap, and provide it with your own Comparator:

TreeMap<String, Integer> myMap = 
    new TreeMap<String, Integer>(new Comparator<Entry<String, Integer>>()
    {
        public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2)
        {
            return o1.getValue().compareTo(o2.getValue()); // your method here
        } 
    });

(inspired by Java TreeMap Comparator )

4 Comments

But this is an override to a Comparator<Entry<String, Integer>> compare(..) method, not to the hashCode() method for a String . . .
@Trunk you're right, this is more of an answer to the more general problem the OP has: "Now I want to declare that the given keys are same in my way." Rather than changing how the String behaves, in their hashcode, I changed how the map will compare those String. Maybe this will be a better fit for OP, maybe not.
OP wants to override the built-in hashCode() method but doesn't say why. I assumed it was because he wants to avoid collisions. As far as I know, the TreeMap hashing gives a narrower spread of data than the HashMap. . . Please correct me if I'm wrong. If it wasn't for that, the TreeMap is good lateral thinking.
Well I thought he wanted a map where some different String keys would be considered equals using his own algorithm (so rewriting String's hashcode would work, and so does my answer). And there would certainly be more collision. Now that I reread it, maybe the Map is just an example.
0

there is no way to do that with string rather create your own object and handle it once you get the data from map .Even if you create your own string class then it won;t be compatible with already existing string class Like no MyString obj="name";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.