The Java Collections Framework (JCF) Introduction and review 1.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
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.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Computer Science 209 Software Development Java Collections.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Using the Java Collection Libraries COMP 103 # T2
Lists A List ADT Types of Lists Using ordered lists – Tournament Maker
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
CSE 373: Data Structures and Algorithms
Welcome to CSE 143!.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Welcome to CSE 143!.
Arrays and Collections
Welcome to CSE 143! Go to pollev.com/cse143.
Collections Framework
CSE 1020: The Collection Framework
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
structures and their relationships." - Linus Torvalds
Java Generics & Iterators
Presentation transcript:

The Java Collections Framework (JCF) Introduction and review 1

CS-2851 Dr. Mark L. Hornick 2 You learned about ArrayList and List in SE1011 and SE1021 ArrayList is one type of Collection class in the java.util package. A Collection represents a group of objects, or elements. Some methods of ArrayList are: boolean add( E o) Adds an object o of type E to the list void clear( ) Clears this list, i.e., make the list empty Object get( int index ) Returns the element at position index boolean remove( int index ) Removes the element at position index int size() Returns the number of elements in the list

CS-2851 Dr. Mark L. Hornick 3 The Java Collections Framework …is implemented as a series of hierarchies with interfaces at the top abstract classes in the middle and fully defined classes at the bottom More than 200 methods in all! The interfaces are:

Review What is an interface? CS-2851 Dr. Mark L. Hornick 4

CS-2851 Dr. Mark L. Hornick 5 Collection is the base interface that many other interfaces, abstract classes, and classes inherit The Collection interface defines certain basic behaviors, such as (to name just a few): add(e) – allows an element to be added to the collection clear() – removes all elements from the collection contains(e) – determines if an element is in the collection isEmpty() – determines if the collection is empty remove(e) – removes a specific element from the collection size() – gets the number of elements in the collection Note: Collection does not define a way to retrieve an element from a collection

CS-2851 Dr. Mark L. Hornick 6 Set, List, and Queue are derived interfaces that define more specific behavior List – an ordered collection, or a sequence that defines specific control over where elements are placed in the collection Set – a collection that does not allow duplicate items SortedSet – self explanatory Queue – an ordered collection supporting a specific way of adding and removing elements from a collection

CS-2851 Dr. Mark L. Hornick 7 The List interface declares methods for inserting, removing and retrieving an element at a certain location in a collection The List interface defines some index-oriented methods, such as: add(index,e) – adds an element at a specific location/index in the list get(index) – retrieve the element at the specific location/index remove(index) – remove the element at the specific location/index set(index, e) – replace the element at the specific location/index

CS-2851 Dr. Mark L. Hornick 8

CS-2851 Dr. Mark L. Hornick 9 JCF Abstract Classes reside between interfaces and collection classes have undefined methods (like an interface) as well as defined methods (like a regular class). What does an abstract class provide that an interface does not? Simple definitions of common methods that need not be overridden in the fully defined subclasses. One example is that a subclass of AbstractList need not override the isEmpty() or size() methods.

CS-2851 Dr. Mark L. Hornick 10 JCF Collection Classes At the bottom of the JCF hierarchy are the fully-defined collection classes (or containers) A container is another name for a class whose instances are collections (of elements) Collection classes implement the interfaces at the lowest level JCF provides (most) common containers with iterators and an assortment of algorithms. InterfaceGeneral-purpose Implementations Hash TableResizable array TreeLinked ListHash Table + Linked List SetHashSetTreeSetLinkedHash Set ListArrayListLinkedList Queue MapHashMapTreeMapLinkedHash Map

CS-2851 Dr. Mark L. Hornick 11 JCF Collection class: ArrayList List salaryList = new ArrayList (); salaryList.add(1000); salaryList.add(0,2000); // add before 1000 salaryList.remove(1); // remove 2 nd element salaryList.clear(1000); // clear list This creates an instance, salaryList, of the ArrayList<> collection class. The elements in salaryList must be (references to) Double objects. The size need not be specified

CS-2851 Dr. Mark L. Hornick 12 JCF Collection class: LinkedList List salaryList = new LinkedList (); salaryList.add(1000); salaryList.add(0,2000); // add before 1000 salaryList.remove(1); // remove 2 nd element salaryList.clear(1000); // clear list This creates an instance, salaryList, of the LinkedList<> collection class. The elements in salaryList must be (references to) Double objects. Does LinkedList sound identical to ArrayList?

So what’s the difference between ArrayList and LinkedList? CS-2851 Dr. Mark L. Hornick 13

CS-2851 Dr. Mark L. Hornick 14 Demo Looking at ArrayList and LinkedList ArrayList LinkedList