0

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
Community
  • 1
  • 1
TheFlanman91
  • 141
  • 1
  • 2
  • 11
  • While allowing an equals check like the question suggests can work, what happens when you then wish to "search" by title, or description? Overriding the .equals as shown is potentially limiting. – KevinO Apr 18 '16 at 16:44
  • Yes, I agree it is limiting and perhaps not very scalable but for my needs currently, the weblink is all I need to match. It's kind of link a unique identifier. – TheFlanman91 Apr 18 '16 at 16:45
  • Note, in this case, you want `indexOf` instead of `contains` but the idea is exactly the same. – Tunaki Apr 18 '16 at 16:48
  • Maybe you should just use a map like this: Map linkMap = new HashMap<>(); – Ben Apr 18 '16 at 16:53

2 Answers2

1

Check the method indexOf of the Collections API, it's what you're looking for.

Humberto Pinheiro
  • 1,082
  • 1
  • 13
  • 19
  • As far as I know, indexOf requires the whole object to match. So in this case it would return -1. Or am I mistaken? Does indexOf call the overridden equals method when determining the index? – TheFlanman91 Apr 18 '16 at 16:47
  • Yes it uses the overriden equals method, see https://docs.oracle.com/javase/7/docs/api/java/util/List.html#indexOf%28java.lang.Object%29. – Humberto Pinheiro Apr 18 '16 at 16:49
  • Holy Cow so it does. I tested the indexOf method already but that must have been before I created the new equals method. Thank you sir that worked perfectly. – TheFlanman91 Apr 18 '16 at 16:54
1

use indexOf() to get the element index.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Be aware, if you have multiple meaningful equality shops, it returns the first match index.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
  • As far as I know, indexOf requires the whole object to match. So in this case it would return -1. Or am I mistaken? Does indexOf call the overridden equals method when determining the index? – TheFlanman91 Apr 18 '16 at 16:48
  • you can create a `Shop` instance based on your String `webLink`, and checks the instance in List or not, right? – Haifeng Zhang Apr 18 '16 at 16:52