imin
imin

Reputation: 4578

search list of custom objects by its properties

I have list of ObjectLocation, declared as

List<ObjectLocation> myLocations;

And here's how ObjectLocation looks like:

public class ObjectLocation {
    int locationID, ratingCount = 0;
}

Ok now myLocations holds thousands of locationID. If I have a particular locationID, how do I search the contents of myLocations for the locationID, and get the searched locationID's index (within myLocations) and it's ratingCount?

Upvotes: 0

Views: 46

Answers (5)

Arne Burmeister
Arne Burmeister

Reputation: 20594

For Java 8, I would use (without changing anything on the data structure like using a Map instead or knowing about ordering in the list):

Optional<ObjectLocation> found = myLocations.stream()
  .filter(location -> location.locationID == particularLocationID)
  .findAny();
if (found.isPresent() {
  int ratingCount = found.get();
  …
}

When you need more performance for single searches, you may try parallelStream() instead of stream().

Upvotes: 0

user1585852
user1585852

Reputation:

List<ObjectLocation> myLocations = new ArrayList<>();

int index =0;
        int particularId = 1;//!your Id
        int locationid = 0;
        int ratingcount = 0;
        for(int i =0; i < myLocations.size(); i++) {
            if(myLocations.get(i).locationID == particularId) {
                index = i;
                locationid = myLocations.get(i).locationID;
                ratingcount = myLocations.get(i).ratingCount;
            }            
        }

Upvotes: 0

Rezwan Azfar Haleem
Rezwan Azfar Haleem

Reputation: 1218

For you to easily search and find a your ObjectLocation objects, you should first define .equals(Object o) method, in ObjectLocation class, that allows one ObjectLocation to be compared to another. After that, all you have to do is use .indexOf(Object o)' to get the index of the ObjectLocation you are looking for. Then extract that object and use its information as exemplified in the code below:

public class ObjectLocation {
    int locationID, ratingCount = 0;

    public boolean equals(Object o)
    {
         if(!(o instanceof ObjectLocation))
         return false;

         ObjectLocation another = (ObjectLocation)o;
         if( locationID == another.locationID && ratingCount == another.ratingCount)
             return true;
         else
             return false;
    }

    public static void main(String[] args)
    {
        List<ObjectLocation> myLocations;
        ObjectLocation findThisLocation;
        ObjectLocation found;
        //Additional code here

        int index = myLocations.indexOf(findThisLocation);
        found = myLocations.get(index);
        int id = found.locationID;
        int rating = found.ratingCount;
    }
}

Upvotes: 0

Bobulous
Bobulous

Reputation: 13169

For this sort of lookup I'd switch to using a Map<Integer, ObjectLocation> and store entries in the map like this:

Map<Integer, List<ObjectLocation>> myLocationMap = new HashMap<>();
List<ObjectLocation> currentList = myLocationMap.get(oneLocation.locationID);
if(currentList == null) {
    // We haven't stored anything at this locationID yet,
    // so create a new List and add it to the Map under
    // this locationID value.
    currentList = new ArrayList<>();
    myLocationMap.put(oneLocation.locationID, currentList);
}
currentList.add(oneLocation);

Now you can quickly get all of the ObjectLocation entries with a specific value for locationID by grabbing them from the map like this:

List<ObjectLocation> listOfLocations = myLocationMap.get(someLocationId);

This assumes that multiple ObjectLocation instances can have the same locationID value. If not then you wouldn't need a List<ObjectLocation> in the map, just a single ObjectLocation.

Upvotes: 1

JP Moresmau
JP Moresmau

Reputation: 7393

Well, you loop through all of the elements in the list, and if the locationID match, you've found your element!

int idx=0;
for (ObjectLocation ol:myLocations){
  if (ol.locationID==searchedLocationID){
    // found at index idx!!  
  }
  idx++;
}

More efficiently, you could have a Map<Integer,ObjectLocation> where the key is the locationID of the ObjectLocation, to get much faster lookups.

Upvotes: 2

Related Questions