检查List 是否包含String [] [duplicate]的最佳方法

时间:2020-12-19 01:33:32

This question already has an answer here:

这个问题在这里已有答案:

I have a List declared as:

我将List声明为:

List<String[]> arrayList = new ArrayList<>();

This List contains multiple arrays of Strings.

此List包含多个字符串数组。

I need to check if a String[] which I have is contained in this ArrayList<String[]>.

我需要检查我的String []是否包含在此ArrayList 中。

I am currently iterating through the ArrayList and comparing each String[] with the one I am searching for:

我目前正在迭代ArrayList并将每个String []与我正在搜索的String []进行比较:

for(String[] array: arrayList){
    if(Arrays.equals(array, myStringArray)){
        return true;
    }
}
return false;

Is there any better way to check if an ArrayList<String[]> contains a specific String[]?

有没有更好的方法来检查ArrayList 是否包含特定的String []?

4 个解决方案

#1


9  

Array.equals() is the most efficient method afaik. That method alone meant for the purpose and optimized as less as it is in the current state of implementation which is a single for loop.

Array.equals()是最有效的方法。该方法单独用于此目的并且在当前实现状态(即单个for循环)中进行优化。

Just go for it.

只是去吧。

#2


6  

I agree with the answer from Rod_Algonquin, but there is another way to do it. Just write your own class wrapping your array and implement a custom equals and hashCode method and let them return Arrays.equals() and Arrays.hashCode(). With this approach you can store your objects in a List and do contains checks on the list directly.

我同意Rod_Algonquin的答案,但还有另一种方法可以做到。只需编写自己的包装数组的类并实现自定义的equals和hashCode方法,并让它们返回Arrays.equals()和Arrays.hashCode()。使用此方法,您可以将对象存储在List中,并直接对列表进行检查。

List<ArrayWrapper> list = new ArrayList<ArrayWrapper>();
list.add(new ArrayWrapper(new String[]{"test", "123"}));
list.add(new ArrayWrapper(new String[]{"abc", "def"}));
list.add(new ArrayWrapper(new String[]{"789", "cgf"}));

String[] arrayToSearchFor = {"test", "123"};
ArrayWrapper wrapperToSearchFor = new ArrayWrapper(arrayToSearchFor);
System.out.println(list.contains(wrapperToSearchFor));

String[] arrayToSearchFor2 = {"hello", "123"};
ArrayWrapper wrapperToSearchFor2 = new ArrayWrapper(arrayToSearchFor2);
System.out.println(list.contains(wrapperToSearchFor2));


class ArrayWrapper
{
    private String[] array;

    public ArrayWrapper(String[] array)
    {
        this.array = array;
    }

    public String[] getArray()
    {
        return array;
    }

    @Override
    public int hashCode()
    {
        return Arrays.hashCode(array);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (!(obj instanceof ArrayWrapper))
        {
            return false;
        }

        return Arrays.equals(array, ((ArrayWrapper) obj).getArray());
    }
}

this will print

这将打印

true
false

#3


1  

I'd probably look for a solution where you don't store String[] objects directly in your list. That'd probably mean creating some kind of meaningful class that might store a String[] internally, or it might mean just switching to ArrayLists instead of arrays. Without knowing the context, though, the best I can suggest is wrapping them with lists using Arrays.asList

我可能会寻找一个解决方案,你不直接在列表中存储String []对象。这可能意味着创建一些可能在内部存储String []的有意义的类,或者它可能意味着只是切换到ArrayLists而不是数组。但是,如果不知道上下文,我可以建议的最好的方法是使用Arrays.asList将它们包含在列表中

// Make a List that uses the provided array for its contents:
List<String> stringList = Arrays.asList(stringArray);

This gives you useful hashCode and equals methods, which allows you to use ArrayList.contains, or even HashSets if containment testing is the primary concern:

这为您提供了有用的hashCode和equals方法,它们允许您使用ArrayList.contains,甚至包含HashSets,如果包含测试是主要关注点:

Set<List<String>> stringLists = new HashSet<>();

// when you want to add a String[]:
stringLists.add(Arrays.asList(stringArray));

// when you want to check whether a String[] is in the set:
stringLists.contains(Arrays.asList(stringArray));

ArrayList.contains won't be any faster than what you're currently doing, and all the Arrays.asList calls are likely to be quite wordy, but HashSet.contains has the potential to be much faster than what you're doing.

ArrayList.contains不会比你当前正在做的更快,并且所有Arrays.asList调用都可能非常冗长,但HashSet.contains有可能比你正在做的更快。

#4


1  

