1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/* Get Sub List of Java ArrayList Example This Java Example shows how to get sub list of java ArrayList using subList method by providing start and end index. */ import java.util.ArrayList; import java.util.List; public class GetSubListOfJavaArrayListExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("1"); arrayList.add("2"); arrayList.add("3"); arrayList.add("4"); arrayList.add("5"); /* To get a sub list of Java ArrayList use List subList(int startIndex, int endIndex) method. This method returns an object of type List containing elements from startIndex to endIndex - 1. */ List lst = arrayList.subList(1,3); //display elements of sub list. System.out.println("Sub list contains : "); for(int i=0; i< lst.size() ; i++) System.out.println(lst.get(i)); /* Sub List returned by subList method is backed by original Arraylist. So any changes made to sub list will also be REFLECTED in the original Arraylist. */ //remove one element from sub list Object obj = lst.remove(0); System.out.println(obj + " is removed from sub list"); //print original ArrayList System.out.println("After removing " + obj + " from sub list, original ArrayList contains : "); for(int i=0; i< arrayList.size() ; i++) System.out.println(arrayList.get(i)); } } /* Output would be Sub list contains : 2 3 2 is removed from sub list After removing 2 from sub list original ArrayList contains : 1 3 4 5 */ |
very useful site…thanks a lot….it helped me a lot…
Awesome
Rocks!….
Good subject to know. I didnt know till now there is subList method.
Really informative and helpful..this section covers all aspects of collecion framework..really thank u
To remove the duplicate objects in ArrayList:
HashSet hs=new HashSet(AL1);
ArrayList AL2=new ArrayList(hs);
It removes all the duplicate elements..
How to get list from two entity class using fetch group data nucleus method.?