Collections Flashcards
Packages
Packages organize Java classes and create scope to prevent two classes with the same name from having a name collision.
Fully qualified Class name
Is the package name along with the class name.
java. util.Scanner in = new java. util.Scanner(System.in);
Collections
Are classes in java.util package that define data structures that make working with a set of data easier.
Collections are written to be generic so that they are useful for all reference types.
List List variableName = new ArrayList();
Is an interface, unlike a class contains no code and just creates a data type with guaranteed functionality.
Boxing
Boxing is moving a primitive value from the Stack to a Wrapper Class object on the Heap.
Primitive → Wrapper Class
Unboxing
Unboxing is moving the value from a Wrapper Class on the Heap to an appropriate primitive value on the Stack.
Wrapper Class → Primitive type
Stack
A data structure that organizes data Last In First Out (LIFO). Optimizes Insertion and Deletion, but not possible to search.
For each Loop characteristics
- Loops through each item in a collection or array in order.
- Cannot modify the collection or array in the loop
- Does not know the iteration.
Queue
A data structure that organizes data First in First Out (FIFO). Optimizes Insertion and Deletion, but not possible to search.
List characteristics
- Maintains the Order of Insertion.
- Elements can be accessed by index.
- Allows duplicate elements.
- Can hold only one data type that is defined by reference type.
- Can grow and shrink as items are added and removed.
- An ordered set of elements that is 0-indexed like an array.
Stack methods
push( x ) - insert at the beginning of the Stack.
pop() - remove items from the beginning of the Stack.
peek() - to look at the next item without removing it.
isEmpty() - used to determine if the Queue has remaining items.
Queue methods
offer( x ) - insert at the end of the Queue
poll() - remove items from the beginning of the Queue
peek() - used to look at the next item without removing it
isEmpty() - used to determine if the Queue has remaining items
List variableName = new ArrayList();
Sets the Reference Data Type that the list will hold.
ArrayList List variableName = new ArrayList();
Is a class that implements the List data type, and will be instantiated by the new keyword.
List methods
size(), add( x ), add( index, x), get( index ), remove( index )