Sometimes it is appropriate to use an immutable empty collection. The Collections class provides methods to get such collections in an efficient way:
List<String> anEmptyList = Collections.emptyList(); Map<Integer, Date> anEmptyMap = Collections.emptyMap(); Set<Number> anEmptySet = Collections.emptySet();
These methods are generic and will automatically convert the returned collection to the type it is assigned to. That is, an invocation of e.g. emptyList() can be assigned to any type of List and likewise for emptySet() and emptyMap().
The collections returned by these methods are immutable in that they will throw UnsupportedOperationException if you attempt to call methods which would change their contents (add, put, etc.). These collections are primarily useful as substitutes for empty method results or other default values, instead of using null or creating objects with new.
List subList(int fromIndex, int toIndex)
Here fromIndex is inclusive and toIndex is exclusive.
List list = new ArrayList(); List list1 = list.subList(fromIndex,toIndex);
Example:
List<String> words = new ArrayList<String>(); words.add("Word1"); words.add("Word2"); System.out.println("Before Sublist Changes: " + words); List<String> subWords = words.subList(0, 1); subWords.add("Word3"); System.out.println("After Sublist Changes: " + words);
Output:
Before Sublist Changes: [Word1, Word2] After Sublist Changes: [Word1, Word3, Word2]
Set subSet(fromIndex,toIndex)
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.
Map subMap(fromKey,toKey)
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.
Sometimes it's not a good practice expose an internal collection since it can lead to a malicious code vulnerability due to it's mutable characteristic. In order to provide "read-only" collections java provides its unmodifiable versions
An unmodifiable collection is often a copy of a modifiable collection which guarantees that the collection itself cannot be altered. Attempts to modify it will result in an UnsupportedOperationException exception.
It is important to notice that objects which are present inside the collection can still be altered.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CustomListManager { private List<Double> doubleList = new ArrayList<>(); public void addValueToDoubleList(Double value) { doubleList.add(value); } public List<Double> getDoubleList() { return Collections.unmodifiableList(doubleList); } }
The following attempt to modify an unmodifiable collection will throw an exception:
import java.util.List; public class MainApp { public static void main(String[] args) { CustomListManager manager = new CustomListManager(); manager.addValueToDoubleList(3.14); List<Double> values = manager.getDoubleList(); // Attempting to modify the list will throw an UnsupportedOperationException values.add(2.718); } }
Output:
Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) at MainApp.main(MainApp.java:9)
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions