0

@EDIT: I want the function to be able to search without me saying what are the variables that it should be searching. For example, I want the same function to search for minimumClients + Location and give me all that contain those values, but if the user only chose pricePerNight + roomsQuantity then it should search on the same list for different variables without me telling it which variables to search.

I do not want to add inside the function ".getLocation().equals(location)", I want to make it so it compares the values passed as an argument that are != from NULL to the objects inside the list.

List<Property> properties = new ArrayList<>();
// Let's say I want to look for properties with the minimumClients = 4;
// I would do Property p = new Property();
p.setMinimumClients(4);
properties = getPropertyList(p);
// That would give me the Property 3

// But if I want to look for properties with pricePerNight = 30 and location = "Portugal" I would:
Property p = new Property();
p.setPricePerNight(20);
p.setLocation("Portugal);
properties = getPropertyList(p);
// This would give me the Property 1 using the same function above. I do not want to specify what I am looking for, I want to make it so it knows what I am looking for.

This is what I mean:

I have the following class

public class Property() {
    private int minimumClients;
    private int maximumClients;
    private float pricePerNight;
    private String location;
    private int roomsQuantity;
}

I'll be needing to search for properties, meaning I can either search:

  • For a location
  • For a price per night
  • For a location and number of rooms
  • For a location, with given number of rooms and a given price
  • For a location and a minimum number of clients

etc.

I'll be having a list of properties in another class called Repository

public class Repository() {
    private List<Property> properties;
}

What I want to know is: Due to the huge amount of searching possibilities on a property, is there any way I can send an object containing only the information I want to search for, and while iterating through the properties list only return the properties that contain all the object information?

Example:

public class Repository() {
    private List<Property> properties; // Assume a constructor exists

    // I'll be using a foreach here
    // property contains location = "Spain" and roomQuantity = 2
    // The rest is null, I want to add to the list properties that are located in spain and contain 2 rooms
    public List getPropertiesList(Property property) {
        List<Property> newList = new ArrayList<>();

        for(Property p : this.properties)
        {
            // Is there any function I can use here to check if it contains the info I'm searching for, not caring if others are different?
            if(p.containsEqualInfo(property))
            {
                newList.add(p);
            }
        }

        return newList;
    }
}

Basically:

I want to search (for example) and return a list of all the properties that are located in Spain and have a number of rooms equal to 2, not caring about the other variables values.

Property1
0
3
30
Portugal
2

Property2
0
3
30
Spain
2

Property3
4
7
120
Spain
2

Property4
0
10
300
Spain
3

If I had the above list inside the repository, once I called the function (getPropertiesList(p), p = the object that only contains spain and 2) the returned list would contain:

Property2 + Property3

  • You'll have to define your own function to do that. – clinomaniac Dec 20 '17 at 18:58
  • There are zillions of ways to do that. The typical approach is that you would define one or more predicates, and then you can use java stream magic to apply those to a list/stream of objects. – GhostCat Dec 20 '17 at 19:00
  • Do I not need to specify the variables I want to use on those lambda things though? I wanted to only use 1 function for any type of search I want. I am new to java so, should I try and explore more that lambda thing? – Gustavo Moreira Vieira Dec 21 '17 at 02:33

0 Answers0