The contains() method of java.util.ArrayList class returns true if this list contains the specified element. … [Read more...]
How to swap elements in an ArrayList
In this tutorial we will learn how to swap elements of an ArrayList, we will be using Collections.swap() method to achieve sorting. Swapping the elements of the ArrayList Create a new arraylist arraylist al1. Add elements to al1 using add() method. Use Collections.swap() method to swap the required elements of al1. … [Read more...]
Override toString() method of ArrayList in Java
When we are working with ArrayList of Objects then it is must that we have to override toString() method of Java ArrayList to get the output in the desired format. Lets learn how to override toString() method. … [Read more...]
Find Common Elements between two ArrayList in Java
In this tutorial we will learn how to get the common elements between two ArrayList. We would be using removeAll() method to retain only the common elements between two lists. public boolean removeAll(Collection<?> c) It removes all the elements in the list that are not contained in the specified list. … [Read more...]
Java ArrayList indexOf(Object o) Method Example
In this tutorial we will learn how to use indexOf(Object o) method of java.utilArrayList class. This method is used for finding the index of a particular element in the list. … [Read more...]
How to create read only List, Set, Map in Java?
You can create a read-only Collection by using unmodifiableCollection() method, which returns you a unmodifiable or a read only collection. It will not allow you to add or remove element from the collection. When we try to modify the collection it will throw the java.lang.UnsupportedOperationException. Here we will try to make the Collection, List, Set and Map read-only. … [Read more...]
How to Convert Array to ArrayList in Java?
This is one of the popular questions in the Java world. I have encountered this situation a lot of times in my life which made me write this article on converting a Java Array to ArrayList. Almost everybody knows that the Arrays are used to store primitive data types and ArrayList is used to store the Objects. Let's see the different ways to convert. … [Read more...]