ArrayList<Employee> staff = new ArrayList<Employee>();
var staff = new ArrayList<Employee>();
// diamond syntax
ArrayList<Employee> staff = new ArrayList<>();
staff.ensureCapacity(100);
ArrayList<Employee> staff = new ArrayList<>(100);
int sz = staff.size();
staff. trimToSize();
1
2
3
Employee e = staff.get(i);
// use set only to replace a previously added element.
staff.set(i, harry);
1
2
var a = new X[staff.size()];
staff.toArray(a);
1
2
3
int n = staff.size() / 2;
staff.add(n, e);
Employee e = staff.remove(n);
1
2
3
4
5
6
7
for (Employee e : staff)
do something with e
for (int i = 0; i < staff.size(); i++) {
Employee e = staff.get(i);
do something with e
}
1
2
3
4
5
6
7
8
9
10
public class EmployeeDB {
public void update(ArrayList list) { . . . }
public ArrayList find(String query) { . . . }
}
// pass a typed array list without any casts.
ArrayList<Employee> staff = . . .;
employeeDB.update(staff);
// Using a cast and SuppressWarnings
@SuppressWarnings("unchecked") ArrayList<Employee> result = (ArrayList<Employee>) employeeDB.find(query);
Linked Lists
1
2
3
4
5
var staff = new LinkedList<String>();
staff.add("Amy");
staff.add("Bob"); staff.add("Carl");
Iterator<String> iter = staff.iterator();
String first = iter.next(); // visit first element String second = iter.next(); // visit second element iter.remove(); // remove last visited element
1
2
ListIterator<String> iter = staff.listIterator();
iter.next(); // skip past first element iter.add("Juliet");
1
2
3
ListIterator<String> iter = staff.listIterator();
String oldValue = iter.next(); // returns first element
iter.set(newValue); // sets first element to newValue
1
boolean isContain = staff.contains("Harry")
1
2
3
String obj = list.get(n); // this method is not very efficient.
// You should never use this illusory random access method to step through a linked list.
// programmers don’t usually use linked lists in situations where elements need to be accessed by an integer index.
1
2
3
4
5
6
ListIterator<String> iter = staff.listIterator();
int previous = iter.previousIndex()
int next = iter.nextIndex()
String elem = iter.next()
String elem = iter.previous()
1
2
while (iter.hasNext()) {}
while (iter.hasPrevious()) {}
Hash Sets
1
2
3
4
5
6
7
8
9
var words = new HashSet<String>();
String word = “word”;
words.add(word);
Iterator<String> iter = words.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println(words.size() + " distinct words. ");
Tree Sets
1
2
3
4
5
var parts = new TreeSet<Item>();
parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912));
var sortByDescription = new TreeSet<Item>(Comparator.comparing(Item::getDescription));
sortByDescription.addAll(parts);
1
2
3
4
5
6
7
8
9
10
11
public class Item implements Comparable<Item> {
private String description;
private int partNumber;
public int hashCode() {
return Objects.hash(description, partNumber);
}
public int compareTo(Item other) {
int diff = Integer.compare(partNumber, other.partNumber);
return diff != 0 ? diff : description.compareTo(other.description);
}
}
Queues and Deques
1
2
3
4
5
6
7
8
boolean add(E element)
boolean offer(E element)
E remove()
E poll()
E element()
E peek()