CodesJava

Easy learning with example program codes

How to get last index of a sub list from another list?


The java.util.Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other odds and ends.

Collections.lastIndexOfSubList () method will returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.
Syntax: public static int lastIndexOfSubList(List source, List target)

Example

package com.codesjava;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class Test { 
public static void main(String a[]){         
        List<String> list1 = new ArrayList<String>();
        list1.add("vikas");
        list1.add("anil");
        list1.add("amit");
        list1.add("mahesh");
        list1.add("perl");
        List<String> list2 = new ArrayList<String>();
        list2.add("amit");
        list2.add("mahesh");
        System.out.println("Last index of list2: "
                    +Collections.lastIndexOfSubList(list1, list2));
    }
}

Output

Last index of list2: 2

Java Collections class examples



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap