Part 3
Interview questions on Concurrent Collections in java for experienced covered in this post:
What the the new methods introduced in ConcurrentHashMap to performing read and write operation?
-
There are 3 methods has introduced in ConcurrentHashMap which
are as follows.
1.
putIfAbsent():
If the key is present then there won’t be any change in collection.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapPutIfAbsentDemo { public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map
= new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.putIfAbsent(103,"Arpita"); // In this case 103 key was present
System.out.println(map);
map.putIfAbsent(104,"Ananya"); // In this case 104 key was not present.
System.out.println(map);
}
}
Output:
{101=Shweta, 102=Shruti, 103=Yogesh}
{101=Shweta, 102=Shruti, 103=Yogesh, 104=Ananya}
|
2. remove(): This method won’t remove elements until and unless key and value match.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapRemoveDemo {
public
static
void
main(String[] args) {
ConcurrentHashMap<Integer, String> map
= new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.remove(103,"Arpita"); // In this case 103 key is present but value was present.
System.out.println(map);
map.remove(103,"Yogesh"); // In this case key 103 and value Yogesh was present so
removed.
System.out.println(map);
} }
Output:
{101=Shweta, 102=Shruti, 103=Yogesh}
{101=Shweta, 102=Shruti} |
3.
replace(): This
method won’t replace value until and unless key and value match.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapReplaceDemo {
public
static
void
main(String[] args) {
ConcurrentHashMap<Integer, String> map
= new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.replace(103,"Arpita");
System.out.println(map);
map.replace(103,"Arpita","Updated Arpita");
System.out.println(map);
}
}
Output:
{101=Shweta, 102=Shruti, 103=Arpita}
{101=Shweta, 102=Shruti, 103=Updated Arpita} |
What is CopyOnWriteArrayList? How does it work internally?
-
CopyOnWriteArrayList is Concurrent implementation of List
interface.
-
CopyOnWriteArrayList implements List interface so, all basic
functionality of List is applicable to CopyOnWriteArrayList.
-
It is a very costly object because every update operation
creates a clone copy of the same object.
-
When one thread is iterating CopyOnWriteArrayList same time
another thread is allowed to perform writes operation still we won’t get
ConcurrentModificationException.
-
Iterator of ArrayList performs remove operation. Iterator of
CopyOnWriteArrayList can not perform the remove operation. If we try to
remove then it will throw UnSupportedOperationException.
-
Iterator of CopyOnWriteArrayList is Fail-Safe.
-
Please refer to the below example.
package
simplifiedjava.crackedInterview; import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListDemo {
public
static
void
main(String[] args) {
CopyOnWriteArrayList<Integer> list
= new
CopyOnWriteArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
System.out.println(list);
}
}
Output: [10, 20, 30, 40, 50] |
What is ConcurrentModificationException?
-
When one thread is performing a read operation other threads
are not allowed to perform any operation on the same object still you are
attempting the same then it will throw ConcurrentModificationException.
-
Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.HashMap;
import
java.util.Iterator; import java.util.Map; public class ConcurrentModificationExceptionDemo {
public
static
void
main(String[] args) {
HashMap<Integer,String> m
= new
HashMap<Integer,String>();
m.put(101,"Shweta"); m.put(102,"Shruti");
Iterator itr
= m.entrySet().iterator();
while(itr.hasNext()) {
m.put(103, "Anjali");
Map.Entry<Integer, String> pair
= (Map.Entry<Integer, String>)itr.next();
System.out.println("Key = "+ pair.getKey()+ "\t Value = "+ pair.getValue());
}
}
}
Output:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
at
simplifiedjava.crackedInterview.ConcurrentModificationExceptionDemo.main(ConcurrentModificationExceptionDemo.java:17)
|
-
Java interview questions and answers all MNC - Click here
-
Basic core java interview questions and answers for freshers
- Click here
-
Core java interview questions for 3 years experience - Click here
-
Core java interview questions and answers for 3-5 years exp
- Click here
-
Core java interview questions and Answers for 5 - 7 Years exp
- Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced
- Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on
some support projects and then I moved to java projects.
I had worked with many multi-national companies like
R-Systems, Tata Consultancy Services, Cybage
Softwares. Fortunately, TCS and Cybage has
given me an opportunity to take interviews for
experienced candidates. I have conducted more than 1000
interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments