Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Similar documents
The class Object. Lecture CS1122 Summer 2008

equals() in the class Object

Advanced Programming Generics Collections

Collections Questions

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

Collections (Java) Collections Framework

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Java Collections Framework

Collection Framework Collection, Set, Queue, List, Map 3

Java Collections Framework reloaded

7 Objects and Collections

Generics and collections

Chapter 11: Collections and Maps

Object-Oriented Programming in the Java language

Collections. James Brucker

Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being-

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

Pieter van den Hombergh Richard van den Ham. February 8, 2018

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

CONTAİNERS COLLECTİONS

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Methods Common to all Classes

USAL1J: Java Collections. S. Rosmorduc

5/23/2015. Core Java Syllabus. VikRam ShaRma

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

Java Collections Framework. 24 April 2013 OSU CSE 1

Chapter 5 Java Collection. Wang Yang

Lecture 6 Collections

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Collections Framework: Part 2

Object Oriented Programming and Design in Java. Session 19 Instructor: Bert Huang

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Collections & Maps. Lecture 12. Collections & Maps

Programming Languages and Techniques (CIS120)

Super-Classes and sub-classes

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Expected properties of equality

CSE 331 Software Design & Implementation

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

CSE 331 Software Design & Implementation

Core Java Contents. Duration: 25 Hours (1 Month)

For this section, we will implement a class with only non-static features, that represents a rectangle

Programming Languages and Techniques (CIS120)

Elementary Symbol Tables

The Bucharest University of Economic Studies. Data Structures. Heap Priority Queues Maps and Sets

Principles of So3ware Construc9on: Objects, Design, and Concurrency. Introduc9on to Java. Josh Bloch Charlie Garrod Darya Melicher

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Java HashMap Interview Questions

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

CS2113 Lab: Collections 10/29/2018

Standard ADTs. Lecture 19 CS2110 Summer 2009

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Get started with the Java Collections Framework By Dan Becker

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

STANDARD ADTS Lecture 17 CS2110 Spring 2013

CS11 Java. Winter Lecture 8

Questions and Answers. Q.3) asses is not part of Java's collection framework?explanation:

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

Practical Session 3 Java Collections

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc.

java.lang.object: Equality

CS2110: Software Development Methods. Maps and Sets in Java

Model Solutions. COMP 103: Test April, 2013

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

Java Programming Unit 8. Selected Java Collec5ons. Generics.

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Chapter 10 Collections

Equality. Michael Ernst. CSE 331 University of Washington

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Introduction to Object-Oriented Programming

Equality. Michael Ernst. CSE 331 University of Washington

Collections and Maps

Programming Languages and Techniques (CIS120e)

Core Java Syllabus. Overview

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Generic Collections CSC Spring 2019 Howard Rosenthal

Introduction to Collections

JAVA. Duration: 2 Months

Data Structures 1 (Notes by Matt Fontaine, very lightly edited by Arup Guha)

Computer Science II (Spring )

13 A: External Algorithms II; Disjoint Sets; Java API Support

Uguaglianza e Identità. (no, non avete sbagliato corso )

Sets and Maps. Part of the Collections Framework

PIC 20A Collections and Data Structures

JAVA SYLLABUS FOR 6 WEEKS

MET08-J. Preserve the equality contract when overriding the equals() method

Domain-Driven Design Activity

DM550 Introduction to Programming part 2. Jan Baumbach.

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

COMP 250 Fall inheritance Nov. 17, 2017

JAVA MOCK TEST JAVA MOCK TEST III

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Transcription:

Collections by Vlad Costel Ungureanu for Learn Stuff

Collections 2

Collections Operations Add objects to the collection Remove objects from the collection Find out if an object (or group of objects) is in the collection Retrieve an object from the collection (without removing it) Iterate through the collection, looking at each element (object) one after another 3

