Collection details in Java
How can you get synchronized standard collection objects?
Using the static methods synchronizedMap() and synchronizedList() of the Collections class. These methods return the synchronized decorator of the passed collection. However, manual synchronization is still required in the case of a collection traversal.
Map m = Collections.synchronizedMap(new HashMap());
List l = Collections.synchronizedList(new ArrayList());
Since Java 6, JCF has been extended with special collections that support multithreading, such as CopyOnWriteArrayList and ConcurrentHashMap.
How do I get a read-only collection?
With help:
- Collections.unmodifiableList(list);
- Collections.unmodifiableSet(set);
- Collections.unmodifiableMap(map).
These methods take a collection as a parameter and return a read-only collection with the same elements inside.
Single threaded program that causes the collection to throw a ConcurrentModificationException.
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
for (Integer integer : list) {
list.remove(1);
}
}
Give an example where a collection is throwing an UnsupportedOperationException.
public static void main(String[] args) {
List<Integer> list = Collections.emptyList();
list.add(0);
}
Implementation the symmetric difference of the two collections using the Collection (addAll(...), removeAll(...), retainAll(...)) methods.
The symmetric difference of two collections is the set of items that do not belong to both of the original collections at the same time.
<T> Collection<T> symmetricDifference(Collection<T> a, Collection<T> b) {
// Merge collections.
Collection<T> result = new ArrayList<>(a);
result.addAll(b);
// Get the intersection of the collections.
Collection<T> intersection = new ArrayList<>(a);
intersection.retainAll(b);
// Delete the items located in both collections.
result.removeAll(intersection);
return result;
}
How to make cache with "invalidation policy" using LinkedHashMap?
It is necessary to use LRU (Least Recently Used algorithm) and LinkedHashMap with access-order. In this case, when accessing an element, it will move to the end of the list, and the least used elements will gradually group at the beginning of the list. Also in the standard implementation of LinkedHashMap there is a removeEldestEntries() method that returns true if the current LinkedHashMap object should remove the least used element from the collection using the put() and putAll() methods.
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private static final int MAX_ENTRIES = 10;
public LRUCache(int initialCapacity) {
super(initialCapacity, 0.85f, true);
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > MAX_ENTRIES;
}
}
It is worth noting that LinkedHashMap does not allow you to fully implement the LRU algorithm, since when you insert an element already in the collection, the order of iteration over the elements does not change.
How to copy the elements of any collection into an array in one line?
Object[] array = collection.toArray();
How to get a List with all the elements except the first and last 3 in one call from List?
List<Integer> subList = list.subList(3, list.size() - 3);
How can I convert a HashSet to an ArrayList in one line?
ArrayList<Integer> list = new ArrayList<>(new HashSet<>());
How to convert ArrayList to HashSet in one line?
HashSet<Integer> set = new HashSet<>(new ArrayList<>());
Make a HashSet from the keys of the HashMap.
HashSet<Object> set = new HashSet<>(map.keySet());
Make HashMap from HashSet <Map.Entry<K, V>>.
HashMap<K, V> map = new HashMap<>(set.size());
for (Map.Entry<K, V> entry : set) {
map.put(entry.getKey(), entry.getValue());
}
Read also:
Comments
Post a Comment