A collection is an object that represents a group of objects. Collections are like arrays but much more powerful.
There are three main types of collection:
* Lists - like arrays
* Sets - like lists but with duplicate values not allowed
* Maps - key, value pairs: like dictionaries in python
You will mostly find yourself using ArrayLists and HashMaps
Note that Java collections use templates to show what sort of objects they contain, so List\<String\> is a list of strings and List\<Integer\> is a list of integers.
Here's an example of creating and printing an ArrayList
```java
ArrayList<String> names = new ArrayList<String>();
names.add("Alice");
names.add("Bob");
names.add("Celia");
//Traverse ArrayList using for each loop
for(String name: names) {
System.out.println(name);
}
```
# Really Useful Things You Can Do with Collections
## Sort a List into Order/Reverse order
```java
List<String> list = new ArrayList();
list.add("Smith, J");
list.add("Jones, B");
list.add("Anderson, L");
list.add("Smethwick, P");
list.add("Williams, L");
System.out.println("Sorted Alphabetically! ");
Collections.sort(list);
for(String s: list){
System.out.println(s);
}
System.out.println(" And now in Reverse order! ");
Collections.reverse(list);
for(String s: list) {
System.out.println(s);
}
```
## Shuffle a List
```java
String [] names = {"Mr Ballantyne", "Ms Wright","Mr Kelly",
"Mr Lightfoot","Mr Mahmood","Mrs Lamb"};
List<String> list = Arrays.asList(names);
Collections.shuffle(list);
System.out.println(list);
```
## Make a Deck of Cards and Shuffle it
```java
String[] suit = new String[]
{"spades", "hearts", "diamonds", "clubs"};
String[] rank = new String[]
{"ace","2","3","4","5","6","7","8",
"9","10","jack","queen","king"};
List<String> deck = new ArrayList<String>();
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < rank.length; j++)
deck.add(rank[j] + " of " + suit[i]);
Collections.shuffle(deck);
```
## Count the Frequency of Words in a String
note: this can look confusing until you realise that the String is stored as the Key, the frequency as the Value. This might be the opposite of what you expect!
```java
Map<String, Integer> m = new HashMap<String, Integer>();
String poem = "please please me oh yeah like I please you";
String [] words = poem.split("\\s+");
for (String a : words) {
Integer freq = m.get(a);
m.put(a, (freq == null) ? 1 : freq + 1);
}
System.out.println(m.size() + " distinct words:");
System.out.println(m);
```
## Find the Items Two Collections Have in Common
```java
String [] cheese = {"Cheddar","Cheshire","Wensleydale","Stilton","Edam"};
String []shopping = {"Soap","Beans","Eggs","Cheddar","Yoghurt","Cheshire"};
List<String>commonKeys = new ArrayList<String>(Arrays.asList(cheese));
commonKeys.retainAll(Arrays.asList(shopping));
for (String s : commonKeys){
System.out.println(s);
}
```
## Find the Items that Aren't in Another Collection
```java
String [] cheese = {"Cheddar","Cheshire","Wensleydale","Stilton","Edam"};
String []shopping = {"Soap","Beans","Eggs","Cheddar","Yoghurt","Cheshire"};
List<String>diffKeys = new ArrayList<String>(Arrays.asList(cheese));
diffKeys.removeAll(Arrays.asList(shopping));
for (String s : diffKeys){
System.out.println(s);
}
```
# Collections Exercise
1. Create an ArrayList containing the days of the week. Print out the contents of the list.
2. Create an ArrayList containing 5 names. Print out the contents of the list in random order
3. Create a HashMap that can be contains the following musical terms. Key = "Allegro" Value = "At a Brisk Speed"; Key ="Andante" Value = "Moderately Slow"; Key = "Presto" Value = "In a quick tempo"
4. Extend question 3 so that when the user inputs the key term, the value is output
5. Write a pupil shuffler. This is a program that allows a text file of pupil names to be read in and then output in random order
6. Write a class that asks the user to input five numbers and then prints them out in ascending order.
7. A Happy Families deck of cards contains Mister Baker, Mrs Baker, Master Baker and Miss Baker. It also contains the families for Butcher, Teacher, Greengrocer and Java Programmer. Write a class prints out all the cards in a random order
8. Write a class that reads in a text file and outputs the frequency of words in the string
9. Joe's favourite games are Halo 4, GTA 5, Assassin's Creed 2 and Sonic the Hedgehog. Jill's are GTA 5, Halo 3 and Command and Conquer. Write a class that finds which games they both like.