All you need is to implement the Comparable interface for your User
class. Then you can use Collections.binarySearch()
method (only if your list is sorted of course).
Something like that would work for you:
public static void main(String[] args) {
List <User> list = new ArrayList<>();
list.add(new User("A1", "B1", "Name1"));
list.add(new User("A2", "B2", "Name2"));
list.add(new User("A3", "B3", "Name3"));
list.add(new User("A4", "B4", "Name4"));
list.add(new User("A5", "B5", "Name5"));
System.out.println(list.get(Collections.binarySearch(list, new User("A4", "B4", "Name4"))));
}
static class User implements Comparable <User>{
String territory;
String company;
String name;
public User(String territory, String company, String name) {
this.territory = territory;
this.company = company;
this.name = name;
}
@Override
public int compareTo(User o) {
return (territory+company).compareTo(o.territory+o.company);
}
@Override
public String toString(){
return territory + "," + company + "," + name;
}
}