• sashi

Java 8 - Collection vs Collections (Advanced)

Updated: Jul 13, 2020

There are many Java interfaces and classes in Java SDK. Some of their names are very similar, and they confuse some people. One example is Collection and Collections.


The Collection is a Java interface and is the root of the collections framework, whereas the Collections are utility class consists of static methods.


Collection

The Collection interface contains around twenty methods; some methods are overriding methods of Java Object class.



Class declaration

public interface Collection<E> extends Iterable<E>


Twenty methods seem like a lot. However, six of them are defined as 'optional operation' which means you do not have to implement these if you do not need to. But you still need to override them by throwing an exception 'UnsupportedOperationException.'


Here are some such operations

boolean add​(E e)

boolean addAll​(Collection<? extends E> c)

void clear​()

boolean remove​(Object o)

boolean removeAll​(Collection<?> c)


One example of a class which does not implement such operations is unmodifiableCollection() wrapper. By throwing the 'UnsupportedOperationException.' for the add, addAll, clear, remove, and removeAll methods, the user can not modify the list.


Extra: Do not read this if you do not want to get confused.

AbstractCollection unanimous class uses the only required operations. See the picture below.

public Collection<V> values() {
        Collection<V> vals = values;
        if (vals == null) {
            vals = new AbstractCollection<V>() {
                public Iterator<V> iterator() {
                    return new Iterator<V>() {
                        private Iterator<Entry<K,V>> i =     
                           entrySet().iterator();
                        public boolean hasNext() {
                            return i.hasNext();
                        }
                        public V next() {
                            return i.next().getValue();
                        }
                        public void remove() {
                            i.remove();
                        }
                    };
                }
                public int size() {
                    return AbstractMap.this.size();
                }
                public boolean isEmpty() {
                    return AbstractMap.this.isEmpty();
                }
                public void clear() {
                    AbstractMap.this.clear();
                }
                public boolean contains(Object v) {
                    return AbstractMap.this.containsValue(v);
                }
            };
            values = vals;
        }
        return vals;
    } 


For the official Collection document, click here

Collections

Collections, on the other hand, is a utility class. They consist of a bunch of static methods. We will not list all the methods here since you can find a list everywhere on the internet. For the official document, click here


Class declaration

public class Collections


Here are some methods often used.

method declarations

synchronizedCollection(Collection<T> c)

synchronizedList(List<T> list)

synchronizedMap(Map<K,V> m)

synchronizedSet(Set<T> s)


These wrapper methods return the synchronized version of the collection and map.

Note that they implement the synchronization using a synchronized keyword with a final instance object.


unmodifiableCollection(Collection<? extends T> c)

unmodifiableList(List<? extends T> list)

unmodifiableMap(Map<? extends K,? extends V> m)

unmodifiableSet(Set<? extends T> s)


These wrapper methods return the read-only version of the collection and map.

Please see the Collection interface section above for the implementation example.






18 views0 comments