Skip to main content

TreeMap in Java

TreeMap in Java

In this article, we are going to learn about TreeMap in Java what it is, various ways to create an instance of TreeMap, methods provided by TreeMap in Java and many more topics related to TreeMap in Java.

What is TreeMap in Java?

TreeMap in java is an implementation of Map interface, which provides the facility to store and retrieve key-value pairs.
TreeMap internally implements the Red-Black Tree data structure and stores key-value pairs in the sorted order. To sort the key-value pairs, the key object must be mutually comparable with each other.
TreeMap sorts the key-value pairs according to one of them :
  • Sort in the order specified by the Comparator<T>, if the Comparator<T> has been provided at the time of creation of TreeMap, or
  • Sort in the natural order (order provided by the Comparable<T> Interface) of its keys. In this case, the key object must implement the Comparable<T> Interface
Note : We must have to provide an implementation of one of them. If we provide the implementation of both then the implementation of the Comparator will have higher priority.
TreeMap in Java is the part of Java Collection Framework. It is defined in java.util package and implements Map interface which provides standard method declaration to perform basic operation like store, retrieve key-value pairs from the TreeMap and etc.

Important point to remember about the TreeMap in Java

  • TreeMap in java is based on the implementation of Red-Black Tree.
  • The key objects of the TreeMap cannot be null but TreeMap in java can have null value objects.
  • The key must be unique within a TreeMap i.e., the duplicity of keys is not allowed.
  • The key-value pair is sorted into an order, it can be a natural order (i.e., in case of Comparable Interface) or specified order (i.e., in case of Comparator Interface).
  • TreeMap in java is a generic class i.e., it can store any type of objects for key and value. But it cannot store any primitive type elements, for that we can use wrapper classes. For example Integer class instead of int primitive.
  • TreeMap in java is not synchronized among the threads, it must be synchronized externally in a multi-threaded environment.

Time complexity of TreeMap in Java?

Time complexity to store and retrieve key-value pairs from the TreeMap in Java is O(log n) in any scenario because whenever we add any key-value pair, the Red-Black Tree of TreeMap internally gets self-balanced i.e., the height of Red-Black Tree becomes O(log n), that provides the O(log n) time complexity to search any element in the tree.

Hierarchy of the TreeMap in java?

Declaration of TreeMap
public class TreeMap<K, V>
    extends AbstractMap<K, V>
    implements NavigableMap<K, V>, Cloneable, java.io.Serializable {
     ----------------
}

AbstractMap<K, V>

The AbstractMap<K, V> provides a basic and abstract implementation of the Map Interface to the TreeMap.

NavigableMap<K, V>

TreeMap in java also implements NavigableMap<K, V>, which is an extension of SortedMap<K, V>. NavigableMap provides standard methods to navigate throughout the Map, methods like lowerKey(), floorKey(), ceilingKey() and higherKey() and many more.
Also, this interface provides the functionality to retrieve the sub-map from the actual Map. The headMap(), tailMap() and subMap() methods are used to fetch sub-map on the basis of fromKey and toKey.

Why Cloneable?

Cloneable is a marker interface and it is a part of java.lang package. A class implements this interface if it wants to call the clone() method of Object class using super keyword i.e., super.clone(); through any method or any constructor. If that class does not implement this marker interface and invoke the i.e., super.clone(); then java.lang.CloneNotSupportedExceptionwill be raised.
TreeMap in Java, provides overrode clone() method that internally calls the actual clone() method of Object class. So, TreeMap needs to implement this marker interface.
Note : The clone() method of Object class is used to create a cloned object.

Why Serializable?

Serializable is a marker interface and it is a part of java.io package. If Any class implements this interface then the class can be serialized or deserialized. Serialization is a process in which an object is converted into the byte stream and Deserialization is the reverse of serialization i.e., byte stream to java object. Serialization helps us to store and retrieve objects from and to files, databases, networks, etc. The only class marked with the Serializable interface can be serialized or deserialized.
TreeMap in Java often needs to store and retrieve the object from the files, databases, etc. That's why TreeMap is implemented as Serializable.

