SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
Collections Framework
Beginners guide II
HASUNUMA Kenji

k.hasunuma@coppermine.jp
Introduction
What's Collections?
• Major data structures

• Basic algorithm

• Simple and flexible design

• e.g. Concurrency, Hazelcast

• packaged in java.util.*
Algorithm and Data structures
Basic concepts
Algorithm Data structures
Basic concepts
Algorithm Data structures
Collections Collection
Map
Data structures
Collection<E> Map<K, V>
Set<E> List<E> Queue<E>
Deque<E>
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
Set<E> List<E> Deque<E> Map<K, V>
Hash
Table
HashSet -- -- HashMap
Resizable
Array
-- ArrayList ArrayDeque --
Balanced
Tree
TreeSet -- -- TreeMap
Linked
List
-- LinkedList LinkedList --
Hash Table

+

Linked List
Linked

HashSet
-- --
Linked

HashMap
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)
How to select the type
How to select the type
1. Basic type

e.g. String, Integer, BigDecimal

2. Type of data structure

e.g. Set<E>, List<E>, Deque<E>, Map<K, V>

3. Implement of data structure

e.g. HashSet, TreeSet, ArrayList, LinkedList,
ArrayDeque, HashMap, TreeMap
#1: String, unordered, no duplicated
1. Basic type : String

2. Data structure : Set<String>

3. Implement : HashSet<String>

Set<String> set = new HashSet<>();
#2: int, ordered, random access
1. Basic type : Integer (as int)

2. Data structure : List<Integer>

3. Implement : ArrayList<Integer>

List<Integer> list = new ArrayList<>();
#3: String, ordered, random access,
insert or remove frequently
1. Basic type : String

2. Data structure : List<Integer>

3. Implement : LinkedList<Integer>

List<String> list = new LinkedList<>();
Basic operations
Methods of Collection<E>
• boolean add(E e)
• boolean remove(Object o)
• boolean isEmpty()
• int size()
• void clear()
• <T> T toArray(T[] a)
Methods of List<E>
• 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
Methods of Deque<E>
• boolean offer(E element)
• boolean add(E element) w/Exception
• E poll()
• E remove() w/Exception
• E peek()
• E element() w/Exception
As Queue
Methods of Deque<E>
• void push(E element)
• E pop()
• E peek() As Stack
Methods of Map<K, V>
• 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)
Methods of Map<K, V>
• Set<K> keySet()
• Collection<V> values()
• boolean containsKey(Object key)
• boolean containsValue(Object value)
• int size()
• void clear()
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...
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
// Example #1:
// print any elements in the List
// to upper case every lines.
List<String> list = new ArrayList<>();
...
for (String s : list) {
// show elements (to upper case)
System.out.println(s.toUpperCase());
}
// Example #2:
// print any elements in the Set
// to upper case every lines.
Set<String> set = new HashSet<>();
...
for (String s : set) {
// show elements (as upper case)
System.out.println(s.toUpperCase());
}
Bulk operations
Basic concepts
Algorithm Data structures
Collections Collection
Map
Basic concepts
Algorithm Data structures
Collections Collection
Map
Stream
Collection and Stream API
Collection Stream
stream()
collect()
Data structure
Operators for
whole collection
Terminal
Operations
<<Export>>
<<Import>>
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
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
// Example
// Create a view from the result of Twitter4J
Twitter twitter =
TwitterFactory.getSingleton();
// snip
QueryResult result = twitter.search(query);
List<Status> statuses = result.getTweets();
List<Tweet> 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<Tweet>
Attentions
Concurrency
• Never synchronized (spec.)

• Make collections thread-safe:

• Synchronized by Collections

• Using concurrency collections
(java.util.concurrent.*) *recommend*
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
Appendix
Array
• Fixed Array

• Array

• Resizable Array

• Vector (now replaced ArrayList)
Definition of Array
• T [ ] identifier = new T [ size ] ;

• T : primitives or classes

• T [ ] : assumed a class

• e.g. String[] names = new String[3];
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”;
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);
Array and Collection
• Array to List:

List<String> list = 

Arrays.asList(names);

• List (Collection) to Array:

String[] names = 

list.toArray(new String[list.size()]);
More Array.asList
• Arrays.asList method accepts variable
arguments, so …

List<String> list = Arrays.asList(

“Able”, “Baker”, “Charlie”);
Collections Framework
Beginners guide II
HASUNUMA Kenji

k.hasunuma@coppermine.jp

Contenu connexe

Tendances

java collections
java collectionsjava collections
java collectionsjaveed_mhd
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_FrameworkKrishna Sujeer
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
L11 array list
L11 array listL11 array list
L11 array listteach4uin
 
SPL: The Undiscovered Library - DataStructures
SPL: The Undiscovered Library -  DataStructuresSPL: The Undiscovered Library -  DataStructures
SPL: The Undiscovered Library - DataStructuresMark Baker
 

Tendances (20)

java collections
java collectionsjava collections
java collections
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Java collection
Java collectionJava collection
Java collection
 
Fp intro scala
Fp intro scalaFp intro scala
Fp intro scala
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
collections
collectionscollections
collections
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Array list
Array listArray list
Array list
 
Java collection
Java collectionJava collection
Java collection
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
L11 array list
L11 array listL11 array list
L11 array list
 
Java
JavaJava
Java
 
SPL: The Undiscovered Library - DataStructures
SPL: The Undiscovered Library -  DataStructuresSPL: The Undiscovered Library -  DataStructures
SPL: The Undiscovered Library - DataStructures
 

Similaire à Collections Framework Begineers guide 2

standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxhemanth248901
 
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1Shinya Mochida
 
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...Howard Greenberg
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from javaIndicThreads
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CAlexis Gallagher
 
Iniciando com jquery
Iniciando com jqueryIniciando com jquery
Iniciando com jqueryDanilo Sousa
 
Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.pptswapnilslide2019
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryNilesh Dalvi
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)Ortus Solutions, Corp
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...Ortus Solutions, Corp
 

Similaire à Collections Framework Begineers guide 2 (20)

Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
JavaCollections.ppt
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
 
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1
jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1
 
Datastruct2
Datastruct2Datastruct2
Datastruct2
 
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...MWLUG Session-  AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
MWLUG Session- AD112 - Take a Trip Into the Forest - A Java Primer on Maps, ...
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
07 java collection
07 java collection07 java collection
07 java collection
 
Iniciando com jquery
Iniciando com jqueryIniciando com jquery
Iniciando com jquery
 
Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
 
Java Collections
Java  Collections Java  Collections
Java Collections
 

Plus de Kenji HASUNUMA

Life of our small product
Life of our small productLife of our small product
Life of our small productKenji HASUNUMA
 
Jakarta EE : The First Parts
Jakarta EE : The First PartsJakarta EE : The First Parts
Jakarta EE : The First PartsKenji HASUNUMA
 
Overviewing Admin Console
Overviewing Admin ConsoleOverviewing Admin Console
Overviewing Admin ConsoleKenji HASUNUMA
 
How to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web ApplicationsHow to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web ApplicationsKenji HASUNUMA
 
Introduction to MicroProfile Metrics
Introduction to MicroProfile MetricsIntroduction to MicroProfile Metrics
Introduction to MicroProfile MetricsKenji HASUNUMA
 
Introduction to JCA and MDB
Introduction to JCA and MDBIntroduction to JCA and MDB
Introduction to JCA and MDBKenji HASUNUMA
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization FundamentalKenji HASUNUMA
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4Kenji HASUNUMA
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3Kenji HASUNUMA
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX DialogsKenji HASUNUMA
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time APIKenji HASUNUMA
 
Introduction to Date and Time API 2
Introduction to Date and Time API 2Introduction to Date and Time API 2
Introduction to Date and Time API 2Kenji HASUNUMA
 
Introduction to Data and Time API
Introduction to Data and Time APIIntroduction to Data and Time API
Introduction to Data and Time APIKenji HASUNUMA
 

Plus de Kenji HASUNUMA (17)

oop-in-javaee
oop-in-javaeeoop-in-javaee
oop-in-javaee
 
Jakarta REST in depth
Jakarta REST in depthJakarta REST in depth
Jakarta REST in depth
 
Life of our small product
Life of our small productLife of our small product
Life of our small product
 
Jakarta EE : The First Parts
Jakarta EE : The First PartsJakarta EE : The First Parts
Jakarta EE : The First Parts
 
Overviewing Admin Console
Overviewing Admin ConsoleOverviewing Admin Console
Overviewing Admin Console
 
How to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web ApplicationsHow to adapt MicroProfile API for Generic Web Applications
How to adapt MicroProfile API for Generic Web Applications
 
Introduction to MicroProfile Metrics
Introduction to MicroProfile MetricsIntroduction to MicroProfile Metrics
Introduction to MicroProfile Metrics
 
Introduction to JCA and MDB
Introduction to JCA and MDBIntroduction to JCA and MDB
Introduction to JCA and MDB
 
Virtualization Fundamental
Virtualization FundamentalVirtualization Fundamental
Virtualization Fundamental
 
JLS myths
JLS mythsJLS myths
JLS myths
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Fundamental Java
Fundamental JavaFundamental Java
Fundamental Java
 
Introduction to Date and Time API 3
Introduction to Date and Time API 3Introduction to Date and Time API 3
Introduction to Date and Time API 3
 
Introduction to JavaFX Dialogs
Introduction to JavaFX DialogsIntroduction to JavaFX Dialogs
Introduction to JavaFX Dialogs
 
Brand new Date and Time API
Brand new Date and Time APIBrand new Date and Time API
Brand new Date and Time API
 
