Introduction to Java Collection

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 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.
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.
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
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.
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.
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.
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.
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,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Chapter 19 Java Data Structures
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Java Collections Overview
Introduction to Collections
Introduction to Collections
Introduction to Collections
Java Collections Framework
14.1 The java.util Package.
Object Oriented Programming in java
Collections James Brucker.
CS2110: Software Development Methods
Introduction to Collections
CSE 1020: The Collection Framework
Introduction to Collections
Intro to Collections.
Part of the Collections Framework
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Introduction to Computer Science
Presentation transcript:

Introduction to Java Collection

Java Collections What are they? Advantages Disadvantages 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 Maps 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

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”, “http://www.google.com”); myMap.put(“yahoo”, “http://www.yahoo.com”);

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 if 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); }