Collections A First Glimpse. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
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.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
15-Jun-15 Lists in Java Part of the Collections Framework.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Java Collection Framework. Interface Collection add(o) Add a new element clear() Remove all elements contains(o) Membership checking. IsEmpty() Whether.
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.
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.
Chapter 19 Java Data Structures
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
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.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
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.
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
Java 2 Collections Bartosz Walter Software Engineering II.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
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.
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.
Collections Dwight Deugo Nesa Matic
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Chapter 19 Java Data Structures
Software Development Java Collections
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Introduction to Collections
Introduction to Collections
Collections A First Glimpse.
CIT Nov-18.
Part of the Collections Framework
Introduction to Collections
Introduction to Collections
Collections A First Glimpse 16-Apr-19.
Presentation transcript:

Collections A First Glimpse

Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection –A linked list is a kind of collection Java 1.2 introduced the Collections Framework and provided many great implementations –Vector s have been redefined to implement Collection –Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection –Arrays do not implement the Collection interfaces

Types of Collection Java supplies several types of Collection : –Set : cannot contain duplicate elements, order is not important –SortedSet : like a Set, but order is important –List : may contain duplicate elements, order is important Java also supplies some “collection-like” things: –Map : a “dictionary” that associates keys with values, order is not important –SortedMap : like a Map, but order is important

Collections are ADTs I’m not going to cover Collections just yet, but I want to use them as an example Collections are one of the best-designed parts of Java, because –They are elegant: they combine maximum power with maximum simplicity –They are uniform: when you know how to use one, you almost know how to use them all –You can easily convert from one to another

Uniformity through interfaces Much of the elegance of the Collections Framework arises from the intelligent use of interfaces For example, the Collection interface specifies (among many other operations): –boolean add(Object o) –boolean isEmpty() –boolean remove() –int size() –Object[] toArray() –Iterator iterator()

Vectors The class Vector has been retrofitted to implement the Collection interface Vector supplies add(Object) and iterator() methods (among others) Let’s look at creating a Vector and iterating through the elements

The Iterator interface interface iterator { // java.lang.util boolean hasNext(); // Returns true if the iteration has more // elements. Object next(); // Returns the next element in the // interation. void remove(); // Removes from the underlying collection // the last element returned by the // iterator (optional operation).

Using a Vector Collection numerals = new Vector(); numerals.add("one"); numerals.add("two"); numerals.add("three"); Iterator iter = numerals.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } Results: one two three

Using a TreeSet Collection numerals = new TreeSet(); numerals.add("one"); numerals.add("two"); numerals.add("three"); Iterator iter = numerals.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } Results: one three two

Conversions Vector v = new Vector(numerals); TreeSet ts = new TreeSet(v);

Back to arrays Arrays are not part of the new Collections Framework, but they haven’t been ignored Java 1.2 introduced the new Arrays class, with some useful operations, for example: –static void sort(Object[] a) –static int binarySearch(Object[] a, Object key) –static boolean equals(Object[] a, Object[] a2) –static void fill(Object[] a, Object val) –static void fill(Object[] a, Object val, int from, int to) –static List asList(Object[] a)

What to remember We haven’t really begun to study the Collections framework yet, but— –You should learn to use the Iterator interface –You should examine and learn more about the Arrays package

The End