©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.

Slides:



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

JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
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.
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.
Professor Evan Korth (adapted from Sun’s collections documentation)
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 8.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on 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.
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 Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
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.
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.
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.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  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.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
AD Lecture #3 Java Collection Framework 1.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
Design & Analysis of Algorithm Map
Chapter 19 Java Data Structures
Software Development Java Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Programming & Data Structures
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
Java Collections Overview
CS313D: Advanced Programming Language
Introduction to Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Introduction to Collections
Java Collections Framework
Collections in Java The objectives of this lecture are:
Collections Not in our text.
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
CS 240 – Advanced Programming Concepts
structures and their relationships." - Linus Torvalds
Introduction to Java Collection
Presentation transcript:

©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double doubleValues[] = { 8.4, 9.3, 0.2, 7.9, 3.4 }; private int filledInt[], intValuesCopy[]; public Example() { filledInt = new int[ 10 ]; intValuesCopy = new int[ intValues.length ]; Arrays.fill( filledInt, 7 ); // fill with 7s Arrays.sort( doubleValues ); // sort doubleValues ascending // copy array intValues into array intValuesCopy System.arraycopy( intValues, 0, intValuesCopy, 0, intValues.length ); } Use static method fill of class Arrays to populate array with 7 s Use static method sort of class Arrays to sort array’s elements in ascending order Use static method arraycopy of class System to copy array intValues into array intValuesCopy

©Karsten Lundqvist The Java Collections Framework The Java collections framework is made up of a set of interfaces and classes for working with groups of objects The Java Collections Framework provides Interfaces: abstract data types representing collections. Implementations: concrete implementations of the collection interfaces. Algorithms: methods that perform useful computations, like searching and sorting, on objects that implement collection interfaces. More information: 2

©Karsten Lundqvist JCF A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Set: a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction. List: an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List. Queue: a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations. (E.g., a FIFO queue) Map: an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map. 3

©Karsten Lundqvist Collections 4

©Karsten Lundqvist Maps 5

©Karsten Lundqvist Interface Collection 6 // Basic Operations size():int; isEmpty():boolean; contains(Object):boolean; add(Object):boolean; // Optional remove(Object):boolean; // Optional iterator():Iterator; // Bulk Operations containsAll(Collection):boolean; addAll(Collection):boolean; // Optional removeAll(Collection):boolean; // Optional retainAll(Collecton):boolean; // Optional clear():void; // Optional // Array Operations toArray():Object[]; toArray(Object[]):Object[]; Collection Found in the java.util package Optional methods throw UnsupportedOperationException if the implementing class does not support the operation. Bulk operations perform some operation on an entire Collection in a single shot The toArray methods allow the contents of a Collection to be translated into An array.

©Karsten Lundqvist Interface Set A Set is a Collection that cannot contain duplicate elements. Set models the mathematical set abstraction. The Set interface extends Collection and contains no methods other than those inherited from Collection It adds the restriction that duplicate elements are prohibited. Two Set objects are equal if they contain the same elements. 7

©Karsten Lundqvist Interface List Elements in a list are ordered By insertion sequence, if nothing else By magnitude, alphabetical order, etc. A list can contain duplicate entries e.g., a list of purchased items can include the same item more than once. 8 // Positional Access get(int):Object; set(int,Object):Object; // Optional add(int, Object):void;// Optional remove(int index):Object; // Optional addAll(int, Collection):boolean;// Optional // Search int indexOf(Object); int lastIndexOf(Object); // Iteration listIterator():ListIterator; listIterator(int):ListIterator; // Range-view List subList(int, int):List;

©Karsten Lundqvist Interface Queue A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Deque: A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" 9 ValueThrows Exception Returns special value Insert : add(e) offer(e) Remove : remove() poll() Examine : element() peek()

©Karsten Lundqvist Interface Map A map is a set of pairs each pair represents a one-directional “mapping” from one object (the key) to another (the value) The typical use of a Map is to provide access to values stored by key. Some examples: A map of student IDs to database records A dictionary (words mapped to meanings) The conversion from base 2 to base 10 Names to phone numbers 10 … Object k,v;  clear() Remove all mappings  containsKey(k) Whether contains a mapping for k  containsValue(v) Whether contains a mapping to v  entrySet() Set of key-value pairs  get(k) The value associated with k  isEmpty() Whether it is empty  keySet() Set of keys  put(k,v) Associate v with k  remove(k) Remove the mapping for k  size() The number of pairs  values() The collection of values

©Karsten Lundqvist Concrete Collections concrete implements description collection HashSet Set hash table TreeSet SortedSet balanced binary tree ArrayList List resizable-array LinkedList List linked list Vector List resizable-array HashMap Map hash table TreeMap SortedMap balanced binary tree Hashtable Map hash table ArrayDequeDequedouble ended resizable-array 11

©Karsten Lundqvist Concrete Collections 12 InterfaceImplementationHistorical SetHashSetTreeSet ListArrayListLinkedList Vector Stack DequeArrayDequeLinkedList MapHashMapTreeMapHashTable Properties Note: When writing programs think about interfaces and not implementations. This way the program does not become dependent on any added methods in a given implementation, leaving the programmer with the freedom to change implementations.

©Karsten Lundqvist ArrayList public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.) 13    

©Karsten Lundqvist LinkedList public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, Serializable Linked list implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue. The class implements the Deque interface, providing first-in-first-out queue operations for add, poll, along with other stack and deque operations. All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index. 14  first last

©Karsten Lundqvist Ordering and Sorting There are two ways to define orders on objects. Each class can define a natural order among its instances by implementing the Comparable interface. int compareTo(Object o) Arbitrary orders among different objects can be defined by comparators, classes that implement the Comparator interface. int compare(Object o1, Object o2) Use the sort method in the Class Collections: Collections.sort(myConcreteCollection); Collections.sort(myConcreteCollection, myCompatarator); Other methods include: shuffle, reverse, fill, binarySearch, frequency... 15