How to find a sublist in a List?
SolutionFollowing example uses indexOfSubList() & lastIndexOfSubList() to check whether the sublist is there in the list or not & to find the last occurance of the sublist in the list.
import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six one three Four".split(" ")); System.out.println("List :"+list); List sublist = Arrays.asList("three Four".split(" ")); System.out.println("SubList :"+sublist); System.out.println( "indexOfSubList: " + Collections.indexOfSubList(list, sublist)); System.out.println( "lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); } }Result
The above code sample will produce the following result.
List :[one, Two, three, Four, five, six, one, three, Four] SubList :[three, Four] indexOfSubList: 2 lastIndexOfSubList: 7
java_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4