Home  >  Core Java

Example of Collections.indexOfSubList in Java

By Arvind Rai, May 18, 2013
Collections.indexOfSubList in java checks the index of a sub list in the parent list. To get index, java uses "brute force" technique. If the sub list has more than one index in the source list, then the lowest index is returned. Find the example
CollectionsIndexOfSubListDemo.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsIndexOfSubListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("AAAA");
        list.add("BBBB");
        list.add("CCCC");
        list.add("DDDD");
        list.add("EEEE");
        List<String> subList = Arrays.asList("CCCC", "DDDD");
        int index = Collections.indexOfSubList(list,subList);
        System.out.println("Index of sublist:" + index);
    }
}
Output
Index of sublist:2
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us