ArrayList in Java Tutorial with Example

ArrayList class implements List interface. Due to dynamic nature of ArrayList most developers choose ArrayList over Array. It is a resizable array implementation of the List interface. It implements all optional list operations,and permits all elements, including null.

The issue with Array is that they are of fixed length so if it is full you can not resize it and add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same  as it doesn't shrink. On the other hand ArrayList can dynamically grow and shrink after addition and removal operations.

This class is roughly equivalent to Vector, except that it is unsynchronized.The iterators returned by this class's iterator are fail-fast.

There is a list of several ArrayList tutorials at the end of this guide, refer it to gain the understanding of ArrayList class.

How to Iterate TreeMap Keys in Reverse Order with Example

In the last tutorial we have discussed how to sort TreeMap by value. TreeMap elements are sorted in ascending order of keys by default. What if we want to sort it by descending order of keys. In this tutorial we will sort the TreeMap keys in descending order.

Read Also :   How to iterate a TreeMap in Java

Program to Iterate TreeMap in Reverse Order


import java.util.*;

 public class TreeMapKeysInDescendingOrder {
    public static void main(String args[]) {
        
    // Declaring a TreeMap of String keys and String values
    TreeMap<String, String> treemap = 
                new TreeMap<String, String>(Collections.reverseOrder());
    // Add Key-Value pairs to TreeMap
    treemap.put("Key1", "Pear");
    treemap.put("Key2", "Apple");
    treemap.put("Key3", "Orange");
    treemap.put("Key4", "Papaya");
    treemap.put("Key5", "Banana");