jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 20: Collections Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 jgs Definition § Java Collections is a framework that provides an architecture to store and manipulate the group of objects. § Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs Common methods § public boolean add(E e) § public boolean remove(E e) § public int size() § public void clear() § public boolean contains(Object element) § public boolean isEmpty() § And more …
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs The interface List § List list1= new LinkedList(); § List list4 = new Stack(); § LinkedList list2 = new LinkedList(); § Stack list4 = new Stack();
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs PriorityQueue // PriorityQueue doesn't allow null values to be stored in the queue import java.util.*; public class TestJavaCollection5{ public static void main(String args[]){ PriorityQueue queue=new PriorityQueue(); queue.add(”One"); queue.add(”Two"); queue.add(”Three"); queue.add(”Four"); // Retrieves, but does not remove, the head of this queue System.out.println("head:"+queue.element()); System.out.println("head:"+queue.peek()); // Retrieves and removes the head of this queue queue.remove(); queue.poll(); System.out.println("after removing two elements:"); }
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20 jgs Binary Tree § A non-linear data structure § where each node can have 2 children at most. § keep the tree sorted
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21 jgs Node class Node { public int value; public Node left public Node right; Node(int value){ value = value; left = null; right = null; } }
Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 22 jgs Tree | insert public class Tree { public void insert(Node node, int value) { if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted to left"); node.left = new Node(value); } } else if (value > node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted to right "); node.right = new Node(value); } } }
jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.