java在一个巨大的列表中查找一个元素
我有一个巨大的字符串列表(List<String>
),其中可能包含超过10.000个唯一元素(字符串),我需要在循环中多次(可能也超过10.000)引用该列表,以确定该列表是否包含一些元素
例如:
/**
* The size of this list might be over 10.000.
*/
public static final List<String> list = new ArrayList<>();
<...>
/**
* The size of the 'x' list might be over 10.000, too.
*
* This method just does something with elements in the list 'x'
* which are not in the list 'list' (for example (!), just returns them).
*/
public static List<String> findWhatsNotInList(List<String> x) {
List<String> result = new ArrayList<>();
for (String s : x) {
if (list.contains(s))
continue;
result.add(s);
}
return result;
}
<...>
根据列表list
和x
的大小,此方法可以执行几分钟,这太长了
有没有办法加快这个过程?(在完全替换List
后,请随意建议任何内容,并使用其他内容循环。)
编辑:尽管使用了List#contains
方法,但我可能需要使用List#stream
并进行一些检查,而不仅仅是String#equals
(例如使用startsWith
)。例如:
/**
* The size of this list might be over 10.000.
*/
public static final List<String> list = new ArrayList<>();
<...>
/**
* The size of the 'x' list might be over 10.000, too.
*
* This method just does something with strings in the list 'x'
* which do not start with any of strings in the list 'list' (for example (!), just returns them).
*/
public static List<String> findWhatsNotInList(List<String> x) {
List<String> result = new ArrayList<>();
for (String s : x) {
if (startsWithAny(s, list))
continue;
result.add(s);
}
return result;
}
<...>
/**
* Check if the given string `s` starts with anything from the list `list`
*/
public boolean startsWithAny(String s, List<String> sw) {
return sw.stream().filter(s::startsWith).findAny().orElse(null) != null;
}
<...>
编辑#2:示例:
public class Test {
private static final List<String> list = new ArrayList<>();
static {
for (int i = 0; i < 7; i++) {
list.add(Integer.toString(i));
}
}
public static void main(String[] args) {
List<String> in = new ArrayList<>();
for (int i = 0; i < 10; i++)
in.add(Integer.toString(i));
List<String> out = findWhatsNotInList(in);
// Prints 7, 8 and 9 — Strings that do not start with
// 0, 1, 2, 3, 4, 5, or 6 (Strings from the list `list`)
out.forEach(System.out::println);
}
private static List<String> findWhatsNotInList(List<String> x) {
List<String> result = new ArrayList<>();
for (String s : x) {
if (startsWithAny(s, list))
continue;
result.add(s);
}
return result;
}
private static boolean startsWithAny(String s, List<String> sw) {
return sw.stream().filter(s::startsWith).findAny().orElse(null) != null;
}
}
共 (0) 个答案