awesomeleft.blogg.se

Java collections scenario based questions
Java collections scenario based questions











java collections scenario based questions

There is a huge difference between HashMap and ArrayList, fundamental is former is a Map data structure that stores key-value pairs while the latter stores just an object. Difference between HashMap and ArrayList in Java? ( Answer) The former is used to sort on natural order while the latter is used while sorting in the custom order.ġ1. You can easily sort the ArrayList by using Collections.sort() method, all you need to make sure is that elements implement either the Comparable or Comparator interface.

#JAVA COLLECTIONS SCENARIO BASED QUESTIONS HOW TO#

How to sort ArrayList in Java? ( Answer)Īnother task-based ArrayList interview question. There is a nice little trick to do this by using the Arrays.asList() method, but remember the List returned by this class has some differences with ArrayList, please check the answer to understand what are those differences.ġ0. How to create and initialize ArrayList in one line? ( Answer)

java collections scenario based questions

Though both Vector and ArrayList implement List interface, Vector is synchronized while ArrayList is not synchronized, which means the former is thread-safe and fast while the latter is not thread-safe and slow.ĩ. This is the second most popular question based on ArrayList in Java. Difference between Vector and ArrayList in Java? ( Answer) There are many ways to traverse over ArrayList, you can use classic for loop with index, or you can take iterator from ArrayList and can use while loop in conjunction with Iterator.hasNext() method, Or you can use the new foreach loop introduced in Java 5, which doesn't require an index. How to loop over ArrayList in Java? ( Answer) The main difference is the former is List while the later is Set which means ArrayList allowed duplicates, keeps elements in order while HashSet doesn't allow duplicates and provides no ordering guarantee.ħ. One of the simplest questions you will ever see on a Java interview. Difference between ArrayList and HashSet in Java? ( Answer) See the answer for more ways to do this task.Ħ. There are a couple of more ways like iterating through the list and copying elements into a new list. You can reverse ArrayList by using the Collections.reverse() method. How to reverse ArrayList in Java? ( Answer) Since the List interface allows duplicates, ArrayList also allowed it but if you remember Set interface doesn't allow duplicates, which means you can remove duplicates from ArrayList by converting it into a Set and then back to ArrayList, but how will you keep the order intact? See the answer for a more detailed explanation.Ģ. How to remove duplicates from ArrayList in Java? ( Answer) In my list, I have combined both of them.ġ. The questions are mainly divided into two categories, one is a fact-based question, which checks how much you know about ArrayList, and the other is the task-based question, which evaluates how good you are at doing things with ArrayList. So without wasting any more time, let's start with the questions. 20+ ArrayList Questions with Answers from Java Interviews













Java collections scenario based questions