Java 在庞大的列表中查找元素

Java 在庞大的列表中查找元素,java,list,arraylist,bigdata,Java,List,Arraylist,Bigdata,我有一个巨大的字符串列表(list),其中可能包含超过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 t

我有一个巨大的字符串列表(
list
),其中可能包含超过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;
}
<...>
/**
 * 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;
    }

}
公共类测试{
私有静态最终列表=新的ArrayList();
静止的{
对于(int i=0;i<7;i++){
list.add(Integer.toString(i));
}
}
公共静态void main(字符串[]args){
List in=new ArrayList();
对于(int i=0;i<10;i++)
in.add(Integer.toString(i));
列表输出=FindHatsNotInList(输入);
//打印不以开头的7、8和9字符串
//0、1、2、3、4、5或6(列表'list'中的字符串)
out.forEach(System.out::println);
}
私有静态列表FindHatsNotInList(列表x){
列表结果=新建ArrayList();
for(字符串s:x){
如果(从任何开始,列表))
继续;
结果。添加(s);
}
返回结果;
}
私有静态布尔startsWithAny(字符串s,列表sw){
返回sw.stream().filter(s::startsWith.findAny().orElse(null)!=null;
}
}

你基本上是在问如何最好地重新发明轮子

唯一合理的答案是:不要

意思:你想在“大数据”上实现大规模搜索。我建议您转而研究Solr或ElasticSearch等框架。因为处理大量数据的唯一真正答案是太多地使用“横向扩展”解决方案。做“你自己”是一项严肃的任务

如果您的需求有一点点可能会“增长”,并且需要更复杂的搜索,那么就花精力选择最佳匹配技术。而不是试图建造一些难以建造的东西


上述框架有一定的开销,但如果使用得当,它们可以掌握TB的数据。作为单个开发人员,您所能提供的任何东西都无法接近这一点。在你的路上,你很可能会重复每个人都在犯的错误。或者,如前所述,您可以选择看到此类错误的工具,并在几年前修复它们

您是否考虑过除列表之外的其他数据结构?是否可以将“列表”设置为集合而不是列表。方法contains on list需要O(N)来运行,其中Set有一个O(1)。编辑帖子:我实际上需要通过列表
list
进行流式处理,并使用
startsWith
,而不仅仅是
contains
。很抱歉。对于Starts,您可以先订购数据。