SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
Core Java Training
Collections - Maps
Page 1Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 1
Agenda
• Collections – Maps
• Map Interface
• Map methods
• Mapuse
• Hashmap
• Treemap
• Utilities
Page 2Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 2
Map Interface Context
Map
Page 3Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 3
Map Interface
• Stores key/value pairs
• Maps from the key to the value
• Keys are unique
• a single key only appears once in the Map
• a key can map to only one value
• Values do not have to be unique
Page 4Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 4
Map methods
Page 5Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 5
Map views
• A means of iterating over the keys and values in a Map
• Set keySet()
• returns the Set of keys contained in the Map
• Collection values()
• returns the Collection of values contained in the Map. This Collection
is not a Set, as multiple keys can map to the same value.
• Set entrySet()
• returns the Set of key-value pairs contained in the Map. The Map
interface provides a small nested interface called Map.Entry that is
the type of the elements in this Set.
Page 6Classification: Restricted
Map.Entry interface Example
import java.util.*;
class MapInterfaceExample{
public static void main(String args[]){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Page 7Classification: Restricted
Exercise..
• Create a Map with 5 values…
• Iterate through and print
• the keys,
• the values,
• both the keys and the values of the Map
Page 8Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 8
HashMap and TreeMap Context
HashMap TreeMap
Map
Page 9Classification: Restricted
HashMap and TreeMap
• HashMap
• The keys are a set - unique, unordered
• Fast
• TreeMap
• The keys are a set - unique, ordered
• Same options for ordering as a TreeSet
• Natural order (Comparable, compareTo(Object))
• Special order (Comparator, compare(Object, Object))
Page 10Classification: Restricted
HashMap
• A HashMap contains values based on the key.
• It contains only unique elements.
• It may have one null key and multiple null values.
• It maintains no order.
Page 11Classification: Restricted
HashMap Constructors
Page 12Classification: Restricted
HashMap methods
Page 13Classification: Restricted
HashMap Example: remove()
import java.util.*;
public class HashMapExample {
public static void main(String args[]) {
// create and populate hash map
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(101,"Let us C");
map.put(102, "Operating System");
map.put(103, "Data Communication and Networking");
System.out.println("Values before remove: "+ map);
// Remove value for key 102
map.remove(102);
System.out.println("Values after remove: "+ map);
}
}
Page 14Classification: Restricted
Difference between HashSet and HashMap
• Think…
Page 15Classification: Restricted
HashMap Example: Book
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author,
String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class MapExample {
public static void main(String[] args) {
//Creating map of Books
Map<Integer,Book> map=new HashMap<Integer,Book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications &
Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to map
map.put(1,b1);
map.put(2,b2);
map.put(3,b3);
//Traversing map
for(Map.Entry<Integer, Book> entry:map.entrySet()){
int key=entry.getKey();
Book b=entry.getValue();
System.out.println(key+" Details:");
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+
"+b.quantity);
}
}
}
Page 16Classification: Restricted
Exercise..
• What is a TreeMap? Implement the book example for TreeMap.
• What is a LinkedHashMap?
• What is a HashTable? How is it different from a HashMap?
• What is Comparable and Comparator interfaces in Java? How are they
different? Write programs utilizing both to test their usage.
Page 17Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 17
Bulk Operations
• In addition to the basic operations, a Collection may provide “bulk” operations
boolean containsAll(Collection c);
boolean addAll(Collection c); // Optional
boolean removeAll(Collection c); // Optional
boolean retainAll(Collection c); // Optional
void clear(); // Optional
Object[] toArray();
Object[] toArray(Object a[]);
A = {1,2,3,4} B={2,3,4,5}
addAll() -> A U B = {1,2,3,4,5} UNION
A = STUDENTS LEARNING JAVA; B = STUDENTS LEARNING .NET
A U B = ALL STUDENTS LEARNING EITHER JAVA OR .NET OR BOTH
A INTERSECTION B = ALL STUDENTS LEARNING BOTH JAVA AND .NET
removeAll() -> A-B = {1}, B-A={5}..
A IS ALL EMPLOYEES, B IS EMPLOYEES SERVING NOTICE PERIOD
A-B = EMPLOYEES NOT SERVING NOTICE PERIOD
retainAll() -> A INTERSECTION B = {2,3,4}
Page 18Classification: Restricted
HashMap vs TreeMap
• HashMap can contain one null key. TreeMap cannot contain a null key.
• HashMap does not maintain any order, whereas TreeMap maintains
ascending order.
Page 19Classification: Restricted
Utilities Context
Page 20Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 20
Utilities
• The Collections class provides a number of static methods for fundamental
algorithms
• Most operate on Lists, some on all Collections
• Sort, Search, Shuffle
• Reverse, fill, copy
• Min, max
• Wrappers
• synchronized Collections, Lists, Sets, etc
• unmodifiable Collections, Lists, Sets, etc
Page 21Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 21
Properties class
• Located in java.util package
• Special case of Hashtable
• Keys and values are Strings
• Tables can be saved to/loaded from file
Page 22Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 22
Topics to be covered in next session
• Inner Classes
• Method-local Inner Class
• Anonymous Inner Class
• Static Nested Inner Class
Page 23Classification: Restricted
3-February-
2003
cse403-10-Collections © 2003 University of Washington 23
Thank you!

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

sets and maps
 sets and maps sets and maps
sets and maps
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
List in java
List in javaList in java
List in java
 
Generics
GenericsGenerics
Generics
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java collections
Java collectionsJava collections
Java collections
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Java awt
Java awtJava awt
Java awt
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 

Semelhante a Java Map Interface and Implementations Guide

Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - MapsPawanMM
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List Hitesh-Java
 
Session 15 - Collections - Array List
Session 15 - Collections - Array ListSession 15 - Collections - Array List
Session 15 - Collections - Array ListPawanMM
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsPawanMM
 
Java Collection
Java CollectionJava Collection
Java CollectionDeeptiJava
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptx
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptxCON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptx
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptxskypy045
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfakankshasorate1
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Kenji HASUNUMA
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Kenji HASUNUMA
 

Semelhante a Java Map Interface and Implementations Guide (20)

Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Session 15 - Collections - Array List
Session 15 - Collections - Array ListSession 15 - Collections - Array List
Session 15 - Collections - Array List
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Collections
CollectionsCollections
Collections
 
Collections
CollectionsCollections
Collections
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
collections
collectionscollections
collections
 
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
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Java Collection
Java CollectionJava Collection
Java Collection
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptx
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptxCON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptx
CON5423-GS-Collections-and-Java-8-Functional-Fluent-Friendly-and-Fun.pptx
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
22.ppt
22.ppt22.ppt
22.ppt
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdf
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 

Mais de Hitesh-Java

Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCHitesh-Java
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOPHitesh-Java
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesHitesh-Java
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Hitesh-Java
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final) Hitesh-Java
 
Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Hitesh-Java
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction Hitesh-Java
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2 Hitesh-Java
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1Hitesh-Java
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2Hitesh-Java
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Hitesh-Java
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Hitesh-Java
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued Hitesh-Java
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1 Hitesh-Java
 

Mais de Hitesh-Java (20)

Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOP
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
JDBC
JDBCJDBC
JDBC
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
Object Class
Object Class Object Class
Object Class
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
 

Último

Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 

Último (20)

Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 

Java Map Interface and Implementations Guide

