Interview questions on Collections in java for experienced

 Part 5

Interview questions on Collections in java for experienced covered in this post:

    Interview questions on Collections classes in java for experienced

        What is NavigableSet?

    -          The Java NavigableSet interface, java.util.NavigableSet, is a subtype of the Java SortedSet interface.

    -          Therefore the NavigableSet behaves like a SortedSet, but with an additional set of navigation methods available in addition to the sorting mechanisms of the SortedSet.

    -          A NavigableSet may be accessed and traversed in either ascending or descending order.

    -          The descendingSet() method returns a view of the set with the senses of all relational and directional methods inverted.

    -          NavigableSet reverse = original.descendingSet(); also returns the reverse order.

    -          This interface additionally defines methods pollFirst and pollLast that return and remove the lowest and highest element, if one exists, else returning null.

    -          Methods subSet, headSet, and tailSet differ from the like-named SortedSet methods in accepting additional arguments describing whether lower and upper bounds are inclusive versus exclusive.

    -          Implementation class of NavigableSet is java.util.TreeSet class.

    -          Subsets of any NavigableSet must implement the NavigableSet interface.

    -          Create a NavigableSet.

    1.       NavigableSet navigableSet = new TreeSet();

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.NavigableSet;

    import java.util.TreeSet; 

    public class NavigableSetDemo { 

          public static void main(String[] args) {

                NavigableSet<Integer> set = new TreeSet<Integer>();

                set.add(50);

                set.add(10);

                set.add(30);

                set.add(20);

                set.add(40);           

          System.out.println("Actual Set : " + set);           

       System.out.println("Lower value of 30 using lower()   = "+ set.lower(30));

       System.out.println("Higher value of 30 using floor()  = "+ set.floor(30));

       System.out.println("Lowest value of 30 using ceiling()     = "+ set.ceiling(30));

                System.out.println("Lowest value of 30 using higher()       = "+ set.higher(30));

                System.out.println("Lowest value of 30 using pollFirst()    = "+ set.pollFirst());

                System.out.println("Lowest value of 30 using pollLast()     = "+ set.pollLast());          

                NavigableSet<Integer> reverse = set.descendingSet();       

                System.out.println("Reverse Set : " + reverse);      

          }

    }

    Output:

    Actual Set : [10, 20, 30, 40, 50]

    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

    Lowest value of 30 using pollLast()  = 50

    Reverse Set : [40, 30, 20] 

     

        What is the TreeSet? How the object will be stored in Treeset?

    -          TreeSet stores the object in sorted and ascending order.

    -          TreeSets underlying data structure is a balanced tree.

    -          Duplicate objects are not allowed.

    -          Insertion order not preserved.

    -          Homogeneous objects are allowed.

    -          Heterogeneous (Different types of Object) objects are not allowed otherwise we will get ClassCastException.

    -          Null insertion is not possible otherwise you will get NullPointerException.

    -          TreeSet is Serializable because TreeSet implements a Serializable interface.

    -          TreeSet is Clonable because TreeSet implements a Clonable interface.

    -          TreeSet has not implemented RandomAccess.

    -          All objects will be inserted based on some sorting order. It may be a default sorting order or customized sorting order.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.Iterator;

    import java.util.TreeSet; 

    public class TreeSetDemo { 

          public static void main(String[] args) {

                TreeSet<Integer> set = new TreeSet<Integer>();

                set.add(50);

                set.add(10);

                set.add(30);

                set.add(20);

                set.add(40);

                System.out.println("Original Set : " + set);

                TreeSet<Integer> reverse = (TreeSet<Integer>) set.descendingSet();

                System.out.println("Reverse Set : " + reverse);           

                // Reverse using iterator

                Iterator<Integer> itr = set.descendingIterator();

                System.out.println("Using Iterator : ");

                while(itr.hasNext()) {

                      int i = (int)itr.next();

                      System.out.print(i+ ", ");

                }          

          }

    }

    Output:

    Original Set : [10, 20, 30, 40, 50]

    Reverse Set : [50, 40, 30, 20, 10]

    50, 40, 30, 20, 10,

     

     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

        Can we insert null in TreeSet? If not then why it is not possible?

    -          We cannot insert null in TreeSet.

    -          TreeSet internally stores the elements according to some sorting order.

    -          While inserting records into the tree set JVM always compare the two values and while comparing you might get NullPointerException.

     

        What is importance of emptySet() method in collection framework?

    -          An emptySet() method can be used to get the set which is empty.

     

        What are the characteristics of Queue Interface?

    -          Queue interface is used to hold the element about to be processed in FIFO(First In First Out).

    -          A Queue is a collection for holding elements prior to processing.

    -          Queue is a child interface of the Collection interface.

    -          Besides basic Collection operations, queues provide additional insertion, removal, and inspection operations.

    -          Please refer the queue interface and its method.

    public interface Queue<E> extends Collection<E> {

        E element();

        boolean offer(E e);

        E peek();

        E poll();

        E remove();

    }

       

        What is Priority Queue?

    -          If we want to represent a group of individual objects prior to processing according to some priority then it is called Priority Queue.

    -          Priority can be natural sorting order or customized sorting order defined by the comparator.

    -          Insertion order is not preserved. It is based on some priority.

    -          Duplicate objects are not allowed.

    -          Null is not allowed.

    -           If we are depending on natural sorting order then objects must implement a comparable interface or it should be homogeneous. Otherwise, we will get ClassCastException.

    -          If we are defining our own Comparator then there is no need to have homogeneous object.

    -          Please refer to the below two examples.

    package simplifiedjava.crackedInterview; 

    import java.util.Comparator;

    import java.util.PriorityQueue; 

    public class PriorityQueueDemo { 

          public static void main(String[] args) {

                PriorityQueue<String> q = new PriorityQueue<>(new DecendingQueueComparator());

                q.offer("Shweta");

                q.offer("Shruti");

                q.offer("Arpita");           

                while(!q.isEmpty()) {

                      System.out.println(q.poll().toString());

                }    

          }

    } 

    class DecendingQueueComparator implements Comparator<String>{ 

          @Override

          public int compare(String s1, String s2) {

                return s2.compareTo(s1);

          }    

    }

    Output:

    Shweta

    Shruti

    Arpita 

    // Ascending Order. 

    package simplifiedjava.crackedInterview; 

    import java.util.Comparator;

    import java.util.PriorityQueue; 

    public class PriorityQueueDemo { 

          public static void main(String[] args) {

                PriorityQueue<String> q = new PriorityQueue<>(new AscendingQueueComparator());

                q.offer("Shweta");

                q.offer("Shruti");

                q.offer("Arpita");           

                while(!q.isEmpty()) {

                      System.out.println(q.poll().toString());

                }    

          }

    } 

    class AscendingQueueComparator implements Comparator<String>{ 

          @Override

          public int compare(String s1, String s2) {

                return s1.compareTo(s2);

          }    

    }




    • 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


    Upcoming questions in next post:


    Previous Post                                                                    Next Post



    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.

     











    Post a Comment

    0 Comments