Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Collections newSetFromMap() method in Java with Examples
Next article icon

Collections synchronizedMap() method in Java with Examples

Last Updated : 06 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

The synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.

Syntax:

public static <K, V> Map<K, V> synchronizedMap(Map<K, V> m)

Parameters: This method takes the map as a parameter to be “wrapped” in a synchronized map.

Return Value: This method returns a synchronized view of the specified map.

Below are the examples to illustrate the synchronizedMap() method.

Example 1:




// Java program to demonstrate
// synchronizedMap() method
// for <String, String> Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of Map<String, String>
            Map<String, String>
                map = new HashMap<String, String>();
  
            // populate the map
            map.put("Value1", "20");
            map.put("Value2", "30");
            map.put("Value3", "40");
  
            // printing the Collection
            System.out.println("Map : " + map);
  
            // create a synchronized map
            Map<String, String>
                synmap = Collections.synchronizedMap(map);
  
            // printing the Collection
            System.out.println("Synchronized map is : "
                               + synmap);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
 
 
Output:
  Map : {Value3=40, Value1=20, Value2=30}  Synchronized map is : {Value3=40, Value1=20, Value2=30}  

Example 2:




// Java program to demonstrate
// synchronizedMap() method
// for <String, Boolean> Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of Map<String, Boolean>
            Map<String, Boolean>
                map = new HashMap<String, Boolean>();
  
            // populate the map
            map.put("Bramha", true);
            map.put("Vishnu", true);
            map.put("Mahesh", true);
  
            // printing the Collection
            System.out.println("Map : " + map);
  
            // create a synchronized map
            Map<String, Boolean>
                synmap = Collections.synchronizedMap(map);
  
            // printing the Collection
            System.out.println("Synchronized map is : "
                               + synmap);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
 
 
Output:
  Map : {Bramha=true, Vishnu=true, Mahesh=true}  Synchronized map is : {Bramha=true, Vishnu=true, Mahesh=true}  


Next Article
Collections newSetFromMap() method in Java with Examples

R

RohitPrasad3
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Collections
  • Java-Functions
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Collections synchronizedSortedMap() method in Java with Examples
    The synchronizedSortedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that all access to the backing sorted map is accomplished through the returned sorted ma
    2 min read
  • Java Collections synchronizedNavigableMap​() Method with Examples
    NavigableMap is used for convenient navigation methods like lowerKey, floorKey, ceilingKey, and higherKey, along with this popular navigation method. It will take key-value pair as input We can create a navigable map by using the following syntax: NavigableMap<key_datatype, value_datatype> dat
    2 min read
  • Collections synchronizedCollection() method in Java with Examples
    The synchronizedCollection() method of java.util.Collections class is used to return a synchronized (thread-safe) collection backed by the specified collection. In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collecti
    2 min read
  • Collections singletonMap() method in Java with Examples
    The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a arg
    2 min read
  • Collections newSetFromMap() method in Java with Examples
    The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to
    2 min read
  • SortedMap conatinsKey() method in Java with Examples
    The containsKey() method is used to check whether a particular key is being mapped into the SortedMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: boolean containsKey(key_element) Parameters: The method takes just one parameter key_el
    2 min read
  • SortedMap clear() method in Java with Examples
    The clear() method in SortedMap Java is used to clear and remove all of the elements or mappings from a specified SortedMap collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Note: The clear() method in SortedMa
    2 min read
  • SortedMap conatinsValue() method in Java with Examples
    The containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the SortedMap. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the map. Syntax: boolean containsValue(Object Value) Parameters:
    3 min read
  • Collectors toList() method in Java with Examples
    The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
    2 min read
  • Collections list() method in Java with Examples
    The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences