![]() They greatly simplify some common tasks, reduce the number of times when repacking is needed and also reduce the amount of code to type. I tried to mention most popular and useful views in Java Collections Framework. Of course, you should no longer hold or use the reference to the original collection because it would allow unsynchronized access to it. SortedSet synchronizedSortedSet(SortedSet s) Įvery method of synchronized view is synchronized which make the view thread-safe. SortedMap synchronizedSortedMap(SortedMap m) Similarly to unmodifiable views we can create synchronized views using methods:Ĭollection synchronizedCollection(Collection c) It starts with the keyword for like a normal for-loop. It is still possible to change it using the original reference. Prerequisite: Decision making in Java For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java5. However, it is still possible to modify the elements inside the collection.Ĭreating unmodifiable view does not make the original collection unmodifiable. The Java collections framework is a set of classes and interfaces for implementing a commonly used collection data structure. This kind of behaviour is very useful if we want to ensure that given method will not modify the collection. If somebody tries to add or remove elements from the view, it will throw an exception. SortedSet unmodifiableSortedSet(SortedSet s) SortedMap unmodifiableSortedMap(SortedMap m) Unmodifiable viewsĬlass Collection provides methods which create immutable view for many collections types: They also return views instead of real collections which make them very efficient. You should be probably aware of keySet(), entrySet() and values() methods of Map interface. There are even more such methods in NavigableSet and NavigableMap interfaces. SortedSet subSet(E fromElement, E toElement) The similar functionality is also possible on SortedSet using methods: adding or removing elements), will also affect the original list. The view is also mutable so any modification on the view (e.g. The returned view contains 5 elements of the original list between index 5, inclusive, and 10, exclusive. We can also create a view of a portion of a list: List months = Arrays.asList("July", "August") Since Java 5 it is also possible to use varargs in asList() method: But it is still possible to modify the elements inside the view using get() or set() methods. List monthList = Arrays.asList(monthArray) įor obvious reasons the returned collection is immutable which means it is impossible to add or remove elements from it. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). All elements in the list must implement the Comparable interface. If you ever needed to repack elements from an array into a list just to call a single method, you may appreciate asList() method from Arrays class which creates a list view backed by an array: Sorts the specified list into ascending order, according to the natural ordering of its elements. Additionally, the views does not have the overhead of a typical collection and are easier to create. The created collections are immutable similarly to empty views. List nTimesList = Collections.nCopies(9, "elem") It is also possible to create a list which contains specified element given number of times: Map oneMap = Collections.singletonMap("one", 1) Set oneSet = Collections.singleton("elem") List oneList = Collections.singletonList("elem") We can create such list, set or map by calling singletonList(), singleton() or singletonMap() methods respectively: ![]() ![]() Very often we need a collection with only one element. However, this kind of empty collection is very convenient if some API requires a collection but for some reason we don’t want to pass any objects there. The returned instance is actually immutable so trying to add an element into it will result in an exception. List clearList = Collections.emptyList() In Collections class you can find emptyList(), emptySet() and emptyMap() methods which create empty instance of List, Set or Map respectively: To start with views let’s take a look at the simplest ones which represent empty collections. In fact, view does store objects inside but references another collection, array or a single object and uses it to provide the data to a user. Public interface Collection extends in Java Collections Framework is a lightweight object which implements Collection or Map interface but is not a real collection in a traditional sense. ("List after deleting first and last: " + list) ("Contents of list after deletion: " + list) Print out the list, after removing two elements. Inserts the specified element at position with index 1. Add the specified element to the beginning of this list. Add the specified element to the end of this list.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |