Slide 1

Slide 1 text

Collections Framework Beginners guide II HASUNUMA Kenji [email protected]

Slide 2

Slide 2 text

Introduction

Slide 3

Slide 3 text

What's Collections? • Major data structures • Basic algorithm • Simple and flexible design • e.g. Concurrency, Hazelcast • packaged in java.util.*

Slide 4

Slide 4 text

Algorithm and Data structures

Slide 5

Slide 5 text

Basic concepts Algorithm Data structures

Slide 6

Slide 6 text

Basic concepts Algorithm Data structures Collections Collection Map

Slide 7

Slide 7 text

Data structures Collection Map Set List Queue Deque Elements are • Unordered • No duplicate • Sequential Access key and value pairs (Hash variable) Elements are • Ordered • May duplicate • Random Access Elements are • Ordered • May duplicate • Sequential Access Collection of values

Slide 8

Slide 8 text

Set List Deque Map Hash Table HashSet -- -- HashMap Resizable Array -- ArrayList ArrayDeque -- Balanced Tree TreeSet -- -- TreeMap Linked List -- LinkedList LinkedList -- Hash Table + Linked List Linked HashSet -- -- Linked HashMap

Slide 9

Slide 9 text

Hash Table Sequantial Access Slow Random Access Fastest Insert/Delete Fastest Resizable Array Sequantial Access Fast (size dependent) Random Access Fast (size dependent) Insert/Delete Slow Balanced Tree Sequantial Access Medium Random Access Medium Insert/Delete Fast (constant) Linked List Sequantial Access Fast (size dependent) Random Access Slow Insert/Delete Fast (size dependent)

Slide 10

Slide 10 text

How to select the type

Slide 11

Slide 11 text

How to select the type 1. Basic type
 e.g. String, Integer, BigDecimal 2. Type of data structure
 e.g. Set, List, Deque, Map 3. Implement of data structure
 e.g. HashSet, TreeSet, ArrayList, LinkedList, ArrayDeque, HashMap, TreeMap

Slide 12

Slide 12 text

#1: String, unordered, no duplicated 1. Basic type : String 2. Data structure : Set 3. Implement : HashSet Set set = new HashSet<>();

Slide 13

Slide 13 text

#2: int, ordered, random access 1. Basic type : Integer (as int) 2. Data structure : List 3. Implement : ArrayList List list = new ArrayList<>();

Slide 14

Slide 14 text

#3: String, ordered, random access, insert or remove frequently 1. Basic type : String 2. Data structure : List 3. Implement : LinkedList List list = new LinkedList<>();

Slide 15

Slide 15 text

Basic operations

Slide 16

Slide 16 text

Methods of Collection • boolean add(E e) • boolean remove(Object o) • boolean isEmpty() • int size() • void clear() • T toArray(T[] a)

Slide 17

Slide 17 text

Methods of List • boolean add(int index, E element) • E get(int index) • E set(int index, E element) • int remove(int index) • int indexOf(Object o) • int lastIndexOf(Object o) For Random Access

Slide 18

Slide 18 text

Methods of Deque • boolean offer(E element) • boolean add(E element) w/Exception • E poll() • E remove() w/Exception • E peek() • E element() w/Exception As Queue

Slide 19

Slide 19 text

Methods of Deque • void push(E element) • E pop() • E peek() As Stack

Slide 20

Slide 20 text

Methods of Map • V get(Object key) • V getOrDefault(Object key, V value) • V put(K key, V value) • V putIfAbsent(K key, V value) • V replace(K key, V value) • V remove(Object key)

Slide 21

Slide 21 text

Methods of Map • Set keySet() • Collection values() • boolean containsKey(Object key) • boolean containsValue(Object value) • int size() • void clear()

Slide 22

Slide 22 text

Algorithm : Collections Provides general algorithms: • Sort and shuffle elements (List) • Reverse, fill and copy elements (List) • Binary search (List) • Minimum, maximum and frequency • Create synchronized or read only collection • and more...

Slide 23

Slide 23 text

enhanced for statement • for (UnannType var : expr) stmt • var : an element of expr • expr : Collection or Array • Get an element from expr iteratively and set it to var, then do stmt

Slide 24

Slide 24 text

// Example #1: // print any elements in the List // to upper case every lines. List list = new ArrayList<>(); ... for (String s : list) { // show elements (to upper case) System.out.println(s.toUpperCase()); }

Slide 25

Slide 25 text

// Example #2: // print any elements in the Set // to upper case every lines. Set set = new HashSet<>(); ... for (String s : set) { // show elements (as upper case) System.out.println(s.toUpperCase()); }

Slide 26

Slide 26 text

Bulk operations

Slide 27

Slide 27 text

Basic concepts Algorithm Data structures Collections Collection Map

Slide 28

Slide 28 text

Basic concepts Algorithm Data structures Collections Collection Map Stream

Slide 29

Slide 29 text

Collection and Stream API Collection Stream stream() collect() Data structure Operators for whole collection Terminal Operations <> <>

Slide 30

Slide 30 text

Stream as Set operators (intermediate operations) Method Role as Set operator filter condition map mapping values (a.k.a. production) distinct reducing duplication sorted sorting values concat union with another set limit truncation of values

Slide 31

Slide 31 text

Stream as Set operators (terminal operations) Method Role as Set operator reduce general purpose totaling min minimum of values max maximum of values count count of values anyMatch whether any values match condition allMatch whether all values match condition noneMatch whether no value match condition

Slide 32

Slide 32 text

// Example // Create a view from the result of Twitter4J Twitter twitter = TwitterFactory.getSingleton(); // snip QueryResult result = twitter.search(query); List statuses = result.getTweets(); List tweets = statuses.stream() // to Stream (Set operations) .filter(s -> s.getCreateAt().after(SOME_DAY)) .map(Tweet::new) // Convert Status to Tweet .distinct() // Maybe, Stream has duplicates .sort() // Sort by default order (prepare) .limit(10) // Restrict 10 values from head .collect(Collectors.toList); // List

Slide 33

Slide 33 text

Attentions

Slide 34

Slide 34 text

Concurrency • Never synchronized (spec.) • Make collections thread-safe: • Synchronized by Collections • Using concurrency collections (java.util.concurrent.*) *recommend*

Slide 35

Slide 35 text

Old collections • Vector, Hashtable, Stack • Fixed to parts of collections:
 e.g. Vector -> List, Hashtable -> Map • Different behavior from standards: • Synchronized • Not allowed to add/put null

Slide 36

Slide 36 text

Appendix

Slide 37

Slide 37 text

Array • Fixed Array • Array • Resizable Array • Vector (now replaced ArrayList)

Slide 38

Slide 38 text

Definition of Array • T [ ] identifier = new T [ size ] ; • T : primitives or classes • T [ ] : assumed a class • e.g. String[] names = new String[3];

Slide 39

Slide 39 text

Initialization of Array 1. String[] names = new String[] {
 “Able”, “Baker”, “Charlie” }; 2. String[] names = new String[3];
 String[0] = “Able”;
 String[1] = “Baker”;
 String[2] = “Charlie”;

Slide 40

Slide 40 text

Using Array • Access by index: String name = names[0]; // get “Able”
 names[0] = “Alfa”; // set “Alfa” • Obtain the length: // Output ‘3’ to the console
 System.out.println(names.length);

Slide 41

Slide 41 text

Array and Collection • Array to List: List list = 
 Arrays.asList(names); • List (Collection) to Array: String[] names = 
 list.toArray(new String[list.size()]);

Slide 42

Slide 42 text

More Array.asList • Arrays.asList method accepts variable arguments, so … List list = Arrays.asList(
 “Able”, “Baker”, “Charlie”);

Slide 43

Slide 43 text

Collections Framework Beginners guide II HASUNUMA Kenji [email protected]