Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,

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.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
1 Collections Working with More than One Number or Data Type or Object.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
Java Collections Framework A presentation by Eric Fabricant.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
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 Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
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.
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.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Using the Java Collection Libraries COMP 103 # T2
Java Collections Overview
Introduction to Collections
Introduction to Collections
Introduction to Collections
14.1 The java.util Package.
CS2110: Software Development Methods
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Introduction to Java Collection
Presentation transcript:

Introduction to Java Collection

Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists, Sets, etc. –Part of the java.util package. Advantages –Very flexible, can hold any kind of object Disadvantages –Not as efficient as arrays (for some uses) –Not type-safe. Store references to Object

Java Collections Two Types of Containers Collections –Group of objects, which may restricted or manipulated in some way –E.g. an ordered to make a List or LinkedList –E.g. a Set, an unordered group which can only contain one of each item Maps –Associative array, Dictionary, Lookup Table, Hash –A group of name-value pairs

Java Collections

Several implementations associated with each of the basic interfaces Each has its own advantages/disadvantages Maps –HashMap, SortedMap Lists –ArrayList, LinkedList Sets –HashSet, SortedSet

Java Collections – The Basics HashMap and ArrayList are most commonly encountered Usual object creation syntax Generally hold references to the interface and not the specific collection –Can then process them generically List myList = new ArrayList(); List otherList = new ArrayList(5); Map database = new HashMap(); Set things = new HashSet();

Java Collections – Adding Items For Collections, use add() List myList = new ArrayList(); myList.add(“A String”); myList.add(“Other String”); For Maps, use put() Map myMap = new HashMap(); myMap.put(“google”, “ myMap.put(“yahoo”, “

Java Collections – Copying Very easy, just use addAll() List myList = new ArrayList(); //assume we add items to the list List otherList = new ArrayList(); myList.addAll(myList);

Collections – Getting Individual Items Use get() Note that we have to cast the object to its original type. Collections… String s = (String)myList.get(1); //get second element String s2 = (String)myList.get(10); //get eleventh element Maps… String s = (String)myMap.get(“google”); String s2 = (String)myMap.get(“yahoo”);

Collections – Getting all items For Lists, we could use a for loop, and loop through the list to get() each item But this doesn’t work for Maps. To allow generic handling of collections, Java defines an object called an Iterator –An object whose function is to walk through a Collection of objects and provide access to each object in sequence

Collections – Getting all items Get an iterator using the iterator() method Iterator objects have three methods: –next() – gets the next item in the collection –hasNext() – tests whether it has reached the end –remove() – removes the item just returned Basic iterators only go forwards –Lists objects have a ListIterator that can go forward and backward

Collections – Getting all items Simple example: List myList = new ArrayList(); //we add items Iterator iterator = myList.iterator(); while (iterator.hasNext()) { String s = (String)iterator.next(); //do something with it }

Collections – Other Functions The java.util.Collections class has many useful methods for working with collections –min, max, sort, reverse, search, shuffle Virtually all require your objects to implement an extra interface, called Comparable

Collections – Comparable The Comparable interface labels objects that can be compared to one another. –Allows sorting algorithms to be written to work on any kind of object –so long as they support this interface Single method to implement public int compareTo(Object o); Returns –A negative number of parameter is less than the object –Zero if they’re equal –A positive number if the parameter is greater than the object

Collections – Comparator Like Comparable, but is a stand-alone object used for comparing other objects –Useful when you want to use your criteria, not that of the implementor of the object. –Or altering the behaviour of a system Many of the methods in the Collections object all a Comparator to be specified Again has single method: public int compare(Object obj1, Object obj2)

Collections – Comparator Example Java String comparison is lexicographic not alphabetic, I.e. based on the character set, not alphabetic order public class AlphaComparison implements Comparator { public int compare(Object obj1, Object obj2) { String s1 = ((String)o1).toLowerCase(); String s2 = ((String)o2).toLowerCase(); return s1.compareTo(s2); }