Java Collections Overview

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.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
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.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
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.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
תוכנה 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.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Lecture 23:More on Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Road Map CS Concepts Data Structures Java Language Java Collections
Introduction to Collections
Introduction to Collections
Lecture 6: Collections.
Introduction to Collections
Part of the Collections Framework
Java Collections Framework
Welcome to CSE 143!.
Collections in Java The objectives of this lecture are:
Arrays and Collections
Object-Oriented Programming Paradigm
Welcome to CSE 143! Go to pollev.com/cse143.
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Set and Map IS 313,
Introduction to Java Collection
Presentation transcript:

Java Collections Overview 11/11/2018

The Collections Framework Started with Java 1.2 Numerous classes for common data structures Consistent interfaces Common algorithms Iterators All are in package java.util Convenient, interoperable Conversion to/from arrays Easily extendable 11/11/2018

Major Interfaces Collection List Map Set LinkedList, ArrayList implementations Map HashMap, TreeMap implementations Set HastSet, TreeSet implementations 11/11/2018

Interface Collection All lists and sets are subtypes Interface methods: add, clear, contains, get, remove, set, size, toArray All collections store and return Objects Must cast to specific actual type before using Can’t store elementary values (ints, chars, etc.) without wrapping (Integer, Character, etc.) Unlike arrays, where the contents type is declared Fix coming in Java 1.5. 11/11/2018

Lists Sequential access to data Interface List elements have an integer index Interface List Abstract classe AbstractList Concrete classes ArrayList, LinkedList 11/11/2018

Sets Duplicates automatically eliminated (.equals) Subtype (interface) SortedSet maintains an order Concrete implementations: HashSet, TreeSet 11/11/2018

equals Many collections methods depend on equals duplicate checking, containment checking, etc. Objects stored in collection need a proper equals reflexive symmetric transitive 11/11/2018

compareTo Many situations depend on a proper compareTo method Signaled by Comparable interface Should be reflexive transitive anti-symmetric 11/11/2018

Iterators iterator: an object that identifies a position within a collection All collection classes support iterators List iterators: will follow index order Other iterators: either no order guaranteed, or class-dependent Interface Iterator Concrete inner classes usually not visible to user 11/11/2018

Maps Map: association between key and value Main operations put (key, value) get(key) returns value Maps per se do not implement the Collections interface Can get Collections (Set) of the keys and values separately 11/11/2018

Problem-Solving with Collections "Unique" -- think sets "Properties" -- think maps "Order" -- think Comparable and sorting 11/11/2018

Generic Algorithms Class Collections Collections.sort(List) not to be confused with Collection handy static methods Collections.sort(List) Collections.binarySearch(List) Collections.copy ... 11/11/2018

Interoperability Via common methods of the interfaces Via addAll method mycollectionobject.addAll(existingCollection) Via constructor List myList = new ArrayList(existingCollection) 11/11/2018

Interoperability Advice Advice: use the most general type possible Example: instead of ArrayList myList = new ArrayList( ); consider List myList = new ArrayList( ); or even Collection myList = new ArrayList( ); LinkedList myOperation(HashSet s); consider Collection myOperation(Collection s); not always possible 11/11/2018

Arrays Class Arrays has handy methods .sort, etc Interoperating between arrays and collections 11/11/2018

Wrapped Collections (Views) Unmodifiable (Read-only) Protects the collection structure, not the object contents Created by Collections factory methods example: Set myReadOnlySet = Collections.unmodifiableSet(mySet); Synchronized Safe simultaneous access to an object Needed for multi-thread programming 11/11/2018

Generics Coming in Java 1.5 Types as parameters Can specify the types of the objects stored in the collections Greater type-safety Eliminates annoying casts 11/11/2018

Summary Java 1.2 and above has numerous useful collections facilities Great programming convenience Get familiar with them! 11/11/2018