1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
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.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
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.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
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
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
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.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Collections Dwight Deugo Nesa Matic
C19: Collection Classes (don’t forget to look at all the online code examples)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Java Collections CHAPTER 3-February-2003
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
TCSS 143, Autumn 2004 Lecture Notes
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Welcome to CSE 143!.
ArrayLists 22-Feb-19.
Collections Framework
Web Design & Development Lecture 6
Presentation transcript:

1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply wrapper classes. 5. Apply string manipulation with String and StringBuffer classes.

2 Data structures  A data structure is a collection of data that can be handled altogether using a single name, eg.  bitwise operation on an integer, an object, an array.  Common data structures 1.List  Maintain data items with a predefined order oThe order may be alphabetical order, or simply the order of the items entering the list ( eg. a queue )  Usually an index is assigned to each item in the list for accessing purpose 2.Stack  Maintain data items in a special order - LIFO (like a pile of files in that the last file you place on the pile is the first one you can get) (pp4, fig-10.1)

3 3.Tree  Maintain structured data that are hierarchical  Each tree contains storage called nodes (represented as rectangles in diagram), and the tree starts with a root node (hence it is actually an inverted tree ).  The nodes are interconnected with pointers (arrows) from parent node to child node.  If a node has at least one child node, it is called an internal node. Otherwise it is a leaf node (i.e. no child).  If each node of a tree can have at most 2 nodes, it is called a binary tree.  A binary tree is a good data structure for storing and searching a collection of orderable data items (e.g. numbers, names, etc.) oe.g. How to store the numbers 45, 60, 57, 23, 10, 70, 31 in a tree structure that can facilitate searching? (pp5, fig-10.2) (pp6, fig-10.3)

4 4.Set  It is a collection of unique elements.  a set is like a list but no elements can be duplicated. 5.Map  The objects maintained by a list object can be accessed by using an index (i.e. a value of primitive type int in Java).  Actually objects in a collection ( value objects) can be specified or accessed by using another object ( key objects). The relationship between the key and value is a mapping and this structure is called a map. oeg. a Student object can be accessed by using the student name (which is a String object in Java) in a collection.  a map is like a list but its index can be any object instead of an integer.

5 Collections  Java provides implementation of these frequently used data structures in the java.util package.  These data structures have the common purpose to maintain a collection of data items (although by different means). So most of them are defined as subclasses of the interface Collection.  The Collection interface defines the baseline functionality a data structure or collection must have.  Then specific Java interfaces, like List and Set, are implemented on top to define the baseline behaviours each kind of data structure should have.  concrete classes of data structure are built from these interfaces to define all abstract methods in a way appropriate to the data structure itself.

6 The Collection interface  The Collection interface is the base class for all classes that implement data structures.  Commonly used (abstract) methods public boolean add (Object obj) public void clear ( ) public boolean contains (Object obj) public boolean isEmpty ( ) public boolean remove (Object obj) public int size ( ) public Object[ ] toArray ( )

7 The List interface  List interface is a subinterface of Collection interface  All implementation classes of the list data structure are the subclasses of this List interface. They can maintain a collection of objects and the order among them. And it is possible to access each object by an index.  A list can has any number of objects, and its storage/size can be extended whenever needed (unlike array viz. is fixed )  Additional commonly used (abstract) methods: public void add(int index, Object element) public Object remove(int index) public Object get(int index) public void set(int index, Object element) public int indexOf(Object element) public int lastIndexOf(Object element) (pp10)

8 Implementation class of the List interface  There are several implementation classes of the List interface in the java.util package, e.g. ArrayList, LinkedList, Vector.  All these classes have implemented all the abstract methods defined in Collection and List.  The add ( ) method of the ArrayList class implicitly appends the object to the end of the list.  Hence the implicit predefined ordering of an ArrayList object is the entry sequence  order of adding the elements to it.  Read the example TestArrayList.java  eg. java TestArrayList guys (pp11)

