Java Generics And collections

Slides:



Advertisements
Similar presentations
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Advertisements

Generic types for ArrayList Old Java (1.4 and older): ArrayList strings = new ArrayList(); strings.enqueue(“hello”); String word = (String) strings.get(0);
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 15 – Monday.  What did we talk about last time?  Tries.
Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at.
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
Advanced Programming in Java
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 23:More on Interfaces
תרגול 7 – מבני נתונים גנריים רובי בוים ומתי שמרת
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Slides by Donald W. Smith
Slides by Donald W. Smith
Unit 1 Hadoop and big data
Some Collections: BAGS, SETS, and STACKS
Chapter 19 Java Data Structures
Software Development Java Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Advanced Programming in Java
Generic(Parameterized ) Types
Stacks.
A tree set Our SearchTree class is essentially a set.
Week 15 – Monday CS221.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
structures and their relationships." - Linus Torvalds
Java Collections Overview
Advanced Programming Behnam Hatami Fall 2017.
Back to Collections Lists: Sets Maps ArrayList LinkedList
Introduction to Collections
Introduction to Collections
Introduction to Collections
Advanced Programming in Java
תגבור פרונטלי 6 ברוכים הבאים מבוא למדעי המחשב 2018
(Java Collections Framework) AbstractSequentialList
Java Collections Framework
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
slides adapted from Marty Stepp
Collections in Java The objectives of this lecture are:
Arrays and Collections
Iteration Abstraction
Advanced Programming in Java
Introduction to Collections
Collections Framework
Interator and Iterable
CSE 1020: The Collection Framework
Introduction to Collections
Containers and Iterators
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
Iterators Dan Fleck.
CSE 143 Lecture 21 Advanced List Implementation
COMP204 Bernhard Pfahringer (with input from Robi Malik)
structures and their relationships." - Linus Torvalds
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Java Generics And collections Generic Methods and Classes Java Collections – List, Sets and Maps Iterators and compareTo()

Generic method static float add(float f, float g) { float answer = f + g; return answer; } static int add(int i, int j) int answer = i + j; static long add(long l, long m) long answer = l + m; // Generic add method static <T> add(T a, T b) { T answer = a + b; return answer; } float f = 3.14; float g = 5; float h = add(f,g);

Generic Class class Pair<T,S> { protected T first; protected S second; public Pair( T a, S b ) first = a; second = b; } public printPair( system.out.print(“First=” + first .” Second=“ + second”) ); Pair<String,Color> colorName = new Pair<String,Color>("Red", Color.RED); Pair<Double,Double> coordinates = new Pair<Double,Double>(17.3,42.8);

Generics – Stack (of Objects) Class class Stack<E> { private final int size; private int top; private E[] elements; public Stack() { this(10); } public Stack(int s) { size = s > 0 ? s : 10; top = -1; elements = (E[]) new Object[size]; // create array } public void push(E pushValue) { if (top < size - 1) // if stack is not full elements[++top] = pushValue; // place pushValue on Stack public E pop() { if (top > -1) // if stack is not empty return elements[top--]; // remove and return top element of Stack

Java Collections The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. Although it is a framework, it works in a manner of a library. The JCF provides both interfaces that define various collections and classes that implement them.

Three main types of Collections Lists, Sets and Maps

Three main types of collections - Lists Lists: always ordered, may contain duplicates and can be handled the same way as usual arrays

Three main types of collections - Maps Maps: connect unique keys with values, provide random access to its keys and may host duplicate values

Three main types of collections - Sets Sets: cannot contain duplicates and provide random access to their elements

Iterators and Iterating for (int i = 0: i < 10; i++) // i is an iterator of type int { // do stuff } int i=0; // I is an iterator of type int while (I < 10) i = i+1; What if you wanted to iterate through a linked list? What would be the iterator’s type? int? What it the next?

Generic List in Java String string1 = “first string"; String string2 = “second string"; List<String> list = new ArrayList<String>; list.add(string1); list.add(string2); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()) { System.out.println(someString); String someString = iterator.next(); }

Generic Set in Java Set<String> set = new HashSet<String>; String string1 = "a string"; set.add(string1); Iterator<String> iterator = set.iterator(); while(iterator.hasNext()) { String aString = iterator.next(); }

Generic Map in Java Map<Integer, String> map = new HashMap<Integer, String>; Integer key1 = new Integer(123); String value1 = "value 1"; map.put(key1, value1); String value1_1 = map.get(key1); Iterator<Integer> keyIterator = map.keySet().iterator(); while(keyIterator.hasNext()){ Integer aKey = iterator.next(); String aValue = map.get(aKey); }

Java Collections Hierarchy

Lists List listA = new ArrayList(); listA.add("element 0"); listA.add("element 1"); listA.add("element 2"); String element0 = listA.get(0); String element1 = listA.get(1); String element3 = listA.get(2); //access via Iterator Iterator iterator = listA.iterator(); while(iterator.hasNext() { String element = (String) iterator.next(); } for(Object object : listA) String element = (String) object;

Sorting – CompareTo() List list = new ArrayList(); //add elements to the list Collections.sort(list); public int compareTo(Student s) { if (gpa > s.gpa) { return 1; } if (gpa < s.gpa) { return -1; } if (gpa == s.gpa { return 0; } } Note 1: Since ArrayList implements the Comparable interface. it must supply a compareTo() method. public interface Comparable<T> { int compareTo(T o); } Note 2: You may override compareTo()