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.
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 …
The interface List § List <data-type> list1= new LinkedList(); § List <data-type> list4 = new Stack(); § LinkedList <data-type> list2 = new LinkedList(); § Stack <data-type> list4 = new Stack();
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<String> queue=new PriorityQueue<String>(); 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:"); }
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); } } }
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.