Introduction to Date and Time API 2
Introduction to Date and Time API 2Introduction to Date and Time API 2
Introduction to Date and Time API 2
 
Introduction to Data and Time API
Introduction to Data and Time APIIntroduction to Data and Time API
Introduction to Data and Time API
 

Dernier

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 

Dernier (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 

Collections Framework Begineers guide 2

  • 1. Collections Framework Beginners guide II HASUNUMA Kenji k.hasunuma@coppermine.jp
  • 3. What's Collections? • Major data structures • Basic algorithm • Simple and flexible design • e.g. Concurrency, Hazelcast • packaged in java.util.*
  • 4. Algorithm and Data structures
  • 6. Basic concepts Algorithm Data structures Collections Collection Map
  • 7. Data structures Collection<E> Map<K, V> Set<E> List<E> Queue<E> Deque<E> 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
  • 8. Set<E> List<E> Deque<E> Map<K, V> Hash Table HashSet -- -- HashMap Resizable Array -- ArrayList ArrayDeque -- Balanced Tree TreeSet -- -- TreeMap Linked List -- LinkedList LinkedList -- Hash Table + Linked List Linked HashSet -- -- Linked HashMap
  • 9. 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)
  • 10. How to select the type
  • 11. How to select the type 1. Basic type
 e.g. String, Integer, BigDecimal 2. Type of data structure
 e.g. Set<E>, List<E>, Deque<E>, Map<K, V> 3. Implement of data structure
 e.g. HashSet, TreeSet, ArrayList, LinkedList, ArrayDeque, HashMap, TreeMap
  • 12. #1: String, unordered, no duplicated 1. Basic type : String 2. Data structure : Set<String> 3. Implement : HashSet<String> Set<String> set = new HashSet<>();
  • 13. #2: int, ordered, random access 1. Basic type : Integer (as int) 2. Data structure : List<Integer> 3. Implement : ArrayList<Integer> List<Integer> list = new ArrayList<>();
  • 14. #3: String, ordered, random access, insert or remove frequently 1. Basic type : String 2. Data structure : List<Integer> 3. Implement : LinkedList<Integer> List<String> list = new LinkedList<>();
  • 16. Methods of Collection<E> • boolean add(E e) • boolean remove(Object o) • boolean isEmpty() • int size() • void clear() • <T> T toArray(T[] a)
  • 17. Methods of List<E> • 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
  • 18. Methods of Deque<E> • boolean offer(E element) • boolean add(E element) w/Exception • E poll() • E remove() w/Exception • E peek() • E element() w/Exception As Queue
  • 19. Methods of Deque<E> • void push(E element) • E pop() • E peek() As Stack
  • 20. Methods of Map<K, V> • 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)
  • 21. Methods of Map<K, V> • Set<K> keySet() • Collection<V> values() • boolean containsKey(Object key) • boolean containsValue(Object value) • int size() • void clear()
  • 22. 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...
  • 23. 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
  • 24. // Example #1: // print any elements in the List // to upper case every lines. List<String> list = new ArrayList<>(); ... for (String s : list) { // show elements (to upper case) System.out.println(s.toUpperCase()); }
  • 25. // Example #2: // print any elements in the Set // to upper case every lines. Set<String> set = new HashSet<>(); ... for (String s : set) { // show elements (as upper case) System.out.println(s.toUpperCase()); }
  • 27. Basic concepts Algorithm Data structures Collections Collection Map
  • 28. Basic concepts Algorithm Data structures Collections Collection Map Stream
  • 29. Collection and Stream API Collection Stream stream() collect() Data structure Operators for whole collection Terminal Operations <<Export>> <<Import>>
  • 30. 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
  • 31. 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
  • 32. // Example // Create a view from the result of Twitter4J Twitter twitter = TwitterFactory.getSingleton(); // snip QueryResult result = twitter.search(query); List<Status> statuses = result.getTweets(); List<Tweet> 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<Tweet>
  • 34. Concurrency • Never synchronized (spec.) • Make collections thread-safe: • Synchronized by Collections • Using concurrency collections (java.util.concurrent.*) *recommend*
  • 35. 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
  • 37. Array • Fixed Array • Array • Resizable Array • Vector (now replaced ArrayList)
  • 38. Definition of Array • T [ ] identifier = new T [ size ] ; • T : primitives or classes • T [ ] : assumed a class • e.g. String[] names = new String[3];
  • 39. 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”;
  • 40. 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);
  • 41. Array and Collection • Array to List: List<String> list = 
 Arrays.asList(names); • List (Collection) to Array: String[] names = 
 list.toArray(new String[list.size()]);
  • 42. More Array.asList • Arrays.asList method accepts variable arguments, so … List<String> list = Arrays.asList(
 “Able”, “Baker”, “Charlie”);
  • 43. Collections Framework Beginners guide II HASUNUMA Kenji k.hasunuma@coppermine.jp