How things are related in Collections?
Iterable ← Collection ← List ← Queue ← Set
List → ArayList, LinkedList, Vector ← Stack Set → HashSet, LinkedHashSet SortedSet → TreeSet
What are collections where can we use them in real life (Doing small stuff)
Collections are a represents a groups of same objects. python has list, c++ has vector …etc.
How to do processing on collections
How to traverse over collections ?
ArrayList<String> myCollection = new ArrayList<>();
names.add("Bleh");
names.add("Mary");
names.add("Bleh");
- For Each
for (String x: myCollection) {
System.out.println(x);
}
- Iterator()
for (Iterator<?> it = myCollection.iterator(); it.hasNext(); ){
System.out.println(it.next());
}
Collection Interface Bulk Operations
Bulk operations perform an operation on an entire Collection
. You could implement these shorthand operations using the basic operations, though in most cases such implementations would be less efficient. The following are the bulk operations:
containsAll
— returnstrue
if the targetCollection
contains all of the elements in the specifiedCollection
.addAll
— adds all of the elements in the specifiedCollection
to the targetCollection
.removeAll
— removes from the targetCollection
all of its elements that are also contained in the specifiedCollection
.retainAll
— removes from the targetCollection
all its elements that are not also contained in the specifiedCollection
. That is, it retains only those elements in the targetCollection
that are also contained in the specifiedCollection
.clear
— removes all elements from theCollection
.