  • 2. Page 1Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 1 Agenda • Collections – Maps • Map Interface • Map methods • Mapuse • Hashmap • Treemap • Utilities
  • 3. Page 2Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 2 Map Interface Context Map
  • 4. Page 3Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 3 Map Interface • Stores key/value pairs • Maps from the key to the value • Keys are unique • a single key only appears once in the Map • a key can map to only one value • Values do not have to be unique
  • 5. Page 4Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 4 Map methods
  • 6. Page 5Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 5 Map views • A means of iterating over the keys and values in a Map • Set keySet() • returns the Set of keys contained in the Map • Collection values() • returns the Collection of values contained in the Map. This Collection is not a Set, as multiple keys can map to the same value. • Set entrySet() • returns the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map.Entry that is the type of the elements in this Set.
  • 7. Page 6Classification: Restricted Map.Entry interface Example import java.util.*; class MapInterfaceExample{ public static void main(String args[]){ Map<Integer,String> map=new HashMap<Integer,String>(); map.put(100,"Amit"); map.put(101,"Vijay"); map.put(102,"Rahul"); for(Map.Entry m:map.entrySet()){ System.out.println(m.getKey()+" "+m.getValue()); } } }
  • 8. Page 7Classification: Restricted Exercise.. • Create a Map with 5 values… • Iterate through and print • the keys, • the values, • both the keys and the values of the Map
  • 9. Page 8Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 8 HashMap and TreeMap Context HashMap TreeMap Map
  • 10. Page 9Classification: Restricted HashMap and TreeMap • HashMap • The keys are a set - unique, unordered • Fast • TreeMap • The keys are a set - unique, ordered • Same options for ordering as a TreeSet • Natural order (Comparable, compareTo(Object)) • Special order (Comparator, compare(Object, Object))
  • 11. Page 10Classification: Restricted HashMap • A HashMap contains values based on the key. • It contains only unique elements. • It may have one null key and multiple null values. • It maintains no order.
  • 14. Page 13Classification: Restricted HashMap Example: remove() import java.util.*; public class HashMapExample { public static void main(String args[]) { // create and populate hash map HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(101,"Let us C"); map.put(102, "Operating System"); map.put(103, "Data Communication and Networking"); System.out.println("Values before remove: "+ map); // Remove value for key 102 map.remove(102); System.out.println("Values after remove: "+ map); } }
  • 15. Page 14Classification: Restricted Difference between HashSet and HashMap • Think…
  • 16. Page 15Classification: Restricted HashMap Example: Book import java.util.*; class Book { int id; String name,author,publisher; int quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name = name; this.author = author; this.publisher = publisher; this.quantity = quantity; } } public class MapExample { public static void main(String[] args) { //Creating map of Books Map<Integer,Book> map=new HashMap<Integer,Book>(); //Creating Books Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8); Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4); Book b3=new Book(103,"Operating System","Galvin","Wiley",6); //Adding Books to map map.put(1,b1); map.put(2,b2); map.put(3,b3); //Traversing map for(Map.Entry<Integer, Book> entry:map.entrySet()){ int key=entry.getKey(); Book b=entry.getValue(); System.out.println(key+" Details:"); System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+ "+b.quantity); } } }
  • 17. Page 16Classification: Restricted Exercise.. • What is a TreeMap? Implement the book example for TreeMap. • What is a LinkedHashMap? • What is a HashTable? How is it different from a HashMap? • What is Comparable and Comparator interfaces in Java? How are they different? Write programs utilizing both to test their usage.
  • 18. Page 17Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 17 Bulk Operations • In addition to the basic operations, a Collection may provide “bulk” operations boolean containsAll(Collection c); boolean addAll(Collection c); // Optional boolean removeAll(Collection c); // Optional boolean retainAll(Collection c); // Optional void clear(); // Optional Object[] toArray(); Object[] toArray(Object a[]); A = {1,2,3,4} B={2,3,4,5} addAll() -> A U B = {1,2,3,4,5} UNION A = STUDENTS LEARNING JAVA; B = STUDENTS LEARNING .NET A U B = ALL STUDENTS LEARNING EITHER JAVA OR .NET OR BOTH A INTERSECTION B = ALL STUDENTS LEARNING BOTH JAVA AND .NET removeAll() -> A-B = {1}, B-A={5}.. A IS ALL EMPLOYEES, B IS EMPLOYEES SERVING NOTICE PERIOD A-B = EMPLOYEES NOT SERVING NOTICE PERIOD retainAll() -> A INTERSECTION B = {2,3,4}
  • 19. Page 18Classification: Restricted HashMap vs TreeMap • HashMap can contain one null key. TreeMap cannot contain a null key. • HashMap does not maintain any order, whereas TreeMap maintains ascending order.
  • 21. Page 20Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 20 Utilities • The Collections class provides a number of static methods for fundamental algorithms • Most operate on Lists, some on all Collections • Sort, Search, Shuffle • Reverse, fill, copy • Min, max • Wrappers • synchronized Collections, Lists, Sets, etc • unmodifiable Collections, Lists, Sets, etc
  • 22. Page 21Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 21 Properties class • Located in java.util package • Special case of Hashtable • Keys and values are Strings • Tables can be saved to/loaded from file
  • 23. Page 22Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 22 Topics to be covered in next session • Inner Classes • Method-local Inner Class • Anonymous Inner Class • Static Nested Inner Class
  • 24. Page 23Classification: Restricted 3-February- 2003 cse403-10-Collections © 2003 University of Washington 23 Thank you!