Data Structures In Java

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What are data structures?

Standardized patterns of code designed to organize one or more variables

Talk about maps in Java.

- A map is an object consisting of key:value pairs. - Keys are unique but values don't have to be - Java implements maps through the map interface - the map interface is part of the collections framework but does not implement the collections interface - like all collections framework members, it only stores objects so wrapper classes must be used for primitive variables - Use the map interface in java by using a class that implements it - the HashMap class implements the map interface, does not preserve the order of elements but is fast - the TreeMap class implements the map interface, which is slower but preserves order Code: import.java.util.map import.java.util.hashMap Map<Integer, String> employee = new HashMap<>(); employee.put(234, "Homer Simpson"); employee.put(234, "Homer Simpson"); employee.put(445, "Gordon Freeman"); System.out.println(employee.values()); //Values System.out.println(employee.keySet()); //Keys System.out.println(employee.entrySet()); // Both keys and values combined ------ *** MY CODE For(String v: employee.values() ) { print(v) //print values } For(int key : employee.keySet() ) { String v = m1.get(key); //print values print(v) }

Talk about sets in Java

- A set is a collection that does not allow duplicates, does not preserve the order of insertion, does not support indexing, can only contain 1 null value. - The set interface contains methods from the collection interface - it doesn't support indexing of elements so to access elements, use an iterator, an enhanced for loop, or an API - - To use a set, instantiate a class that implements the set interface such as - HashSet Class implements the set interface and is backed by the HashMap class, which Stores its elements/objects internally. Whenever a HashSet object is created, an associated HashMap is created with key= to the objects value and value = to an object called PRESENT which is a constant. HashSet has no order and no duplicates as well. -TreeSet maintains the order of insertion -LinkedHashSet is an ordered version of HashSet, slowest of the three in general Code Snippet import.java.util.Set; import.java.util.HashSet; Set<String> planets = new HashSet<String>(); planets.add("Jupiter"); planets.add("Mars"); for(String p: planets) { System.out.println(p); } if(planets.contains("Pluto")) { System.out.println("Pluto's included baby! "); }else { System.out.println("Pluto's not included. Womp womp."); }

Talk about arrays

- Arrays are like lists in that they store a collection elements that are ordered, indexable, with duplicates allowed and nulls allowed, but they are fixed in size (not dynamic), can only store elements of a uniform type (ArrayList can be made with type object which takes any kind), and they don't need to store objects, they can store primitive variables without using their wrapper classes - Arrays are a linear collection of variables of fixed size and of uniform type in java (type object) - Arrays reserve a certain amount of memory to fit a predetermined number of variables - Thus array sizes are fixed. - Variables are stored next to each other in memory so searching arrays is very fast b/c the computer knows where to look for the next variable (and not far) - Thus arrays can be accessed using an index

When should you use a list vs a set vs a map ?

- If you don't want duplicate values, use a Set - If you need search/get operations based on index, use a list (ArrayList) - If you need to maintain insertion order, use a List - If you have key, value mappings, use a map

Talk about ArrayLists

- Implements the list interface, is index-based and elements can be accessed using get(j) method, and is like an array except it's resizable (often called a dynamic array), faster than LinkedList - Has properties of list including its ordered, index based, can have duplicates, and is a collection - Elements are actually objects and it cant take primitive types like int, char, boolean (it can take String). You need to specify the wrapper class: Character, Double, Boolean, Integer - You need to specify a wrapper class for primitives: Character, Double, Boolean, Integer - Wrapper classes are objects encapsulating primitive java types so they can be used in classes that only work with objects. The java collection framework only works with objects so primitives must be manually converted to their wrapper classes - it is preferable to store list objects in a list reference variable so if you decide to make changes in the future, its easier, like changing the object from ArrayList to LinkedList code: Integer object = new Integer(1); Code Snippet import java.util.list import java.util.ArrayList List<String> name = new ArrayList<String>(); name.add("Kendra"); name.add("Brittany"); name.get(1); name.remove(0); for(String firstNames: name) { System.out.println(firstNames); } for(int i = 0; i < name.size(); i++) { System.out.println(name.get(i)); } Collections.sort(name); System.out.println(name);

Discuss the differences between Maps, Sets, and Lists in Java.

- List allows duplicate elements, sets must have unique elements, maps have key:value pairs with unique keys but allow duplicate values - Lists allow multiple null values, set allows a single null value, maps can have a single null key but any number of null values in key:value pair - Lists maintain insertion order (ordered collection), Sets don't maintain insertion order (unordered collection), Maps don't maintain insertion order (unordered collection)** sets and maps have some classes w/ order - List and set implement collections interface but map does not. All 3 are part of the collections framework - List, map, and set are all iterable. List and set implement the iterable interface, so can use for loops for iteration (for loop or enhanced for loop for ArrayList, enhanced for loop for sets). Maps provide their own methods for iteration, and can be iterated over keys, values, or key-value pairs via methods keySet(), values(), or entrySet() ** some set and map classes maintain insertion order, set: LinkedHashSet, maps: TreeMap has keys in ascending order, LinkedHashMap has elements in insertion order

Explain the difference between the Stack and Queue in Java

- Stack is a class that extends vector which implements list - Queue is an interface that is implemented by classes like PriorityQueue - Stack is a data structure following the LIFO (last in first out) principle, where insertion and deletion only takes place from one end of the list called the top. Deleting an element via stack.pop() always removes the last element, and inserting an element via stack.push(i) places it at the top of the stack. The top element is always kept track of with a pointer called top. - Queue is a data structure based on the FIFO principle (first in first out). Insertion and deletion takes place from opposite ends (insertion is at the rear of the list and deletion is at the front of the list).

Talk about Lists in Java.

- The list interface stores collections that preserve insertion order, are index based**,can have duplicates, and is a child interface of Collection (**except LinkedLists) - Classes that implement lists include ArrayList, LinkedList, and Vector - Stack extends vector so stack is a list is a collection - it is preferable to store list objects in a list reference variable so if you decide to make changes in the future, its easier, like changing the object from ArrayList to LinkedList LinkedList - LinkedList implements the list interface. It's a collection of linked nodes where a node's location is only known by the node by the node before it and after it - It's comprised of nodes that do not necessarily sit next to each other in memory - It is uni-directional so you must traverse each node in the forward direction only to retrieve a value (doubly linked lists are bi-directions and can be traversed forwards or backwards) - This makes it slower than ArrayList for data retrieval - It maintains order of insertion, it does not support indexing though array list does

Talk about java collections

- a java collection is a unit of objects - the collection framework holds all collection classes & interface - it includes set, list, queue, deque, and map interfaces with classes that implement them (but map doesn't implement collection interface) - it's shown as a hierarchy consisting of interfaces which are implemented by classes - all collections framework classes store objects so wrapper classes must be used for primitive variables integer (Integer class), boolean (Boolean class), etc


Set pelajaran terkait

Medical Surgical Chapter 12 Inflammation and Wound Healing

View Set

CH 56: Management of Patients with Dermatologic Disorders and Wounds

View Set

Chapter 7 Review- Textbook Questions

View Set

Possible Questions for GEOG 271 Test 2

View Set

Unit 2: Quiz 2 - Operations with Complex Numbers

View Set

3510 student questions: Concrete

View Set