List.contains(Object) is broken with lists of arrays, so why don't you use lists of lists?

List.contains(Object)打破了数组列表,为什么不使用列表列表呢?

You can easily convert an array into a list with Arrays.asList(T... a).

您可以使用Arrays.asList(T ... a)轻松地将数组转换为列表。

String[] array = new String[2];
array[0] = "item1";
array[1] = "item2";
List<List<String>> arrayList = new ArrayList<List<String>>();
arrayList.add(Arrays.asList(array));

#1


9  

Array.equals() is the most efficient method afaik. That method alone meant for the purpose and optimized as less as it is in the current state of implementation which is a single for loop.

Array.equals()是最有效的方法。该方法单独用于此目的并且在当前实现状态(即单个for循环)中进行优化。

Just go for it.

只是去吧。

#2


6  

I agree with the answer from Rod_Algonquin, but there is another way to do it. Just write your own class wrapping your array and implement a custom equals and hashCode method and let them return Arrays.equals() and Arrays.hashCode(). With this approach you can store your objects in a List and do contains checks on the list directly.

我同意Rod_Algonquin的答案,但还有另一种方法可以做到。只需编写自己的包装数组的类并实现自定义的equals和hashCode方法,并让它们返回Arrays.equals()和Arrays.hashCode()。使用此方法,您可以将对象存储在List中,并直接对列表进行检查。

List<ArrayWrapper> list = new ArrayList<ArrayWrapper>();
list.add(new ArrayWrapper(new String[]{"test", "123"}));
list.add(new ArrayWrapper(new String[]{"abc", "def"}));
list.add(new ArrayWrapper(new String[]{"789", "cgf"}));

String[] arrayToSearchFor = {"test", "123"};
ArrayWrapper wrapperToSearchFor = new ArrayWrapper(arrayToSearchFor);
System.out.println(list.contains(wrapperToSearchFor));

String[] arrayToSearchFor2 = {"hello", "123"};
ArrayWrapper wrapperToSearchFor2 = new ArrayWrapper(arrayToSearchFor2);
System.out.println(list.contains(wrapperToSearchFor2));


class ArrayWrapper
{
    private String[] array;

    public ArrayWrapper(String[] array)
    {
        this.array = array;
    }

    public String[] getArray()
    {
        return array;
    }

    @Override
    public int hashCode()
    {
        return Arrays.hashCode(array);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (!(obj instanceof ArrayWrapper))
        {
            return false;
        }

        return Arrays.equals(array, ((ArrayWrapper) obj).getArray());
    }
}

this will print

这将打印

true
false

#3


1  

I'd probably look for a solution where you don't store String[] objects directly in your list. That'd probably mean creating some kind of meaningful class that might store a String[] internally, or it might mean just switching to ArrayLists instead of arrays. Without knowing the context, though, the best I can suggest is wrapping them with lists using Arrays.asList

我可能会寻找一个解决方案,你不直接在列表中存储String []对象。这可能意味着创建一些可能在内部存储String []的有意义的类,或者它可能意味着只是切换到ArrayLists而不是数组。但是,如果不知道上下文,我可以建议的最好的方法是使用Arrays.asList将它们包含在列表中

// Make a List that uses the provided array for its contents:
List<String> stringList = Arrays.asList(stringArray);

This gives you useful hashCode and equals methods, which allows you to use ArrayList.contains, or even HashSets if containment testing is the primary concern:

这为您提供了有用的hashCode和equals方法,它们允许您使用ArrayList.contains,甚至包含HashSets,如果包含测试是主要关注点:

Set<List<String>> stringLists = new HashSet<>();

// when you want to add a String[]:
stringLists.add(Arrays.asList(stringArray));

// when you want to check whether a String[] is in the set:
stringLists.contains(Arrays.asList(stringArray));

ArrayList.contains won't be any faster than what you're currently doing, and all the Arrays.asList calls are likely to be quite wordy, but HashSet.contains has the potential to be much faster than what you're doing.

ArrayList.contains不会比你当前正在做的更快,并且所有Arrays.asList调用都可能非常冗长,但HashSet.contains有可能比你正在做的更快。

#4


1  

List.contains(Object) is broken with lists of arrays, so why don't you use lists of lists?

List.contains(Object)打破了数组列表,为什么不使用列表列表呢?

You can easily convert an array into a list with Arrays.asList(T... a).

您可以使用Arrays.asList(T ... a)轻松地将数组转换为列表。

String[] array = new String[2];
array[0] = "item1";
array[1] = "item2";
List<List<String>> arrayList = new ArrayList<List<String>>();
arrayList.add(Arrays.asList(array));