If the elements of the List do not implement the Comparable interface, the sort() method requires a Comparator to be passed as an argument. If the elements of the List implement the Comparable interface, the sort() method uses their natural order. This method sorts the elements of a List in ascending order. “To sort the elements of a collection, I would use the Collections.sort() method. How do you sort the elements of a collection?.For example, for an ArrayList of Strings, I would use the following code: for (String element : myArrayList) ” “There are several ways to iterate over the elements in a collection, but one of the most common ways is to use the for-each loop. How do you iterate over the elements in a collection?.In general, an ArrayList is better suited for situations where you will be frequently accessing elements by index, whereas a LinkedList is better suited for situations where you will be frequently inserting or deleting elements.” A LinkedList is a doubly-linked list that provides fast insertion and deletion of elements but slower random access. “An ArrayList is a dynamic array that provides fast random access to its elements. Can you explain the difference between an ArrayList and a LinkedList?.If I need a collection that will hold unique elements and maintain their order, I would use a LinkedHashSet.” For example, if I need a collection that will hold a large number of elements and allow fast lookups, I would use a HashMap. “When deciding which collection class to use for a particular task, I consider factors such as the size of the collection, the type of data it will hold, and the operations that will be performed on it. How do you decide which collection class to use for a particular task?.Some of the main interfaces it provides include List, Set, and Map.” Some of the main classes it provides include ArrayList, HashSet, and HashMap. “The Java Collections Framework is a set of classes and interfaces that provide a unified way to store and manipulate groups of objects. What is the Java Collections Framework and what are some of the main classes and interfaces it provides?.Must Read 40 Best Resume Headlines for Freshers – Resume Title Samples They may be asked about the different classes and interfaces, how they are used, and their differences, as well as how they would implement specific collection-related tasks. In an interview, a Java developer may be asked about their knowledge and experience with the Java Collections Framework. and interfaces, like List, Set, etc., to handle different types of data. It provides various classes, like ArrayList, HashSet, etc. These classes and interfaces are collectively known as the Java Collections Framework. Save my name, email, and website in this browser for the next time I comment.Java Collections is a framework that provides several classes and interfaces for storing and manipulating groups of objects. Your email address will not be published. Data structure and algorithm Interview Questions.restful web services interview questions.Method overloading and overriding interview questions.Java Serialization interview questions in java.Exceptional handling interview questions in java.Java Multithreading interview questions. That’s all about java collections interview questions. Please go through java interview programs for more such programs and core java interview questions for more interview questions. Please go to difference between comparator and comparable for differences. Hashcode and equals method in java Question 10:ĭifference between Comparator and Comparable in java?Īnswer: This is also one of the most asked java collections interview questions. If we want to sort custom object (Lets say country) on different attributes such as name,population etc.We can implement Comparator interface and can be used for sorting.įor more details,you can go through following links:Īs we did not override equals method here, it will have default implementation which return true if objects point to same reference (=) but here, as we have created two different object as employeeOne and employeeTwo, size of HashSet will be 2 It will provides default way of sorting custom objects. We need to implement comparable interface to custom object class(Lets say Country) and then implement compareTo(Object o) method which will be used for sorting. How to sort a collection of custom Objects in Java? As compareTo method returns 0, employeeOne is equals to employeeTwo, so employeeTwo will not be added to treeSet.When we will added second element with martin, compareTo method will get called with pareTo(employeeTwo) and it will return 0.First element with “John” will be added to employeeSet.As you can see we have overridden compareTo method in Employee class and always return 0.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |