Part 10
Interview questions on Collections in java for experienced covered in this post:
Can we store objects in descending order in SortedMap? If yes then, how can I store objects in descending order in SortedMap?
-
We can store objects in descending order in SortedMap.
-
There are a couple of ways we can do it in the case of
SortedMap.
1.
The first way is to implement Comparator in TreeMap
Constructor.
2.
The second way is you can use the Collections class
reverseOrder() method.
-
Please refer to the below two examples.
1. Using Comparator Interface. package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public
static
void
main(String[] args) {
SortedMap<Integer, String> sMap
= new
TreeMap<>(new
DecendingOrder());
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
} } class DecendingOrder implements Comparator<Integer>{
@Override
public
int
compare(Integer i1, Integer i2) {
return
i2.compareTo(i1);
}
}
Output:
{10000=Ten Thousand, 1000=Thousand, 100=Hundred, 10=Ten, 1=One}
2. Using reverseOrder() of Collections class. package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public
static
void
main(String[] args) {
SortedMap<Integer, String> sMap
= new
TreeMap<>(Collections.reverseOrder());
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
}
}
Output:
{10000=Ten Thousand, 1000=Thousand, 100=Hundred, 10=Ten,
1=One} |
What is NavigableMap? What is the purpose of NavigableMap?
-
NavigableMap is a child class of SortedMap.
-
NavigableMap has added some new methods to navigate
throughout the collection object.
-
Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.NavigableMap; import java.util.TreeMap; public class NavigableMapDemo {
public
static
void
main(String[] args) {
NavigableMap<Integer, String> nMap
= new
TreeMap<Integer, String>();
nMap.put(50,"Fifty");
nMap.put(10,"Ten");
nMap.put(30,"Thirty");
nMap.put(20,"Twenty");
nMap.put(40,"Forty");
System.out.println("Actual nMap : "
+ nMap);
System.out.println("Lower value of 30 using lower()
= "+ nMap.lowerKey(30));
System.out.println("Higher value of 30 using floor() = "+ nMap.floorKey(30));
System.out.println("Lowest value of 30 using ceiling()
=
"+ nMap.ceilingKey(30));
System.out.println("Lowest value of 30 using higher()
= "+ nMap.higherKey(30));
System.out.println("Lowest value of 30 using pollFirst() = "+ nMap.pollFirstEntry());
System.out.println("Lowest value of 30 using pollLast() = "+ nMap.pollLastEntry());
NavigableMap<Integer, String> reverse
= nMap.descendingMap();
System.out.println("Reverse nMap : "
+ reverse);
}
}
Output:
Actual nMap : {10=Ten, 20=Twenty, 30=Thirty, 40=Forty,
50=Fifty}
Lower value of 30 using lower()
= 20
Higher value of 30 using floor() = 30
Lowest value of 30 using ceiling()
=
30
Lowest value of 30 using higher()
= 40
Lowest value of 30 using pollFirst() = {10=Ten}
Lowest value of 30 using pollLast() = {50=Fifty}
Reverse nMap : {40=Forty, 30=Thirty, 20=Twenty} |
What is TreeMap? Can you explain how the objects will be stored in TreeMap?
-
TreeMap is a data structure where elements are stored in some
natural sorting order.
-
Underlying data structure is the RED-BLACK tree.
-
Insertion order never maintains. Maintain some natural
sorting order.
-
Duplicates keys are not allowed but duplicate values are
allowed.
-
If we are depending on natural sorting order then keys should
be homogeneous and comparable otherwise it will through
ClassCastException.
-
If we are defining our comparator then keys need not be
homogeneous or comparable.
-
We can insert homogeneous as well as Heterogeneous (Different
types of Object) objects in the Treemap.
-
Null is strictly now allowed after java 7.
-
Till java 6, For non-empty Treemap if we are trying to insert
an entry with the null key then we will get NullPointerException. For empty
tree map at first entry with null key is allowed but after inserting new key
you will get NullPointerException.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) {
TreeMap<Integer, String> tMap
= new
TreeMap<Integer, String>();
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
}
}
Output:
{1=One, 10=Ten, 100=Hundred, 1000=Thousand} |
Is it possible to store elements in decending order in TreeMap?
-
Yes, it is possible to store elements in treeMap in
descending order.
-
There are a couple of ways we can store elements in
descending order which is as follows.
1.
You can provide a Comparator object in the TreeMap
constructor.
2. You can use Collections.reverse() method to it.
package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) {
TreeMap<Integer, String> tMap
= new
TreeMap<Integer, String>(new
ReverseOrderTreeElements());
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
} } class ReverseOrderTreeElements implements Comparator<Integer>{
@Override
public
int
compare(Integer o1, Integer o2) {
return
o2.compareTo(o1);
}
} Output: {1000=Thousand, 100=Hundred, 10=Ten, 1=One} package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) {
TreeMap<Integer, String> tMap
= new
TreeMap<Integer,
String>(Collections.reverseOrder());
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
}
}
Output:
{1000=Thousand, 100=Hundred, 10=Ten, 1=One} |
What is Hashtable? What are the characteristics of Hashtable?
-
Hashtable is a thread-safe version of HashMap.
-
Hashtable also maintain the elements in the form of Key/value
pair.
-
Underlying data structure of Hashtable is Hashtable.
-
Insertion order never maintains.
-
Insertion depends on the hashcode of the key.
-
Duplicate keys are not allowed but duplicate values are
allowed.
-
Heterogeneous (Different types of Object) objects are
allowed.
-
Null is not allowed for both keys as well as value.
Otherwise, it will through NullPointerException.
-
Every method defined inside HashTable is synchronized.
-
Hashtable is Clonable because Hashtable implements a Clonable
interface.
-
Hashtable is Serializable because Hashtable implements a
Serializable interface.
-
Hashtable is the worst choice if want to perform search or
sort operation.
-
Hashtable has not implemented RandomAccess.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.Hashtable; public class HashTableDemo {
public
static
void
main(String[] args) {
Hashtable<Integer, String> tMap
= new
Hashtable<Integer, String>();
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
}
}
Output: {10=Ten, 1000=Thousand, 1=One, 100=Hundred} |
-
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