145 lines
4.6 KiB
Markdown
145 lines
4.6 KiB
Markdown
# Java Collections
|
|
|
|
Week 3 (Oct 5th)
|
|
|
|
**Part 1**
|
|
|
|
In Java, a *collection* is an object that represents a group of objects.
|
|
The collections API is a unified framework for representing and manipulating collections independently of their implementation.
|
|
|
|
An *API* (application programming interface) is an interface protocol between a client and a server, intended to simplify the client-side software.
|
|
|
|
A *library* contains reusable chunks of code.
|
|
|
|
**Java Collections framework**
|
|
|
|
- We have container objects that contain objects
|
|
- All containers are either "collections" or "maps"
|
|
- All containers provide a common set of method signatures, in addition to their unique set of method signatures
|
|
|
|
*Collection* - Something that holds a dynamic collection of objects
|
|
|
|
*Map* - Defines mapping between keys and objects (two collections)
|
|
|
|
*Iterable* - Collections are able to return an iterator object that can scan over the contents of a collection one object at a time
|
|
|
|
NOTE: Vector is a legacy structure in Java replaced with *ArrayList*.
|
|
|
|
Stack is now *ArrayDeque*.
|
|
|
|
`LinkedList(Collection<? extends E> c)` - means some type that either is E or a subtype of E. The `?` is a wildcard.
|
|
|
|
```java
|
|
public static void main(String[] args) {
|
|
LinkedList list = new LinkedList();
|
|
list.add("string");
|
|
String s = (String)list.getFirst();
|
|
System.out.println(s);
|
|
}
|
|
```
|
|
|
|
This is bad coding practice: the collection constructor does not specify the type of objects the collection is intended to contain. A `ClassCastException` will be thrown if we attempt to cast to the wrong type.
|
|
|
|
```java
|
|
public static void main(String[] args) {
|
|
LinkedList<String> list = new LinkedList<>();
|
|
list.add("string");
|
|
String s = list.getFirst();
|
|
System.out.println(s);
|
|
}
|
|
```
|
|
|
|
This is a type-safe collection using generics.
|
|
|
|
- Classes support generics by allowing a type variable to be included in their declaration.
|
|
- The `<>` indicate the same type as stated (in this case `String`)
|
|
- You cannot type a collection with a primitive data type, e.g. `int`
|
|
|
|
**HashMap Class**
|
|
|
|
- A HashMap is a hash-table-based implementation of the map interface. This implementation provides all of the optional map operations and permits null values and the null key.
|
|
|
|
```java
|
|
public static void main(String[] args) {
|
|
HashMap<String, Integer> userData = new HashMap<>();
|
|
userData.put("Emma", 30);
|
|
userData.put("John", null);
|
|
userData.put("Millie", 17);
|
|
|
|
Set<String> keys = userData.keySet();
|
|
|
|
for (String key:keys){
|
|
System.out.println(key + "=" userData.get(key));
|
|
}
|
|
}
|
|
```
|
|
|
|
```
|
|
Emma = 30
|
|
John = null
|
|
Millie = 17
|
|
```
|
|
|
|
**Part 2**
|
|
|
|
__Relationships between objects__
|
|
|
|
*Aggregation* - The object exists outside the other. It is created outside so it is passed as an argument.
|
|
An animal object *is part of* a compound object (semantically), but the animal object can be shared. If the compound object is deleted, the animal object isn't deleted.
|
|
|
|
```java
|
|
public class Compound {
|
|
private Animal dog;
|
|
|
|
public void setAnimal(Animal dog){
|
|
this.dog = dog;
|
|
}
|
|
}
|
|
```
|
|
|
|

|
|
|
|
*Composition* - The object only exists if the parent object exists. If the parent object is deleted, then so is the child object.
|
|
The zoo object owns the compound object. If the zoo object is deleted then the compound object is also deleted.
|
|
|
|
```java
|
|
public class Zoo {
|
|
private Compound dogArea = new Compound();
|
|
}
|
|
```
|
|
|
|

|
|
|
|
**Inheritance**
|
|
|
|
A way of forming new classes based on existing classes. Has an "is-a" relationship.
|
|
|
|
*Polymorphism* - A concept in object-oriented programming. Method overloading and method overriding are two types of polymorphism.
|
|
|
|
- *Method Overloading* - Methods with the same name co-exist in the same class but they must have different method signatures. Resolved during compile time (static binding).
|
|
- *Method Overriding* - Methods with the same name are declared in parent and child classes. Resolved at run time (dynamic binding).
|
|
|
|
```java
|
|
public class Child extends Parent {
|
|
public Child(String name){
|
|
super(name);
|
|
}
|
|
|
|
@Override
|
|
Public void eat() {
|
|
chew();
|
|
}
|
|
}
|
|
```
|
|
|
|
The `super` keyword calls the parent class's constructor.
|
|
|
|

|
|
|
|
**What is the difference between an abstract class and an interface?**
|
|
|
|
- A Java abstract class can have instance methods that implement a default behaviour. It may contain non-final variables.
|
|
- Java interfaces have methods that are implicitly abstract and cannot have implementations. Variables are declared final by default.
|
|
|
|
Interfaces are less restrictive when it comes to inheritance: interfaces can have many levels of inheritance, whereas a class can only have one level.
|