Java Data Structures
ArrayLists
ArrayList <String> myArrayList = new ArrayList(); // Create an ArrayList
myArrayList.add("myValue1"); // Add to the end of the list
System.out.println( myArrayList.get(0) ); // Access value at index 0
myArrayList.remove(0); // Remove value at index 0
- An ArrayList is like an array that can change size.
- An ArrayList can contain values of the type specified in the <> brackets (primitive types like 'int' must be replaced with their corresponding class e.g. 'Integer').
- Requires import of java.util.ArrayList
- There are many other methods available for ArrayList objects, youi can find documentation on the internet.
Creating ArrayLists with initial values
ArrayList <Integer> myArrayList = new ArrayList<Integer>( Arrays.asList(62,21,13,45) );
System.out.println( myArrayList.get(2) ); // Access value at index 2
- java.util.Arrays must be imported for this to work.
- This example would display the 3rd value, 13.
More ArrayList Methods
ArrayList <Integer> myArrayList = new ArrayList<Integer>( Arrays.asList(62,21,13,45) );
System.out.println( myArrayList.size() ); // Display how many are in the list
myArrayList.set(2, 15); // Change the value at index 2 to be 15
myArrayList.remove(1); // Remove the value at index 1
System.out.println( myArrayList.indexOf(32) ); // Display the index containing the value 45
System.out.println( myArrayList.contains(13) ); // Check if it contains the value 13
System.out.println( myArrayList.clear() ); // Clear list
System.out.println( myArrayList.isEmpty() ); // Check if empty
- There are still more methods available.
Maps
HashMap <Integer, String> myHashMap = new HashMap<Integer, String>();
myHashMap.put( 12312, "Billy" ); // Add a key, value pair to the map
myHashMap.put( 52342, "Sally" ); // Add another
System.out.println( myHashMap.get(12312) ); // Access entry by key
myHashMap.remove(12312); // Remove entry with specified value
myHashMap.clear(); // Remove all items from map
- A map is like an ArrayList except that you access values by their secified key rather than by the index.
- Maps can be used to access a name associated with an ID or a phone number associated with a name.
- It doesn't have to be a number linked with a string, it could be any two data types (as specified in the < >).
- Must import java.util.HashMap
Sets
HashSet <String> mySet = new HashSet<String>();
mySet.add( "apple" ); // Add a value
mySet.add( "apple" ); // Adding same value will not change the set
System.out.println( mySet.contains("apple") ); // See if item in set
mySet.remove("apple"); // Remove entry with specified value
System.out.println( mySet.size() ); // See how many items in set
mySet.clear(); // Remove all items from set
- A set is a list that contains 1 instance of whatever item you add to it (no duplicates).
- Must import java.util.HashSet
Stacks
Stack <String> myStack = new Stack<String>();
myStack.add( "apple" ); // Add a value/item
System.out.println( myStack.peek() ); // View the last added item
System.out.println( myStack.pop() ); // View and remove the last added item
System.out.println( myStack.size() ); // See how many items in stack
myStack.clear(); // Remove all items from stack
- A stack is a list where each new item is added as if to the top of a pile, so that the last added item can be retrieved easily.
- This is useful when you need to handle tasks in reverse sequence that they were collected, such as for backtracking actions, or parsing a nested structure.
- Must import java.util.Stack
Challenge
Make an ArrayList with 10 student names. Loop through the list, and map the IDs 101 - 110 to the students. Make a function that takes an ID and the map as a parameter and will return the name of the student with the specified ID.
Quiz
Completed