How to create an object of TreeMap in java?

Till now, we have learned what is TreeMap in Java and its hierarchy, now let's have a look at the different ways to create objects of TreeMap.

Using Default Constructor

Default Constructor of TreeMap, creates a new TreeMap. The class type of key objects must implement the Comparable Interface otherwise java.lang.ClassCastException will be raised at the time of adding of any key-value pair to the TreeMap in Java.
If the TreeMap is constructed using the default constructor, the keys are sorted in its natural order. The order defined by the Comparable interface known as Natural Order.
TreeMap<String, Integer> treeMap = new TreeMap<>();
Above, the type of key is declared as String and String class implements Comparable interface. If we want to use any user-defined class as the key to the TreeMap, it must implement the Comparable interface.

Using Constructor which requires Comparator

This constructor takes an argument of the Comparator and constructs an empty TreeMap. If we want to store keys that do not implement the Comparable interface then we can use this constructor to create a TreeMap in Java. Let's first create a user-defined class that does not implement the Comparable interface.
class Book{
     private String name;
     private int price;

     public Book(String name, int price){
          this.name = name;
          this.price = price;
     }

     public String getName() {
          return name;
     }

     public void setName(String name) {
          this.name = name;
     }

     public int getPrice() {
          return price;
     }

     public void setPrice(int price) {
          this.price = price;
     }

     @Override
     public String toString(){  
          return "[ " + name + " : " + price + " ]";
     }   
}
Create a TreeMap using Comparator that store key as Book and value as Integer for its quantity.
Comparator<Book> bookComparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
Note : Type declaration of Comparator and Type declaration of key of TreeMap must be same. As we can see in the above case, Book class is used for both Comparator and Key of the TreeMap.

How to create an object of Comparator?

To create an object of Comparator, we have two options using Anonymous class and Lambda Expression.
Using Anonymous class
Comparator<Book> bookComparator = new Comparator<Book>() {
     @Override
     public int compare(Book o1, Book o2) {
          return o1.getName().compareTo(o2.getName());
     }
};
Using Lambda Expression
Comparator<Book> bookComparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
To know more about the Lambda Expression, read this article: Lambda Expression in java

How to add key-value pairs into the TreeMap in java?

The put() method is used to add the key-value pairs into the TreeMap in Java. This method takes 2 arguments, first is key and second is value.

When the TreeMap is constructed using default Constructor

As we know if the TreeMap is constructed using default Constructor then the key objects must implement the Comparable Interface. We can use String class as the key objects for the TreeMap because String class implements Comparable Interface.
For example:

Java TreeMap Example 1 : Create a TreeMap that store name of books (String class) as Key and available quantity (Integer class) as Value.

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++" , 35);
for (Entry<String, Integer> entry : bookMap.entrySet()) {
      System.out.println(entry.getKey() + " = " + entry.getValue());
}
Output
Data Structures = 60
Introduction to Algorithms = 80
Learn C++ = 35
Programming In Java = 45
Don't worry about the "entrySet() method", we will understand this later. Just look at the output, as we can see the book names are sorted into alphabetical order. This is because of the natural order provided by the Comparable interface to the strings.

When the TreeMap is constructed with Comparator

If the TreeMap in Java is constructed using Comparator then we can use Keys which does not implement the Comparable Interface, we can use our custom class defined here.
For Example:

Java TreeMap Example 2 : Create a TreeMap that store object of books (Book class) as Key and available quantity (Integer class) as Value.