9 import java.util.*; public class TestArrayList { public static void main(String[] args) { // Check program parameter if (args.length < 1) {// Show usage System.out.println("Usage: java TestArrayList "); } else {// Prepare a List object (a ArrayList object) List objList = new ArrayList(); objList. add (0, "Hello ");// Add element to the List object objList. add (1, args[0]); objList. add (2, ". It is now "); objList. add (3, new Date()); objList. add (4, "."); // Show the objects maintained by the List one by one for (int i=0; i < objList.size(); i++) { System.out.print(objList.get(i)); } System.out.println(); }

10 The Set interface  Set interface is a subinterface of Collection interface.  All implementation classes of the set data structure are the subclasses of this Set interface.  They can maintain a collection of unique objects with no ordering among them. Implementation class of the Set interface  There are several implementation classes of the Set interface in java.util, e.g. TreeSet, HashSet.  All these classes have implemented all the abstract methods defined in Collection and Set.  Read the example TestHashSet1.java (pp14)

11 import java.util.*; public class TestHashSet1 { public static void main(String[] args) { // Create a Set object Set objSet = new HashSet(); // Add objects to the set objSet.add("Hello"); objSet.add("World"); objSet.add("Hello");// this entry will be ignored objSet.add("World");// this entry will be ignored // Obtain an array object from the Set object Object[] elements = objSet.toArray(); // Show the elements maintained by the Set object for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); }

12 The Map interface  Map object maintains 2 sets of data items :  a key set maintaining all key objects and  a value set maintaining all value objects.  it maintains the mapping between these 2 sets, so that by providing a key object the corresponding value object can be retrieved.  Commonly used (abstract) methods: public Object put(Object key, Object value) public Object get(Object key) public Object remove(Object key) public Set keySet( ) public Collection values( ) public boolean containsKey(Object key) public boolean containsValue(Object value)

13 Implementation class of the Map interface  There are several implementation classes of the Map interface in java.util, eg. HashMap, TreeMap.  All these classes have implemented all the abstract methods defined in Map.  Read the example TestHashMap.java Choosing suitable collection types and implementations if ( need to maintain key / value mappings ) use Map object elseif (allows duplicated objects) if ( fixed number of data items) use Array object else use List object else use Set object (pp15) (pp20, fig-10.11)

14 import java.util.*; // This version has 4 elements in Key set & 3 elements in Value set public class TestHashMap { public static void main(String[] args) { if (args.length < 1) {// Show usage message System.out.println("Usage: java TestTreeMap "); } else {// Create mapping between region codes & their full names Map mapping = new HashMap (); mapping.put(" HKG ", " Hong Kong "); mapping.put(" HK ", " Hong Kong "); mapping.put(" KLN ", " Kowloon "); mapping.put(" NT ", " New Territories "); Set keys = mapping.keySet();// Show the contents of the Map objects Collection values = mapping.values(); System.out.println("Keys are: " + keys); System.out.println("Values are: " + values); String region = (String) mapping.get(args[0]); // Get full name from Map object if (region == null) {// Show the result System.out.println( "The supplied region code is invalid"); } else { System.out.println("The region code(“ + args[0] +") is referring to \"“ + region + "\"."); }}}} Keyvalue

15 Iterators  To access each item in a collection, we can first convert it to an array of objects by using the method toArray( ). Then a for -loop is applied on the array to iterate through all elements  Alternatively, the Collection interface provides a better and more elegant way to iterate through all items. This is called an iterator.  The iterator of a collection can be obtained by calling the iterator( ) method of the collection. This method returns an object implementing the Iterator interface.  Methods of the Iterator interface: public boolean hasNext( ) public Object next( ) public void remove( ) (pp21)

16 Looping through a collection using an Iterator : Collection collection = …; Iterator iterator = collection. iterator( ) ; while (iterator. hasNext( ) ) { Object item = iterator. next( ) ; // process the item } Comparing with the approach using an array: Collection collection = …; Object[ ] items = collection. toArray ( ); for (int i=0; i<items.length; i++) { // process the item at items[i] }

17 Designing a data structure: queue  A queue is a FIFO data structure (compare to a stack which is LIFO). It contains a storage to hold a collection of data items, and the storage is managed by 2 behaviours: enqueue and dequeue.  A queue is a collection and hence should have an attribute length to represent its size. And we should be able to query the length of a queue.  Similar to other collection classes, we should first build a Queue interface to define the baseline behaviour of this data structure. public interface Queue { public void enqueue( Object obj ) ; public Object dequeue( ) ; public int size( ) ; } They are Abstract methods (pp25)

18  We can then build a concrete class of queue by implementing the Queue interface.  A queue contains a list of objects, and hence we can implement a concrete queue class using the concrete LinkedList class. eg. LinkedQueue.java (pp27) public class LinkedQueue implements Queue { private List list = new LinkedList ( ); public void enqueue( Object obj ) { list. add (obj);// add to end } public Object dequeue( ) { return list. remove (0);// remove from front } public int size( ) { return list. size ( ); }

19 Wrapper classes  Notice that the previous collection classes can only store elements of object, but not primitive value. To resolve this problem, Java provides 8 wrapper classes for the 8 primitive types to make these primitive values appear as objects.  Class hierarchy of the wrapper classes: Object Boolean Character Number Byte Double Float Short Long Integer (pp30)

20  To construct an object from a primitive value, we can use the corresponding wrapper class constructor:  eg  Note that all wrapper objects are immutable and final  To revert the process (i.e. conversion from the wrapper object back to its primitive value), use the method primitive Value ( ) ; eg. public WrapperClassName ( PrimitiveType value) Integer intObj = new Integer ( 10 ); Double doubleObj = new Double ( 3.14 ); int i= myInteger. int Value ( ); booleanb= myBoolean. boolean Value ( ); charc= myCharacter. char Value ( ); doubled= myDouble. double Value ( );

21  The constructors of all wrapper classes (except the Character class) can take in a string to obtain the corresponding wrapper object. e.g.  To convert a wrapper object back to a string, use the toString( ) method (defined for all wrapper classes).  Hence to convert a string to a primitive value, we can first construct the corresponding wrapper object and then use its primitive Value ( ) method. e.g.  Alternatively for Number wrapper objects, we can make use of the parsePrimitive ( ) class method of the wrapper classes. e.g. Integer intObj = new Integer(“10”); Double doubleObj = new Double(“3.14”); int x = new Integer(“1234”). intValue ( ); int x = Integer. parseInt (“1234”);

22 String class  String objects are immutable (i.e. once created, there is no way to change it unless creating another string).  Consider the following code, how many string objects are created? [ TestString.java (pp44 fig-10.25) ]  In this example, there is a large overhead of performing string concatenation. Hence if we need to perform string concatenation intensively, it can be a performance problem.  Java provides a StringBuffer class to solve this problem String str = “”; for (int i=0; i<30000; i++) str += i; (pp33)

23 StringBuffer class  In contrast to the String objects, StringBuffer objects are mutable. If we need to repeatedly concatenate strings, the StringBuffer object is more preferred for performance issues  To create a StringBuffer object, we can use 3 constructors: »  To change the content of a StringBuffer object, we can use these methods: append (...), delete (int, int), deleteCharAt (int), insert (int,…), replace (int, int, String), reverse ( )  There are 2 ways to convert a StringBuffer object to a String object : public StringBuffer( ) public StringBuffer(int length) public StringBuffer(String str) String str = aStringBuffer.toString( ); String str = new String(aStringBuffer);

24 What you should have done...  Received all books  Read up to Unit 10 What to do next?  Finish any unfinished unit  Read the specimen exam paper (when it is released)  Review all course materials and prepare for the exam About the last tutorial...  No slide will be prepared  Discussion on exam issues and the specimen exam paper