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?

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51

3 Answers3

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);
    } 
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 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. – Mukul Sharma Nov 01 '16 at 09:11
  • @MukulSharma No, String is a final class, so you can't extend it and therefore can't override any of its methods. – Eran Nov 01 '16 at 09:12
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 )

Community
  • 1
  • 1
Asoub
  • 2,273
  • 1
  • 20
  • 33
  • But this is an override to a Comparator> compare(..) method, not to the hashCode() method for a String . . . – Trunk Jul 09 '19 at 10:44
  • @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. – Asoub Jul 09 '19 at 11:27
  • 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. – Trunk Jul 09 '19 at 12:14
  • 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. – Asoub Jul 09 '19 at 12:33
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";

gladiator
  • 1,249
  • 8
  • 23