In an application I'm designing I have a list of custom Objects called Shop
. The Shop class can be seen here.
Shop.java
public class Shop extends Object {
private String title = "";
private List<HashMap<String, String>> branchDetails = new ArrayList<>();
private String description = "";
private String imageLink = "";
private String webLink = "";
public Shop() {
}
}
In my application I create a List<Shop>
object and populate it with 1500 Shop objects.
Now I would like to search through the List and find the index of a Shop with a "Weblink" that matches a string. i.e. I would like to query the List the same way you would a Database. Is there a way to do this without having to build and populate a Database?
I have overridden the equals and hashcode methods in Shop.java and can use the List#contains method to verify the list contains a object with a matching weblink but cannot get the index of that object.
The equals and hashcode methods:
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Shop shop = (Shop) o;
return getWebLink().equals(shop.getWebLink());
}
@Override
public int hashCode() {
return getWebLink().hashCode();
}
To use this I create a new shop with webLink I would like to query
Shop shop = new Shop();
shop.setWebLink("http://somelink.com");
shopsList.contains(shop) //returns true if weblink string matches one from list of shops