andgarrett
andgarrett

Reputation: 55

contains method with list of objects in Java

I have a list of objects "SaleItem". they are all objects of the same class. each object has a String field "name" and an int field "value". I want to see if one of the objects contains a name. It seems that I can't use the "contains" method to do this. I see two solutions. one is to iterate through all the objects to check if one has said name:

    for (SaleItem item: myList) {
        if (item.getName() == "banana") {
            // do stuff
        }
    }

The other solution would be to create a new list of Strings from "myList" and use the contains method on that:

    ArrayList<String> nameList = new ArrayList<>();
    for (SaleItem item: myList) {
        nameList.add(item.getName());
    }
    if (nameList.contains("banana")) {
        // do stuff
    }

I imagine the first method would be most efficient if I'm only doing it once, and the second would be more efficient if I'm doing it many times. Being a bit of a newbie without a formal education, I don't know what's proper in this situation.

Upvotes: 0

Views: 3412

Answers (3)

Izzy
Izzy

Reputation: 238

A List's .contains method isn't magical, it will generally just loop through the elements checking for equality, O(n) linear performance.

Your first solution is probably fine.

If you really did expect repeated access and wanted better than linear performance on subsequent lookups, you'd probably want to construct a Map<String,SaleItem>, or a Set<String> depending on what you wanted to do with it. But those solutions would normally only work on exact matches. Once you need case-insensitive matches, they have to be TreeMap or TreeSet with a case-insensitive comparator. And if you want partial matching (like using String.contains() or a regular expression), you'd want to go back to a linear search.

But don't do any of that unless you have to. Keep it simple.

Upvotes: 0

Staphy Joseph
Staphy Joseph

Reputation: 131

Since SaleItem.getName() returns a string, you should be able to use "contains" method. It seems like you have initialized the ArrayList or the SaleItem object incorrectly.

public class TestApp {
    public static void main(String[] args) {

        List<SaleItem> list = new ArrayList<SaleItem>();
        SaleItem s1 = new SaleItem();
        s1.setName("banana");
        s1.setValue(1);
        SaleItem s2 = new SaleItem();
        s2.setName("apple");
        s2.setValue(2);
        list.add(s1);
        list.add(s2);
        for (SaleItem item: list) {
            if (item.getName().contains("banana")) {
                System.out.println("Pass");
            }
        }
    }

}

class SaleItem {
    private String name;
    private int value;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
}

Upvotes: 2

iamLalit
iamLalit

Reputation: 458

Try with this code

public class SaleItem {

    private String itemName;

    public String getItemName() {
        return itemName;
    }

    public SaleItem setItemName(String itemName) {
        this.itemName = itemName;
        return this;
    }

    @Override
    public String toString() {

        return "[SaleItem : { itemName = " + this.getItemName() + " }]";
    }

    public static void main(String[] args) {
        ArrayList<SaleItem> nameList = new ArrayList<>();
        nameList.add(new SaleItem().setItemName("banana"));
        nameList.add(new SaleItem().setItemName("grape"));
        nameList.add(new SaleItem().setItemName("watermelon"));
        nameList.add(new SaleItem().setItemName("orange"));
        nameList.add(new SaleItem().setItemName("guava"));

        for (SaleItem item : nameList) {
            if (item.toString().contains("banana")) {
                // Do this
            }

        }
    }
}

Upvotes: 0

Related Questions