问题
I have a List
declared as:
List<String[]> arrayList = new ArrayList<>();
This List
contains multiple arrays of String
s.
I need to check if a String[]
which I have is contained in this ArrayList<String[]>
.
I am currently iterating through the ArrayList
and comparing each String[]
with the one I am searching for:
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[]
?
回答1:
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.
Just go for it.
回答2:
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.
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:
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
// 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 HashSet
s if containment testing is the primary concern:
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.
回答4:
List.contains(Object) is broken with lists of arrays, so why don't you use lists of lists?
You can easily convert an array into a list with 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));
来源:https://stackoverflow.com/questions/25420136/the-best-way-to-check-if-liststring-contains-a-string