I declared a POJO class with id, Name, age, contactNumber and Address attributes. I declared all getters and setters. Now I am using HashMap<String, POJO_CLASS>. By default I sorts these values by Name attribute. Now my need is to search an object by id, and that method should return an object (which is stored in HashMap as value). So how can I use Collections.binarySearch() for this requirement.
I declared a POJO class with id, Name, age, contactNumber and Address attributes. I
Share
The better question is why would you want to. HashMap lookups are going to be very fast, O(1) time whereas binary searches are going to take O(lg(N)) time on Lists that have random access abilities (ArrayList).
If you really want to use binary searches then you need to store your objects in a list (ArrayList probably) and have that list sorted and then call
Collections.binarySearch(list, value).