I would like to find the longest String within a Java Enum. What is the best way to do this? I havent been working to much with Enums so any suggestion is welcome. These are my enums
public enum DOMAIN_LANGUAGES {
ENG, SWE;
public static List<DOMAIN_LANGUAGES> getDomainLanguages(){
List<DOMAIN_LANGUAGES> languages = new ArrayList<DOMAIN_LANGUAGES>();
languages.add(ENG);
languages.add(SWE);
return languages;
}
}
public enum DOMAIN_STATE {
LIVE,
PENDING_RENEWAL,
PENDING_TRANSFER_OUT,
}
EDIT:
I didnt define this question very well and hence i edit it. What I whant from my two enums is a function that takes any of the two kind of enums i have defined as and finds the longest literal. So in the DOMAIN_STATE that would be "PENDING_TRANSFER_OUT". I hope this makes things a bit easier to understand.
EDIT 2
So now i'v gotten som help that has been great, but im not sure why this code will not work. It complains when i try to use the enumList.values()? This is the reason i couldnt find a solution in the first place, what am i missing? =)
public String CalculateDropdownListWidth(Enum enumList){
int chars = 0;
for(Enum e : enumList.values()){
//do stuff
}
}
FINAL:
So now i knwo why this didnt work. I hade to loop over a EnumSet instead of a Enum. Since the Enum is only one in the "set" if im not totally misstaken. So this is my solution. Thanks all for helping out!
public String CalculateDropdownListWidth(EnumSet enumList){
int chars = 0;
for(Object e : enumList){
if(e.toString().length() > chars){
chars = e.toString().length();
}
}
//Pixelmodifier
Double oneChar = 1.35;
Double result = oneChar * chars;
return String.valueOf(result) + "px";
}
Thx for any help!
/Marthin