简体   繁体   English

contains 方法与 Java 中的对象列表

[英]contains method with list of objects in Java

I have a list of objects "SaleItem".我有一个对象列表“SaleItem”。 they are all objects of the same class.它们都是同一个 class 的对象。 each object has a String field "name" and an int field "value".每个 object 都有一个 String 字段“name”和一个 int 字段“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:另一种解决方案是从“myList”创建一个新的字符串列表并使用 contains 方法:

    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.作为一个没有受过正规教育的新手,我不知道在这种情况下什么是合适的。

Since SaleItem.getName() returns a string, you should be able to use "contains" method.由于 SaleItem.getName() 返回一个字符串,您应该能够使用“包含”方法。 It seems like you have initialized the ArrayList or the SaleItem object incorrectly.您似乎已错误地初始化了 ArrayList 或 SaleItem object。

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;
    }
}

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
            }

        }
    }
}

A List's .contains method isn't magical, it will generally just loop through the elements checking for equality, O(n) linear performance. List 的.contains方法并不神奇,它通常只会循环检查元素是否相等,O(n) 线性性能。

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.如果您确实期望重复访问并且希望在后续查找中获得比线性性能更好的性能,那么您可能想要构造一个Map<String,SaleItem>或一个Set<String> ,具体取决于您想用它做什么。 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.一旦您需要不区分大小写的匹配,它们必须是带有不区分大小写比较器的TreeMapTreeSet And if you want partial matching (like using String.contains() or a regular expression), you'd want to go back to a linear search.如果你想要部分匹配(比如使用String.contains()或正则表达式),你会想要 go 回到线性搜索。

But don't do any of that unless you have to.但除非你必须这样做,否则不要这样做。 Keep it simple.把事情简单化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM