org.apache.commons.collections4.list.TreeList Example


What is org.apache.commons.collections4.list.TreeList class? How to use org.apache.commons.collections4.list.TreeList class?



org.apache.commons.collections4.list.TreeList class is a Java class offered in commons-collections4.jar that implements the java.util.List interface using a tree structure internally for fast insertions and removals at any index in the list

Here is a simple example of using org.apache.commons.collections4.list.TreeList class:

// Copyright (c) 2016
import java.util.List;
import org.apache.commons.collections4.list.TreeList;

// Example of using the TreeListExample class
public class TreeListExample {
   public static void main(String[] args) throws Exception {

      // Create some objects
      String apple = "Apple";
      String orange = "Orange";
      String banana = "Banana";

      // Create a TreeList list
      List<String> list = new TreeList<String>();
      // insert objects into the list from the beginning position
      list.add(0, apple);
      list.add(0, orange);
      list.add(0, apple);
      list.add(0, orange);
      list.add(0, apple);
      list.add(0, banana);

      // loop through the set as a list
      for (int i=0; i<list.size(); i++) {
         String obj = list.get(i);
         System.out.println(i+": " + obj);

You can compile and run the above example in a command window as shown below:

   -cp C:\local\commons-collections4-4.1\commons-collections4-4.1.jar

   -cp .;C:\local\commons-collections4-4.1\commons-collections4-4.1.jar 

0: Banana
1: Apple
2: Orange
3: Apple
4: Orange
5: Apple


Using commons-collections.jar in Java Programs

⇒⇒FAQ for Apache commons-collections.jar

2017-05-20, 307👍, 0💬