Comparator<Book> bookComparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
bookMap.put(new Book("Programming In Java", 400), 45);
bookMap.put(new Book("Introduction to Algorithms", 600), 80);
bookMap.put(new Book("Data Structures", 500), 60);
bookMap.put(new Book("Learn C++", 300), 35);
for (Entry<Book, Integer> entry : bookMap.entrySet()) {
      System.out.println(entry.getKey() + " = " + entry.getValue());
}
Output
[ Learn C++ : 300 ] = 35
[ Programming In Java : 400 ] = 45
[ Data Structures : 500 ] = 60
[ Introduction to Algorithms : 600 ] = 80
Now the keys of the TreeMap are sorted into the ascending order w.r.t. to the price due to custom order provided by the Comparator.

How to get the data from the TreeMap in java?

The get() method is used to get the value by key, it takes the key object and returns the value mapped with that key within the TreeMap. To get the data from the TreeMap, we must have the key of that particular value by using that key we can easily get the value from the TreeMap in Java.
Syntax of get() method
V value = treeMap.get(k); // Here k is the Key

When the TreeMap is constructed with default constructor

If the TreeMap is constructed with default constructor then the provided key in the argument of the get() method is compared with each key stored into the TreeMap using the compareTo() method. And when compareTo() method returns 0 i.e., key if found in the TreeMap and return the value mapped with the key.

Java TreeMap Example 3 : Using get() method of TreeMap, get the Quantity of the Book from the TreeMap

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);

Integer qty = bookMap.get("Programming In Java");
System.out.println("Quantity of Programming In Java = " + qty);
Output
Quantity of Programming In Java = 45

When the TreeMap is constructed with Comparator

If the TreeMap is constructed using Comparator then the provided key in the argument of the get() method is compared with each key stored into the TreeMap using the compare() method of Comparator. And when compare() method returns 0 i.e., key if found in the TreeMap and return the value mapped with the key.

Java TreeMap Example 4 : Using get() method of TreeMap, get the Quantity of the Book from the TreeMap

Comparator<Book> bookComparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
bookMap.put(new Book("Programming In Java", 400), 45);
bookMap.put(new Book("Introduction to Algorithms", 600), 80);
bookMap.put(new Book("Data Structures", 500), 60);
bookMap.put(new Book("Learn C++", 300), 35);

Book book = new Book("Programming In Java", 400);
Integer qty = bookMap.get(book);
System.out.println("Quantity of " + book + " = " + qty);
Output
Quantity of [ Programming In Java : 400 ] = 45
Note : If the key is not found then the null value is returned by the TreeMap in Java.

Collection Views Provided by TreeMap in Java

As we know, TreeMap is not a Collection because it doesn't implement the Collection interface. But TreeMap provide few methods that return equivalent object of the Collection from the TreeMap which is known as Collection Views. There are 3 types of Collection Views provided by the TreeMap :
  • Entry Set Collection View
  • Key Set Collection View
  • Values Collection View
Lets look at them one-by-one :

Entry Set Collection View

The entrySet() method of the TreeMap is used to get the Entry Set Collection View. This method provides a Set of all key-value pair stored into the TreeMap where each pair is an instance of Map.Entry<K, V> interface, this interface is a part of java.util package and each instance of this interface represents a key-value pair stored in the TreeMap.

Java TreeMap Example 5 : Iterate over the TreeMap through the Entry Set Collection View.

Comparator<Book> bookComparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
bookMap.put(new Book("Programming In Java", 400), 45);
bookMap.put(new Book("Introduction to Algorithms", 600), 80);
bookMap.put(new Book("Data Structures", 500), 60);
bookMap.put(new Book("Learn C++", 300), 35);

Set<Entry<Book, Integer>> entrySet = bookMap.entrySet();

entrySet.forEach(entry -> {
     System.out.println(entry.getKey() + " = " + entry.getValue());
});
Output
[ Learn C++ : 300 ] = 35
[ Programming In Java : 400 ] = 45
[ Data Structures : 500 ] = 60
[ Introduction to Algorithms : 600 ] = 80

Key Set Collection View

The keySet() method of the TreeMap is used to get the Ket Set Collection View. This method provides a Set which contains all the key object stored into the TreeMap.

