Actually i am facing a problem in comparing string in java. The problem is that i have Arraylist type list which contain some employee designation but designation may be developer or Junior developer or may be Sinior developer type designation my problem that consider all developer designation as a only developer so i use this code for this.
ArrayList<String> al_designation = ret.getDesignationListFromRegistration();
int ii = 0;
for (String designation : al_designation) {
ii++;
for (int j = ii; j < al_designation.size(); j++) {
if (designation.toLowerCase().contains(al_designation.get(j).toLowerCase())) {
al_designation.remove(j);
}
}
}
What i want all HELPER type designation consider only HELPER, Same for STITCHER all type STITCHER.
Add required items from your ArrayList to a HashMap as follows:
ArrayList<String> al_designation = new ArrayList<>(Arrays.asList("Developer", "Junior Developer",
"Senior Developer", "Senior Architect", "Junior Manager", "Senior Manager", "Assistant Manager", //
"CEO", "Tech Lead", "Dev Lead"));
System.out.println(al_designation);// Before it contains all
// designations.
Map<String, String> map = new LinkedHashMap<>();
for (String designation : al_designation) {
designation = designation.toLowerCase();
String[] words = designation.split(" ");
String lastWord = words[words.length - 1];
String value = map.get(lastWord);
if (value == null)
map.put(lastWord, designation);
else
map.put(lastWord, lastWord);
}
al_designation.clear();
al_designation.addAll(map.values());
System.out.println(al_designation);// Now it contains only required designations.
//This is your answer.
I don't know what are you trying to accomplish however you can check if your string e.g. "Junior developer" contains substring "developer" like this:
if (str1.toLowerCase().contains(str2.toLowerCase())){
// do whatever you need to do
}