Sets, Queues

Data Types and Exceptions in Java

Jim White

Java Developer

Set interface

  • Set also a kind of Collection
    • Does not allow duplicate objects
      • Guarantees each element is unique
    • Set objects are, generally, unordered (therefore have no index)
  • Lists are like a pill box for objects; each object in a specific spot in the box
  • Sets are like a sack holding objects; where objects are randomly held in the sack

Lists are like pill organizers where objects are stored at an index in a specific location

Sets are like sacks holding objects randomly and unordered

1 Images courtesy Wikimedia Commons
Data Types and Exceptions in Java

List vs Set

  • When to use a Set
    • Guarantees uniqueness of its elements
    • Makes them faster for lookups
      • Checking if an object is in a Set
    • More memory efficient the larger the collection grows
    • Good for membership testing in caches
      • Example: finding unique words in a document
Data Types and Exceptions in Java

List vs Set

  • When to use a List
    • Preserves order of elements and allows for duplicates
    • Perform better when accessing elements by position
    • Perform better when adding/deleting elements by index
    • Good where order of elements is important
      • Ex: managing steps of a sequence or songs in a playlist
Data Types and Exceptions in Java

Set implementation

  • Many implementations of Set
    • HashSet a popular implementation
  • HashSet
    • An unordered bag of objects
    • Faster than other Set implementation for insert, delete and lookup searches
    • Generally use more memory that other Set implementations
    • Allow storing a single null

HashSet are unordered bags of objects allowing one null

Data Types and Exceptions in Java

HashSet construction

  • Use generic parameterized constructor to create a HashSet
    HashSet<String> set = new HashSet<String>();
    
  • HashSet is found in java.util package
    • Requires an import
    • import java.util.HashSet
Data Types and Exceptions in Java

HashSet methods

  • Use .add() and .remove() to add, remove objects
  • Use .remove() and then .add() to replace an object
  • Use .contains() to check if object already exists
  • Duplicates are ignored
  • Allows null
  • Ordered not guaranteed
set.add("France");
set.add("Japan");
set.add("Brazil");
set.add("Egypt");
set.add(null); // null is allowed
set.remove("Brazil");
boolean z =
  set.contains("France"); // z is true
set.add("Japan"); // Ignored
System.out.println(set);
[null, Japan, Egypt, France]
Data Types and Exceptions in Java

Queue interface

  • Queue data structure processes objects in a first in, first out (FIFO) order
    • First object added is first removed
    • Like a line at a ticket booth
    • Has a head and tail object

Image of a queue with the head (start) and the tail (end) identified

  • Several implementations of Queue
    • Similar in behavior and operations
Data Types and Exceptions in Java

ArrayBlockingQueue

  • Popular Queue implementation
    • An array under the covers
  • Attention: ArrayBlockingQueue is in java.util.concurrent package
    • Not in java.util package
Data Types and Exceptions in Java

ArrayBlockingQueue construction

  • Use generic parameterized constructor to create a ArrayBlockingQueue
    • Has a capacity or object count limit specified with the constructor
import java.util.concurrent;  // At the top of the class

// Create new queue that can store 4 Strings
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(4);
Data Types and Exceptions in Java

ArrayBlockingQueue methods

  • Use .add(object) or .offer(object) to add to the tail
    • .add(object) throws exception when Queue is at capacity
    • .offer(object) ignores the new object when at capacity
ArrayBlockingQueue<String> queue 
    = new ArrayBlockingQueue<String>(4);
queue.offer("France");
queue.offer("Japan");
queue.offer("Brazil");
queue.offer("Egypt");
queue.offer("China"); // Ignores China

// Causes IllegalStateException
// queue.add("China");

System.out.println(queue);
[France, Japan, Brazil, Egypt]
Data Types and Exceptions in Java

ArrayBlockingQueue methods

  • Use .remove() or .poll() to remove from the head
    • .remove() throws an exception when Queue is empty
    • .poll() returns null when Queue is empty
  • Does not allow null
ArrayBlockingQueue<String> queue 
    = new ArrayBlockingQueue<String>(4);
String x = queue.poll(); // x is null
// Causes NoSuchElementException
// String y = queue.remove();

queue.offer("France");
String next = queue.poll();
System.out.println(next);
France
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...