Here fromIndex is inclusive and toIndex is exclusive.
List list = new ArrayList();
List list1 = list.subList(fromIndex,toIndex);
Example:
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<String>();
list.add("Hello1");
list.add("Hello2");
System.out.println("Before Sublist "+list);
List<String> list2 = list.subList(0, 1);
list2.add("Hello3");
System.out.println("After sublist changes "+list);
Output:
Before Sublist [Hello1, Hello2]
After sublist changes [Hello1, Hello3, Hello2]
Here fromIndex is inclusive and toIndex is exclusive.
Set set = new TreeSet();
Set set1 = set.subSet(fromIndex,toIndex);
The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
fromKey is inclusive and toKey is exclusive
Map map = new TreeMap();
Map map1 = map.get(fromKey,toKey);
If fromKey is greater than toKey or if this map itself has a restricted range, and fromKey or toKey lies outside the bounds of the range then it throws IllegalArgumentException.
All the collections support backed collections means changes made on the sub collection will have same change on the main collection.