Collections Equals() If the equals() method of a class is not overridden: it will use the one from Object class which uses == operator for comparison those objects won t be used as key in a map won t lead to the creation accurate Sets The equals() contract: it is reflexive - for any reference value x, x.equals(x) should return true it is symmetric - for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true it is transitive - for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true it is consistent - for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified for any non-null reference value x, x.equals(null) should return false 4

Collections HashCode() If two objects are considered equal, their hashcodes must also be equal! If the hashcode() method of a class is not overridden: it will be used the one from Object class which always comes up with a unique number for each object, even if the equals() method is overridden in such a way that two or more objects are considered equal the object in a collection won t be found unless the same reference to the object is given The hashcode() contract: whenever it is invoked on the same object more than once during an execution of a Java application, the hashcode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified if two objects are equal according to the equals(object) method, then calling the hashcode() method on each of the two objects must produce the same integer result it is NOT required that if two objects are unequal according to the equals(java.lang.object) method, then calling the hashcode() method on each of the two objects must produce distinct integer results; however, producing distinct integer results for unequal objects may improve the performance of Maps 5

Collections Index based collections for (i=0; i < list.size(); i++ ) { System.out.println( list.get(i) ); } Iterators for (Iterator it = collection.iterator(); it.hasnext(); ) { } System.out.println(it.next()); it.remove(); For-each structure Object based access List<Student> students = new ArrayList<Student>(); for (Student student : students) { student.setnota(10); } 6

List Interface ArrayList fast iteration and fast random access it is an ordered collection (by index), but not sorted as of version 1.4, ArrayList implements the RandomAccess interface Vector it is basically the same as an ArrayList, but Vector methods are synchronized for thread safety Vector is the only class other than ArrayList to implement RandomAccess LinkedList a LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another this linkage gives methods for adding and removing from the beginning or end as of Java 5, the LinkedList class has been enhanced to implement the java.util.queue interface it supports the common queue methods: peek(), poll() and offer() 7

Set Interface HashSet it uses the hashcode of the object being inserted it is unsorted, unordered use this class when you want a collection with no duplicates and you don't care about order when you iterate through it LinkedHashSet it is an ordered version of HashSet that maintains a doubly-linked List across all elements maintains insertion order TreeSet the TreeSet is one of two sorted collections it guarantees that the elements will be in ascending order, according to natural order as of Java 6, TreeSet implements NavigableSet 8

Map Interface HashMap it is an unsorted, unordered Map allows one null key and multiple null values Hashtable it is the synchronized counterpart to HashMap doesn't allow null keys or null values LinkedHashMap maintains insertion order of keys (default ) or access order faster iteration TreeMap it is a sorted Map as of Java 6, TreeMap implements NavigableMap 9

Queue Interface PriorityQueue introduced in Java 5 the purpose of a PriorityQueue is to create a "priority-in, priority out queue as opposed to a typical FIFO queue the elements' ordering represents their relative priority 10

Sorting Collections Methods for sorting are given by java.util.collections and by java.util.arrays classes: Collections.sort(List<T> list) it uses the natural order of the elements for sorting an exception is thrown when trying to sort elements of class that doesn t have defined the natural order to define a sorting order, a class can implement the Comparable interface Collections.sort(List<T> list, Comparator<? super T> c) sorts the specified list according to the order induced by the specified comparator the Comparator interface gives you the capability to sort a given collection any number of different ways 11

Assignments Write a class that represents a money transaction between banks. Write a program that stores several transactions in various types of collections: ArrayList, LinkedList, Map, Vector, Set. Make sure that each to add the same transactions(same instance) to each collection. Override equals() and hashcode() for the transaction class. Iterate the collections and for each collection perform the following: add, remove, compare to a specific transaction (using equals), modify properties of a transaction. Observe behavior of the previous operations and calculate for each operation, for each transaction the time each operation took. Write the times in a file and compare times between collection types. 12

THANK YOU! Vlad Costel Ungureanu ungureanu_vlad_costel@yahoo.com This is a free course from LearnStuff.ro not for commercial use 13