collection and collections Flashcards
collection interface
Java standard install, a generic interface called Collection which identifies some common methods for interacting with such data structures
collections class
Java standard install, classcalled Collectionswhich includes some potentially useful static methods that can interact with Collection-based objects
7 of the Collection methods
boolean add(E newVal) void clear() boolean contains(Object ref) boolean remove(Object ref) int size() boolean isEmpty() Object[] toArray()
3 classes that implement Collection
ArrayList, PriorityQueue, Stack
PriorityQueue
removes the smallest item every time you call remove()
handy for printing things smallest to largest:System.out.println(myFloatPQ.remove())
collection interface benefits:
> a method that should be able to operate on multiple objects
>you can write the method to take a reference to any object that implements that interface!
collection interface cons:
Java allows you to access data structures in other ways as well
(less secure)
methods that can take arraylists object’s reference
> ArrayList class implements List which extends Collection > any method which can take either a List or a Collection as a parameter (methods in collection do this)
sort
Collections.sort(myList);
use our compareTomethod as the basis for sorting the list behind the scenes.
min, max
could print the record of the students with the alphabetically smallest and largest name: System.out.println(“Min: “+Collections.min(myList));
System.out.println(“Max: “+Collections.max(myList));
our compareTomethod would be used as the basis for determining this behind the scenes
shuffle
randomly shuffle our list
Collections.shuffle(arrayList);