Converting Ordered ArrayList into Sorted TreeSet

In this article, we will discuss how to convert Ordered ArrayList into Sorted TreeSet

We can sort ArrayList in both ascending and descending order, by passing ArrayList contents as argument to TreeSet’s inter-conversion constructor

 

Solution:

  1. Ascending order: use TreeSet, by passing ArrayList contents as arguments to inter-conversion constructor
  2. Descending order: use TreeSet, by implementing Comparator interface and providing reverse sorting logic and finally add all elements of ArrayList to TreeSet using addAll() method of Collection interface

 

Ascending-order :

1. Converting Ordered ArrayList to Sorted TreeSet in ascending-order

Syntax:

TreeSet<String> set = new TreeSet<String>(arrayList);

SortingArrayListInAscendingOrder.java

package in.bench.resources.java.collections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

public class SortingArrayListInAscendingOrder {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> onlineMart = new ArrayList<String>();

		// adding elements to ArrayList object
		onlineMart.add("Flipkart");
		onlineMart.add("PepperFry");  
		onlineMart.add("Snapdeal");  
		onlineMart.add("Myntra");
		onlineMart.add("Amazon");
		onlineMart.add("Shop Clues");
		onlineMart.add("Jabong");

		System.out.println("Before Sorting : Insertion order\n");

		// Iterating using Iterator
		Iterator<String> strIterator = onlineMart.iterator();
		while(strIterator.hasNext()) {
			System.out.println(strIterator.next());
		}

		// convert to TreeSet
		TreeSet<String> ts = new TreeSet<String>(onlineMart);


		System.out.println("\n\n\nAfter Sorting : Ascending order\n");

		// Iterating using Iterator
		Iterator<String> ascSorting = ts.iterator();
		while(ascSorting.hasNext()) {
			System.out.println(ascSorting.next());
		}
	}
}

Output:

Before Sorting : Insertion order

Flipkart
PepperFry
Snapdeal
Myntra
Amazon
Shop Clues
Jabong



After Sorting : Ascending order

Amazon
Flipkart
Jabong
Myntra
PepperFry
Shop Clues
Snapdeal

 

Descending-order :

2. Converting Ordered ArrayList to Sorted TreeSet in descending-order

Syntax:

TreeSet<String> set = new TreeSet<String>(reverseCompLogic);

set.addAll(arrayList);

SortingArrayListInDescendingOrder.java

package in.bench.resources.java.collections;

import java.util.Comparator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

public class SortingArrayListInDescendingOrder {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> onlineMart = new ArrayList<String>();

		// adding elements to ArrayList object
		onlineMart.add("Flipkart");
		onlineMart.add("PepperFry");  
		onlineMart.add("Snapdeal");  
		onlineMart.add("Myntra");
		onlineMart.add("Amazon");
		onlineMart.add("Shop Clues");
		onlineMart.add("Jabong");

		System.out.println("Before Sorting : Insertion order\n");

		// Iterating using Iterator
		Iterator<String> strIterator = onlineMart.iterator();
		while(strIterator.hasNext()) {
			System.out.println(strIterator.next());
		}


		// using Comparator constructor argument of TreeSet
		TreeSet<String> ts = new TreeSet<String>(new Comparator<String>(){

			@Override
			public int compare(String o1, String o2) {
				// reverse sorting logic
				return o2.compareTo(o1);
			}
		});

		// add ArrayList elements to TreeSet
		ts.addAll(onlineMart);


		System.out.println("\n\n\nAfter Sorting : Descending order\n");

		// Iterating using Iterator
		Iterator<String> ascSorting = ts.iterator();
		while(ascSorting.hasNext()) {
			System.out.println(ascSorting.next());
		}
	}
}

Output:

Before Sorting : Insertion order

Flipkart
PepperFry
Snapdeal
Myntra
Amazon
Shop Clues
Jabong



After Sorting : Descending order

Snapdeal
Shop Clues
PepperFry
Myntra
Jabong
Flipkart
Amazon

 

Important points:

  • ArrayList stores elements as per insertion-order
  • TreeSet stores elements in sorted-order

 

References:

 

Happy Coding !!
Happy Learning !!

How to get maximum element from ArrayList
Sorting HashSet contents in ascending and descending order