Learn how to get the element from an ArrayList. We will be using ArrayList.get() method to get the object at the specified index of the arraylist.
Learn how to get the index of first occurrence of a element in the ArrayList. We will be using ArrayList.indexOf() method to get the first occurrence.
Learn to get last index of element in arraylist in Java using Arraylist.lastIndexOf() method. It returns the last occurrence of the element in this list.
Learn how to get a sublist of an existing ArrayList. We will be using ArrayList.subList() method to get the sublist of arraylist object. 1. ArrayList.subList() method This method returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). 1.1. subList() Method Syntax 1.2. subList() Method Parameters fromIndex – […]
Learn to add multiple items to ArrayList. There can be two usescases which require to add multiple items to an arraylist. Add all items from a collection to arraylist Add selected items from a collection to arraylist 1. Add multiple items to arraylist – ArrayList.addAll() To add all items from another collection to arraylist, use […]
Learn to remove element from ArrayList. Remove element at specifed index, or element value. Remove all elements from arraylist for spcified value example.
Java program to replace element at specified index of arraylist using ArrayList.add(int index, E element) and ArrayList.set(int index, E element) methods.
The Java iterate through ArrayList programs. Learn how to retrieve values from ArrayList in Java using for loop, while loop, iterator and stream api.
The Java ArrayList can be initialized in number of ways depending on the requirement. Learn to initialize ArrayList based on some frequently seen usecases.
Learn to create List instances with only one element in it using Arrays.asList() and Collections.singletonList() methods. Using Collections.singletonList() Method [ Immutable List ] This is simplest and recommended method to create immutable List with single element inside it. The list created with this method is immutable as well, so you are sure that there will […]
To iterate list in java is very basic operation, but over the years it’s gone through some significant changes. We will go through all these changes in given examples.
Below examples uses different techniques to iterate over java collections. Use what’s suitable and easy for you in any situation. Iterable.forEach method (Java 8) Recently introduced in java 8, This method can be called on any Iterable and takes one argument implementing the functional interface java.util.function.Consumer. e.g. Java “foreach” loop (Java 5) The “foreach” loop […]
Difference between Enumerator and Iterator can be asked to you in any java interview. In this post, I am listing down few differences which you may cite while answering the question. Difference between enumerator and iterator First of all, enumerations are only applicable for legacy classes e.g Hashtable, Vector. Enumerations were part of initial java […]
Many times you will be asked this question in your interviews. e.g. How Iterator works in java, how it remove objects from List? While removing objects from list, when it may produce ConcurrentModificationException? In Java, when you create an iterator, it starts to count the modifications that were applied on the collection. If the iterator […]
If you are preparing for a job as entry level java programmer, you can expect this interview question. Though the question becomes really easy when you know other concepts like how hashmap works, yet after getting a many queries from my blog readers who are preparing for or working at junior level, I have decided […]
Without argument, java collections is one of the most important area where you will be tested in any position whether junior or senior. The scope is so much wide, that its almost impossible to cover all the questions. Yet based on my previous interviews, I am attempting to put as many as possible GOOD java […]
Can we use an object as a key for a hashmap in java? This is a very popular interview question indeed. It is asked immediately after “How HashMap works?”. Lets make a reasoning around user defined object as key in hashmap in java. 1. The contract between hashCode() and equals() The very basic need for […]
Vector is a another legacy implementation of List interface provided with java bundle. It is almost similar to ArrayList expect it is synchronized also. It has its own advantages as well as disadvantages e.g. Vector doesn’t need additional synchronization while accessed from multiple threads but it degrades its performance for the same reason. Here I […]
We have already learned about how HashMap in java works internally. If my last similar post, I tried to compare different “for loop” flavors available in java. These studies usually help in setting up best practices for your next project. I this post, I decided to compare traversal in hash map in java. HashMap is […]
For loop is very common control flow statement in programming languages such as java. I am not going to describe the basics of “for loop” as it is beyond the scope of this article and most of us are already well aware of this. In this post, I will list down various ways to use […]