简体   繁体   English

从列表java中获取包含子列表的索引

[英]Get index of contain sublist from list java

I have string lists look like this: 我有字符串列表看起来像这样:

 List<String> parentDataList:  {"this", "is", "a", "test", "string", "and", "a", "test", "other"} 
 List<String> child1:   {"a", "test"}
 List<String> child2:   {"this", "string"} 
 List<String> child3:   {"is", "a", "test"} 

My expectation is that I want to check the parent list has contain sequence children list, then get the start and end indexs in parent list base on child list. 我的期望是我想检查父列表是否包含序列子列表,然后在子列表的父列表中获取开始和结束索引。
From above example: 从上面的例子:

 Parent contain child1 list, and return the indexes: [2 - 3] and [6 - 7]
 Parent doesn't contain child2 list because it isn't sequential.
 Parent contain child3 list, and return the index: [1 - 3] 

I tried using List.containsAll method, but it doesn't care the order of list item, and I can't get start and end index from this method. 我尝试使用List.containsAll方法,但它不关心列表项的顺序,我无法从此方法获取开始和结束索引。
I am looking for the fastest way to do this because my list has many data and I have to search from many input strings. 我正在寻找最快的方法,因为我的列表有很多数据,我必须从许多输入字符串中搜索。
Any help would be appreciated! 任何帮助,将不胜感激!

Update : 更新
I need to get all index of sub lists are contained in parent list. 我需要获取子列表的所有索引都包含在父列表中。 For example, the parent contains child1 in two position: [2 - 3] and [6 - 7] 例如,父级包含两个位置的child1:[2 - 3]和[6 - 7]

The method Collections.indexOfSubList will give you the desired information. Collections.indexOfSubList方法将为您提供所需的信息。

Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. 返回指定源列表中指定目标列表第一次出现的起始位置,如果不存在,则返回-1。 More formally, returns the lowest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such index. 更正式地,返回最低索引i,使得source.subList(i,i + target.size())。equals(target),或者如果没有这样的索引则返回-1。 (Returns -1 if target.size() > source.size().) (如果target.size()> source.size(),则返回-1。)

int index=Collections.indexOfSubList(parentDataList, child1);
…

The index interval will be from index , inclusive, to index+child1.size() , exclusive. 索引间隔将从index (包括index+child1.size()index+child1.size() ,exclusive。 Unless the returned index is -1 , of course. 当然,除非返回的索引是-1 In the latter case the sublist was not found. 在后一种情况下,未找到子列表。

You can change @Alessio's code like this. 您可以像这样更改@ Alessio的代码。 It also works on your cases. 它也适用于您的情况。

public List<Interval> getIntervals(String[] parent, String[] child) {
    List<Interval> intervals = new ArrayList<Interval>();
    Interval interval = new Interval();

    for (int i = 0, j = 0; i < parent.length; i++) {
        if (child[j].equals(parent[i])) {
            j++;
            if (j == 1) {
                interval.start = i;
            }
            if (j == child.length) {
                interval.end = i;
                intervals.add(interval);
                interval = new Interval();
                j = 0;
            }
        } else {
            j = 0;
        }
    }

    return intervals;
}

If you want to do it manually : 如果您想手动执行此操作:

public static List<Interval> getIntervals2(String[] parent, String[] child) {
    List<Interval> intervals = new ArrayList<Launch.Interval>();

    for (int i = 0; i < parent.length; i++) {
        if (child[0].equals(parent[i])) {
            Interval interval = new Interval();
            interval.start = i;
            intervals.add(interval);
        }
    }

    ListIterator<Interval> iterator = intervals.listIterator();
    while (iterator.hasNext()) {
        Interval interval = iterator.next();
        for (int j = 1, i = interval.start + 1; i < child.length; i++, j++) {
            if (!child[j].equals(parent[i]))
                iterator.remove();
        }
        if (interval.start + child.length - 1 < parent.length - 1)
            interval.end = interval.start + child.length - 1;
        else
            iterator.remove();
    }

    return intervals;
}

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

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