Working with Java Collections



Similar documents
Class 32: The Java Collections Framework

JAVA COLLECTIONS FRAMEWORK

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Data Structures in the Java API

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

The Java Collections Framework

Data Structures and Algorithms

Practical Session 4 Java Collections

Basic Programming and PC Skills: Basic Programming and PC Skills:

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Per imparare a programmare bisogna programmare

Software Development with UML and Java 2 SDJ I2, Spring 2010

Java Map and Set collections

D06 PROGRAMMING with JAVA

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Chapter 8: Bags and Sets

core 2 Basic Java Syntax

Génie Logiciel et Gestion de Projets. Refactoring

Java Coding Practices for Improved Application Performance

Data Structures and Algorithms Lists

Principles of Software Construction: Objects, Design and Concurrency. Java Collections. toad Fall 2013

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

AP Computer Science Professional Development Workshop Materials. Special Focus: Using the Java Collections Hierarchy

Introduction: Abstract Data Types and Java Review

LINKED DATA STRUCTURES

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CMSC 202H. ArrayList, Multidimensional Arrays

Lecture 2: Data Structures Steven Skiena. skiena

Data Structures in Java. Session 15 Instructor: Bert Huang

dictionary find definition word definition book index find relevant pages term list of page numbers

Searching Algorithms

Introduction to Stacks

J a v a Quiz (Unit 3, Test 0 Practice)

Java SE 8 Programming

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

TESTING WITH JUNIT. Lab 3 : Testing

cs2010: algorithms and data structures

An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library

Computer. Course Description

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

The Interface Concept

10 Java API, Exceptions, and Collections

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Lecture 7: Class design for security

Computer Programming I

Zabin Visram Room CS115 CS126 Searching. Binary Search

GridWorld AP Computer Science Case Study. Solutions Manual

CS 111 Classes I 1. Software Organization View to this point:

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Introduction to Programming System Design. CSCI 455x (4 Units)

Circular Linked List. Algorithms and Data Structures

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Block IQ. Marko Boon Jacques Resing

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Computer Science 210: Data Structures. Searching

Introduction to Computer Programming (CS 1323) Project 9

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Java Training - A Brief Summary of Objects

Computing Concepts with Java Essentials

Programming with Data Structures

FAQs. This material is built based on. Lambda Architecture. Scaling with a queue. 8/27/2015 Sangmi Pallickara

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Ordered Lists and Binary Trees

CS170 Lab 11 Abstract Data Types & Objects

Core Java+ J2EE+Struts+Hibernate+Spring

A Randomized Dynamic Program Analysis Technique for Detecting Real Deadlocks

Collections Classes for Real-Time and High-Performance Applications. July 4, 2005 Jean-Marie Dautelle

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

1 Hour, Closed Notes, Browser open to Java API docs is OK

Java Software Structures

CSE 143, Winter 2013 Programming Assignment #2: HTML Validator Due Thursday, July 10, 2014, 11:30 PM

Java Programming Language

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Improve your tests with Mutation Testing. Nicolas Fränkel

CmpSci 187: Programming with Data Structures Spring 2015

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Transcription:

Working with Java Collections

Java 2 Collections Lists - linear, supporting many operations Sets - unordered, unique items Sorted sets - like sets, but items can be visited in sorted order

Java 2 Collections Maps - unordered, items accessed by keys Sorted maps - same as maps, but items can be visited in sorted order

Java 2 Collection Interfaces Collection List Set Map SortedSet SortedMap

Java 2 Collection Classes Object AbstractCollection AbstractList AbstractSequentialList AbstractSet AbstractMap ArrayList LinkedList HashSet TreeSet HashMap TreeMap

Lists and Sets

Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); add is supported by most collections

Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i)); size is supported by all collections

Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i)); Iterator iter = set.iterator(); // Display contents of set while (iter.hasnext()) System.out.println(iter.next()); An iterator can be attached to any collection

Transferring Data Set set = new HashSet(list.iterator()); // Transfer to a new set Every collection has a constructor that expects an iterator.

Transferring Data Set set = new HashSet(list); // Transfer to a new set Every collection has a constructor that expects a collection.

Transferring Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.addall(set); // Add all data in set // to list addall is supported by most collections

Filtering Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.removeall(set); // Remove all data from list // that are in set removeall is supported by most collections

Filtering Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.retainall(set); // Remove all data from list // that are not in set retainall is supported by most collections

The Collection Interface boolean add(object o) boolean addall(collection c) void clear() boolean contains(object o) boolean containsall(collection c) boolean equals(object o) int hashcode() boolean isempty() Iterator iterator() boolean remove(object o) boolean removeall(collection c) boolean retainall(collection c) int size() Object[] toarray() Object[] toarray(object[] a)

The Collection Interface boolean add(object o) boolean addall(collection c) void clear() boolean contains(object o) boolean containsall(collection c) boolean equals(object o) int hashcode() boolean isempty() Iterator iterator() boolean remove(object o) boolean removeall(collection c) boolean retainall(collection c) int size() Object[] toarray() Object[] toarray(object[] a) The mutators (in green) are optional operations

Not All Collections Implement Collection Collection List Set Map Tiny SortedSet SortedMap

One-Way Transfers Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection

Collection-View A collection-view is an object that implements the Collection interface has a collection as a backing store which does not implement the collection interface allows clients to use many of the Collection methods with such collections as maps, tinys, etc.

A Collection-View Is Similar to an Iterator backing store collection iterator object client using hasnext, next backing store collection collection-view object client using removeall, retainall, etc. A collection-view allows an object to masquerade as a collection

The Method collectionview Iterator iterator() Collection collectionview() // Returns an iterator // Returns a collection-view Any class that implements collectionview will be compatible with the Collection interface without implementing that interface

Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection

Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection List list3 = new LinkedList(tiny.collectionView()); // OK

Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionview(); // Save a view list.addall(view); view.addall(list); // Add tiny s items to list // Add list s items to tiny

Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionview(); // Save a view list.addall(view); view.addall(list); view.removeall(list); // Add tiny s items to list // Add list s items to tiny // Throws an exception because // TinyIterator does not support // remove