java - How compare Enum Value? -
this question has answer here:
i want compare enum's value in example.
public enum en{ vanila("good"), marlena("luck"), garnela("good"); private string value; private en(string s){ this.value = s; } }
as vanila
, garnela
have same value comparing them should return true. there 2 ways 1 ==
operator , second equals()
method. tried own logic , add method enum.
public boolean comparevalue(en e){ return (this.value).equals(e.value); }
and it's working fine.
en = en.vanila; en b = en.garnela; en c = en.marlena; if(a.comparevalue(b)){ system.out.println("a == b"); } if(a.comparevalue(c)){ system.out.println("a == c"); } if(b.comparevalue(c)){ system.out.println("b == c"); }
i know can't override equals methods in enum ( don't know why, know final not logically reason. can explain, why can't override equals() in enum?).
any how there other way or fine?
in enum each instance meant represent distinguished thing. enum value used describe thing, make human readable, etc.
therefore there's no reason code consider vanilla equal marlena. , makes no sense set same string values both of them.
Comments
Post a Comment