I want to know: What is a collection in Java?
It's a class that implements java.util.Collection interface.
There's another branch for those that implement java.util.Map.
These are the basis for data structures in Java: List, Set, LinkedList, HashMap, TreeMap, etc.
Collection
is an interface in the Java API, and according to the docs it is...
The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
Common examples of collections are: ArrayList
, HashSet
, LinkedList
, Stack
and Vector
.
Usually an instance of java.util.Collection (although java.util.Map is officially also a part of the collections framework)
Although the Collection interface can be implemented directly, usually client code will use an implementation of one of the sub interfaces: Set, List, Queue / Deque
Here is some sample code (on the left side you will usually see an interface and on the right side an implementation class).
Sets don't store duplicates, all of their elements are unique:
final Set<String> basicSet = new HashSet<String>();
basicSet.add("One");
basicSet.add("Two");
basicSet.add("One");
basicSet.add("Three");
System.out.println(basicSet.toString());
// Output: [Three, One, Two]
// (seemingly random order, no duplicates)
SortedSets are a special case of sets that store elements in a specified order:
final SortedSet<String> sortedSet = new TreeSet<String>();
sortedSet.add("One");
sortedSet.add("Two");
sortedSet.add("One");
sortedSet.add("Three");
System.out.println(sortedSet.toString());
// Output: [One, Three, Two]
// (natural order, no duplicates)
Lists let you store a value multiple times and access or modify insertion order:
final List<String> strings = new ArrayList<String>();
strings.add("Two");
strings.add("Three");
strings.add(0, "One");
strings.add(3, "One");
strings.add("Three");
strings.add(strings.size() - 1, "Two");
System.out.println(strings);
// Output: [One, Two, Three, One, Two, Three]
There is also a practical shorthand for defining a list:
List<String> strings = Arrays.asList("One", "Two", "Three");
// this returns a different kind of list but you usually don't need to know that
etc.
To get a better understanding, read The Collections Trail from the Sun Java Tutorial (online), or Java Generics and Collections by Maurice Naftalin and Philip Wadler
I think this question is best answered in a non-programming sense.
Say you have 5 balls, and you want to move them around easily. You get a bad and place the 5 balls instead of it. The bag acts as a container. You can now move this bag around, and so quite easily the 5 balls move with it.
Simply put, your holding zero or more objects, inside another object for easy retrieval.