Java TreeMap Example 6 : Print all the keys stored into the TreeMap

Comparator<Book> bookComparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
bookMap.put(new Book("Programming In Java", 400), 45);
bookMap.put(new Book("Introduction to Algorithms", 600), 80);
bookMap.put(new Book("Data Structures", 500), 60);
bookMap.put(new Book("Learn C++", 300), 35);

Set<Book> keySet = bookMap.keySet();

keySet.forEach(key -> System.out.println(key));

Output
[ Learn C++ : 300 ]
[ Programming In Java : 400 ]
[ Data Structures : 500 ]
[ Introduction to Algorithms : 600 ]

Values Collection View

The values() method of the TreeMap is used to get the Values Collection View. This method returns a Collection of all the values object stored into the TreeMap.

Java TreeMap Example 7 : Print all the values object stored into the TreeMap

Comparator<Book> bookComparator = (o1, o2) -> o1.getPrice() - o2.getPrice();
TreeMap<Book, Integer> bookMap = new TreeMap<>(bookComparator);
bookMap.put(new Book("Programming In Java", 400), 45);
bookMap.put(new Book("Introduction to Algorithms", 600), 80);
bookMap.put(new Book("Data Structures", 500), 60);
bookMap.put(new Book("Learn C++", 300), 35);

Collection<Integer> values = bookMap.values();

System.out.println(values);
Output
[35, 45, 60, 80]

How to check whether a key or a value is present or not within the TreeMap in Java?

TreeMap provides methods from that we can know whether it contains any particular key or value?. There are two methods to do this, those are containsKey() and containsValue().

Check whether the TreeMap contains the given Key or not?

To check whether a key is present or not within the TreeMap, it provides a method named as containsKey(). Internally containsKey() method calls get() method of TreeMap and return true if the get() method return any value otherwise it returns false.

Java TreeMap Example 8 : Using containsKey() method of TreeMap, check whether the "Programming In Java" present in the TreeMap or not

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);

System.out.println("Keys of the TreeMap : " + bookMap.keySet());
boolean isJavaBookExists = bookMap.containsKey("Programming In Java");
if(isJavaBookExists) {
       System.out.println("Programming In Java is present in the TreeMap");
}
Output
Keys of the TreeMap : [Data Structures, Introduction to Algorithms, Learn C++, Programming In Java]
Programming In Java is present in the TreeMap

Check whether the TreeMap contains the given Value or not?

To do this, we have a method named as containsValue(), the containsValue() method takes an argument and returns true if the TreeMap contains that argument as the Value Otherwise, it returns false.

Java TreeMap Example 9 : Using containsValue() method of TreeMap, check whether the bookMap contains any book having quantity with 45

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);

System.out.println("Values of the TreeMap : " + bookMap.values());
boolean isBookWith45Quantity = bookMap.containsValues(45);
if(isBookWith45Quantity) {
       System.out.println("Yes! there is a book having quantity 45.");
}
Output
Values of the TreeMap : [60, 80, 35, 45]
Yes! there is a book having quantity 45.

Reverse order view provided by the TreeMap in Java?

TreeMap provides few method which returns reverse order view of TreeMap i.e., all the entries of the TreeMap returned in the reverse order.

Reverse order view of TreeMap

The descendingMap() method is used to retrieve the reverse order view of TreeMap in Java.

Java TreeMap Example 10 : Using descendingMap() method of TreeMap, retrieve the reverse order view

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);
  
NavigableMap<String, Integer> descendingMap = bookMap.descendingMap();
Set<Entry<String, Integer>> entrySet = descendingMap.entrySet();
for (Entry<String, Integer> entry : entrySet) {
      System.out.println(entry.getKey() + " = " + entry.getValue());
}
Output
Programming In Java = 45
Learn C++ = 35
Introduction to Algorithms = 80
Data Structures = 60

Reverse order KeySet view of the TreeMap

