Computer Science 209 Software Development Java Collections.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Computer Science 209 Software Development Equality and Comparisons.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
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.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on 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.
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (JCF) Introduction and review 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Data structures and algorithms in the collection framework 1.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
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.
Computer Science 209 Software Development Inheritance and Composition.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
University of Limerick1 Collections The Collection Framework.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
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.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Using the Java Collection Libraries COMP 103 # T2
Chapter 19 Java Data Structures
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Software Development Java Collections
Software Development Inheritance and Composition
Java Collections Overview
Introduction to Collections
Introduction to Collections
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
slides created by Alyssa Harding
Presentation transcript:

Computer Science 209 Software Development Java Collections

Collections A collection is a container for 0 or more objects Organized in a specific manner (list, stack, queue, set, map/dictionary, tree, graph) Operations include insert, remove, access, size, iteration, etc. Arrays are too restrictive, more like an implementation structure; collections are smarter objects

The java.util Package View documentation or download from Oracle’s Web site Includes docs for all interfaces, classes, and methods

Interfaces An interface specifies the behavior of a set of implementing classes Really just a name and a set of method headers The behavior is abstract and conceptually the same, regardless of how it is realized in an implementing class

Example: Lists The List interface includes the methods add, remove, get, and set, among many others The ArrayList and LinkedList classes implement List, so the above methods can be run, in the same manner, with either class of list You just study the interface for the logical behavior, then choose an implementing class based on its performance characteristics

Java Collection Interfaces > Collection > Iterable > List > Set > Map > SortedMap > SortedSet > Queue = extends

Java Collection Classes – Lists, Stacks, Queues > Collection > Iterable > List > Queue LinkedListArrayListVector Stack Abstract Collection AbstractList AbstractSequentialList = extends = implements

Java Collection Classes - Sets > Collection > Iterable > Set HashSetTreeSet > SortedSet Abstract Collection AbstractSet = extends = implements

Java Collection Classes - Maps > Map > SortedMap AbstractMap TreeMapHashMap = extends = implements

Declaration and Instantiation List names = new ArrayList (); List students = new LinkedList (); // Now use the same methods with both lists Always use an interface name for the type of a collection variable The type parameter (in red) restricts the type of objects that can go into a collection Select the methods to use from the interface

All Collections Are Iterable List names = new ArrayList (); List students = new LinkedList (); // Now use the same methods with both lists for (int i = 1; i <= 10; i++) students.add(new Student("Name" + i, 3)); for (Student s : students) System.out.println(s);

Syntax of Parameterized Collections Variable declaration: interface-name variable-name; List names; Set statementHandles; Object instantiation: variable-name = class-name (); names = new ArrayList (); statementHandles = new HashSet ();

Collections of Numbers are Funky List evens = new ArrayList (); for (int i = 1; i <= 10; i++) evens.add(i * 2); for (int i : evens) System.out.println(i); Need to use a wrapper class for the element type when it’s a primitive type Java wraps values before insertion and unwraps them before access

Lists vs Queues // List methods (among many others, including Collection) public E get(int index) public E remove(int index) public E set(int index, E newElement) // Queue methods (as well as Collection) public E peek() public E remove() public boolean add(E newElement) > Collection > Iterable > List > Queue LinkedList

Use Queue to Restrict Access // All List and Collection methods apply List list = new LinkedList (); // Only queue and Collection methods apply Queue queue = new LinkedList (); > Collection > Iterable > List > Queue LinkedList

Multiple Type Parameters Variable declaration: interface-name variable-name; SortedMap concordance; Object instantiation: variable-name = class-name (); concordance = new TreeMap (); The keys are all strings and the values are all integers

The Collection Interface > Collection > Iterable > List > Set > SortedSet > Queue A list, queue, set, or sorted set can be used wherever a collection is specified

Type Conversion List listOfNames = new ArrayList (); // Add a bunch of names to the list in random order // Now copy the names to a new sorted set (sorts them // and removes duplicates) SortedSet setOfNames = new SortedSet (listOfNames); Collection classes that implement Collection usually include a constructor with a Collection parameter!

The java.util.Collections Class Like the Arrays class, includes class methods for processing collections that implement the List interface (array lists, linked lists, stacks) Sorting, searching, find the maximum element, etc. Some methods assume compareto for collection elements Collections.sort( ) Collections.binarySearch( )