Object Oriented Programming and Data Abstraction Rowan University Earl Huff.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Concrete collections in Java library
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
CE203 - Application Programming Autumn 2013CE203 Part 41 Part 4.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Lecture 3 Introduction to Collections Advanced Java Programming 1 dr inż. Wojciech Bieniecki
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Professor Evan Korth (adapted from Sun’s collections documentation)
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
CS 602 Collections Arathi Bhandari. Overview A Collection is a container that groups similar elements into an entity. Examples would include a list of.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data structures and algorithms in the collection framework 1 Part 2.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
111 © 2002, Cisco Systems, Inc. All rights reserved.
COLLECTIONS Byju Veedu s/Collection.html.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
University of Limerick1 Collections The Collection Framework.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Marcus Biel, Software Craftsman
Chapter 19 Java Data Structures
Collections Not in our text.
Collections Framework
Programming Languages
Sets and Maps Chapter 9.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Object Oriented Programming and Data Abstraction Rowan University Earl Huff

A collection is an object which groups multiple elements into a single unit. Collections store, retrieve, manipulate and communicate data. Represent items from a natural group (i.e. a poker hand, phone directory, student record system.

A unified architecture for representing and manipulating collections. All frameworks contain: Interfaces – allow collections to be manipulated independently of the details of their representation. Implementations – concrete implementations of collection interfaces. Algorithms – methods performing computations. They are polymorphic; can be used on many different implementations of interface.

Benefits of the Java Collections Framework include Reduces programming effort Increases program speed and quality Allows interoperability among unrelated APIs Reduces effort to learn and to use new APIs Fosters software resuse

CollectionSetSortedSetListQueueDeque MapSortedMap

The root interface in the collection hierarchy. Used to pass around collections of objects where generality is desired. All collection implementations have a constructor that’s a Collection argument called the conversion constructor. Initialize the new collection containing all elements in specified collection, whatever the collection’s implementation or subinterface type.

Collection c; //could be List, Set, or another type //of collection List list = new ArrayList (c);

Basic Methods size() isEmpty() contains(Object element) add (E element) remove(Object element) iterator() Bulk Methods containsAll() addAll() removeAll() retainAll() clear() Array Methods toArray()

Three ways to traverse collections: Aggregate Operations For-each loop Interators

myShapesCollection.stream().filter(e -> e.getColor() == Color.RED).forEach(e-> System.out.println(e.getName()));

for (Object o : collection) { System.out.println(o); }

Iterator it = c.iterator(); while (it.hasNext()) { if (cond(it.next())) it.remove(); }

A Set is a kind of Collection that does not contain duplicate elements. Models the mathematical set abstraction. Contains only methods inherited from Collection and adds the restriction of prohibiting duplicate elements. Places strong stipulation on contract of all constructors and the add, equals, and hashCode methods. Three implementations HashSet – best performing TreeSet – slower than Hashset but faster than LinkedHashSet LinkedHashSet – a hashtable with a linked list running through it.

A list is an ordered Collection (sometimes called a sequence). A list can have duplicate elements. Elements in a list can be accessed by their integer index. Elements in a list can be searched for.

In addition to methods inherited from Collection, the List interface includes methods for: Positional access – get, set, add, addAll, and remove Search – indexOf and lastIndexOf Iteration - listIterator

A Queue is a collection for holding elements prior to processing. In addition to basic Collection operations, queues provide operations for insertion, removal, and inspection. Each Queue method has two forms: One throws an exception if the operation fail. One returns either null or false if the operation fails.

Throws Exception add(e) remove() element() Returns Special Value offer(e) poll() peek()

Queues typically, but not necessarily, order elements in a FIFO (first-in-first-out) manner. Priority queues are the exception. Regardless of the ordering, the head of the element is the element which will be removed by a call to remove() or poll(). offer and add will add an element to the queue if possible. remove and poll will remove and return the head of the queue if possible. element and peek will return, but not remove, the head of the queue.

Pronounced “deck” A double-ended-queue A linear collection that supports insertion and removal of elements at both end points. Implements both Stack and Queue at the same time. Methods are provided to insert, remove, and examine elements as well as access elements at both ends. Implementations of Deque are ArrayDeque and LinkedList

Deque methods are divided into three parts: Insert addFirst(e), offerFirst(e), addLast(e), offerLast(e) Remove removeFirst(), pollFirst(), removeLast(), pollLast() Examine/Retrieve getFirst(), peekFirst(), getLast(), peekLast()

A Map is an object that maps keys to values. A key is a unique index to a value. Only one key per value. A Map cannot contain duplicate keys. Implementations of Map HashMap TreeMap LinkedHashMap Behavior and performance are analogous to their Set counterparts.

Basic Operations put, get, remove, containsKey, containsValue, size, empty Bulk Operations putsAll, clear Collection Views keySet, values, entrySet

Provides a natural ordering for class so objects can be sorted. Lists (and arrays) of objects implementing the interface is sorted automatically by Collections.sort (and Arrays.sort). Highly recommended that natural orderings are consistent with the equals method. The natural ordering for a class C is consistent with equals if and only if e1.compareTo(e2) == 0 has the same Boolean value as e1.equals(e2) for every e1 and e1 of class C.

Compares the receiving object with the invoking object. Returns: 0 – they are equal Positive integer – invoking object is greater than Negative integer – invoking object is less than A ClassCastException is thrown if invoking object cannot be compared to receiving object.

You can write your own Comparable types You can override equals* and compareTo methods *If you override equals, you must also redefine hashCode method. Equal objects must have equal hash codes! Comparator Interface Alternative to Comparable Uses a different kind of ordering for a collection of objects. Consist of one method: compare(T o1, To2)

SortedSet – a set that maintains its elements in ascending order, sorted to either the elements’ natural ordering or to a Comparator. SortedMap – a map that maintains its entries in ascending order of the keys… Both add methods, in addition to their respective superclass methods, for the following operations: Range view – arbitrary range operations on the sorted collection Endpoints – returning first or last element in the sorted collection Comparator access – return the Comparator used to sort the set

SortedSet Range-view subSet(E fromElement, E toElement) headSet(E toElement) tailSet(E fromElement) Endpoints E first() E last() Comparator access Comparator comparator() SortedMap Range-view subMap(K formKey, K toKey) headMap(K toKey) tailMap(K fromKey) Endpoints K firstKey() K lastKey() Comparator access Comparator comparator()

In most applications, it will be necessary to obtain input from the user. Input can come from a variety of sources (i.e. keyboard input, text file, etc.) Java provides a variety of tools to read data from different input sources. Focus will be on keyboard input for now.

Reader class is the root class for the hierarchy of readers for reading character streams. The only methods the subclasses can override are the variations of read() and close(). Notable subclasses BufferedReader, FileReader, InputStreamReader All readers can be fond in the Java.io package

BufferedReader classes allows for reading text from a character-input stream. Creates a buffer, providing efficient reading of characters, arrays, and lines. Ideal to wrap a BufferedReader around other Readers to avoid costly read() operations. For keyboard input, often users wrap BufferedReader around InputStreamReader, which takes System.in (the standard keyboard input stream) as an argument for its constructor)

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parsetInt(in.readLine());

Scanner is the alternative, though more often used, method to reading user input. Can be found in the Java.util package. Performs the same way and at the same level of efficiency of BufferedReader. Difference is that Scanner can parse primitive types and strings with the use of regular expressions. (i.e. nextInt(), nextLong(), etc.) Scanner can break input into tokens using a delimiter pattern

Scanner sc = new Scanner(System.in); int i = sc.nextInt();