The descendingKeySet() method is used to retrieve the reverse order keySet view of TreeMap in Java.

Java TreeMap Example 11 : Using descendingKeySet() method of TreeMap, retrieve the reverse order keySet view

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);
  
NavigableSet<String> descendingKeySet = bookMap.descendingKeySet();
descendingKeySet.forEach(key -> System.out.println(key));
Output
Programming In Java
Learn C++
Introduction to Algorithms
Data Structures

How to get subMap from the TreeMap in Java?

There are some methods through which we can fetch the sub-map from the TreeMap. The sub-map is also following the same sorted order as the actual TreeMap. These methods are :
  • subMap() : this method takes two arguments: fromKey and toKey and return a sub-map from the one key to the another key of the TreeMap.
    Syntax of subMap()
    SortedMap<K, V> subMap = treeMap.subMap(fromKey, toKey);
  • headMap() : this method takes an toKey argument and return a sub-map from the beginning of the TreeMap to just before the specified key node.
    Syntax of headMap()
    SortedMap<K, V> headMap = treeMap.headMap(toKey);
  • tailMap() : this method takes an fromKey argument and return a sub-map from the specified key to the end of the TreeMap.
    Syntax of tailMap()
    SortedMap<K, V> tailMap = treeMap.tailMap(fromKey);

Java TreeMap Example 12 : Using the above methods of TreeMap, retrieve the sub-maps

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);
bookMap.put("Computer Networks", 40);

System.out.println("Full TreeMap : " + bookMap);
  
SortedMap<String, Integer> subMap = bookMap.subMap("Data Structures", "Learn C++");
System.out.println("Sub Map : " + subMap);

SortedMap<String, Integer> headMap = bookMap.headMap("Learn C++");
System.out.println("Head Map : " + headMap);

SortedMap<String, Integer> tailMap = bookMap.tailMap("Learn C++");
System.out.println("Tail Map : " + tailMap);

Output
Full TreeMap : {Computer Networks=40, Data Structures=60, Introduction to Algorithms=80, Learn C++=35, Programming In Java=45}
Sub Map : {Data Structures=60, Introduction to Algorithms=80}
Head Map : {Computer Networks=40, Data Structures=60, Introduction to Algorithms=80}
Tail Map : {Learn C++=35, Programming In Java=45}

How to get first and last entry from the TreeMap in Java?

As we know TreeMap implements SortedMap i.e., all entries are placed into sorted order. Accordingly, we have methods to access the first and last entry of TreeMap. These methods are :
  • firstEntry() : Returns the first entry of the TreeMap
    Syntax of firstEntry()
    Entry<K, V> firstEntry = treeMap.firstEntry();
  • lastEntry() : Returns the last entry of the TreeMap
    Syntax of lastEntry()
    Entry<K, V> lastEntry = treeMap.lastEntry();
  • firstKey() : Returns the first key of the TreeMap
    Syntax of firstKey()
    K firstKey = treeMap.firstKey();
  • lastKey() : Returns the last key of the TreeMap
    Syntax of lastKey()
    K lastKey = treeMap.lastKey();

Java TreeMap Example 13 : fetch the first and last entry of the TreeMap

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);
bookMap.put("Computer Networks", 40);

System.out.println("Full TreeMap : " + bookMap);
  
Entry<String, Integer> firstEntry = bookMap.firstEntry();
System.out.println("First Entry : " + firstEntry);

Entry<String, Integer> lastEntry = bookMap.lastEntry();
System.out.println("Last Entry : " + lastEntry);

String firstKey = bookMap.firstKey();
System.out.println("First Key : " + firstKey);

String lastKey = bookMap.lastKey();
System.out.println("Last Key : " + lastKey);

Output
Full TreeMap : {Computer Networks=40, Data Structures=60, Introduction to Algorithms=80, Learn C++=35, Programming In Java=45}
First Entry : Computer Networks=40
Last Entry : Programming In Java=45
First Key : Computer Networks
Last Key : Programming In Java

Few more methods of TreeMap in Java

size() : returns the size of TreeMap i.e., how many key-value pairs are stored in the TreeMap
clear() : removes all key-value pairs from the TreeMap and make it empty.
isEmpty() : returns true if the Treemap is not containing any key-value pairs in it otherwise false

Java TreeMap Example 14 : Different States of TreeMap in Java

TreeMap<String, Integer> bookMap = new TreeMap<>();
bookMap.put("Programming In Java", 45);
bookMap.put("Introduction to Algorithms", 80);
bookMap.put("Data Structures", 60);
bookMap.put("Learn C++", 35);
bookMap.put("Computer Networks", 40);

System.out.println("How many key-value pairs are there in the TreeMap? : " + bookMap.size());

System.out.println("Removing all key-value pairs from the TreeMap.....");
bookMap.clear();

if(bookMap.isEmpty()) {
       System.out.println("TreeMap is Empty Now");
}
Output
How many key-value pairs are there in the TreeMap? : 5
Removing all key-value pairs from the TreeMap.....
TreeMap is Empty Now

Related Article


Conclusion

In this article, we have learned what is TreeMap in java. we look at the Hierarchy of the TreeMap. Also, the methods introduced by the TreeMap and understand many Java TreeMap Example.

Comments

Popular posts from this blog

How HashMap works internally in java

How Hashmap works Internally in Java In this article, we are going to see how HashMap internally works in java. Also, we will have a look at what Java 8 made changes on the internal working of Hashmap to make it faster. What is Hashmap and Hashing in java? How Hashmap works Internally in Java is majorly dependent upon the Hashing Principle. So, Before going to learn how HashMap works internally in java, lets first understand what is HashMap and hashing. HashMap : A HashMap is a map used to store mappings of key-value pairs. Also, it works on the Principle of Hashing. To know more about the HashMap, visit this article: HashMap in Java Hashing Principle : Simply, Hashing is a method used to produce an integer value from an object and this integer value known as the hash value. In HashMap, the key object is used for Hashing.
Internal Struture of the HashMap in java For internal working of HashMap, HashMap maintains an array of bucket, each bucket is a linked-list and linked list is a li…

HashMap in Java

HashMap in Java In this article, we are going to learn about the most important class of Java known as HashMap. HashMap in java is the most used map interface and it is used in our regular or day-to-day programming practices.
What is HashMap in java? Lets first start with basic definition of HashMap,
HashMap is a map which store key-value pair, where each key is mapped with a value.
HashMap defined in java.util package and implements map interface which provides functionalities to perform basic operation like store, retrieve, size of the map etc. It is a part of Java Collection Framework. Time complexity of HashMap in Java? : Time complexity to store and retrieve data from the HashMap is O(1) in the Best Case. But it can be O(n) in the worst case and after the changes made in Java 8 the worst case time complexity can be O(log n) atmost. Internal working of HashMap in java HashMap maintains an array of the buckets, where each bucket is a linked-list and the linked list is a list of…

ArrayList in Java

ArrayList in Java In this article, we are going to discuss ArrayList in Java. ArrayList is a class from the Java Collection Framework and widely in our programs. We use ArrayList as a substitute for the array. So, it is important to know ArrayList in detail. We are going to see what is ArrayList in Java, how we can create the instance of ArrayList and the methods provided by the ArrayList in Java.

If you already familiar to ArrayList, you may learn common programs related to ArrayList.
Table of ContentsWhat is ArrayList in Java?Hierarchy of the ArrayList in java?How to create objects of ArrayList in Java?How to create an ArrayList of Custom Objects?How to iterate ArrayList in Java?How to get the elements by index from ArrayList in Java?How to check whether an elements is stored in the ArrayList in Java?How to remove elements from the ArrayList in Java?Few more methods of ArrayList in Java?
What is ArrayList in Java? ArrayList in Java is a List in which we can store and retrieve elemen…