In this article, we will discuss how to sort HashSet contents in ascending as well as descending order
Sorting HashSet in Ascending and Descending order:
Solution:
- Ascending order: by passing HashSet contents as argument to TreeSet inter-conversion constructor
- Descending order: by implementing Comparator interface & providing reverse sorting logic and finally add all elements of HashSet to TreeSet using addAll() method of Collection interface
1. Sorting HashSet contents in Ascending order
Syntax:
TreeSet<String> set = new TreeSet<String>(hashSet);
SortingHashSetInAscendingOrder.java
package in.bench.resources.java.collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
public class SortingHashSetInAscendingOrder {
public static void main(String[] args) {
// creating HashSet object of type String
HashSet<String> onlineMart = new HashSet<String>();
// adding elements to HashSet 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 : Random 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 : Random order
Flipkart
Jabong
PepperFry
Amazon
Shop Clues
Snapdeal
Myntra
After Sorting : Ascending order
Amazon
Flipkart
Jabong
Myntra
PepperFry
Shop Clues
Snapdeal
2. Sorting HashSet contents in Descending order
Syntax:
TreeSet<String> set = new TreeSet<String>(reverseCompLogic);
set.addAll(hashSet);
SortingHashSetInDescendingOrder.java
package in.bench.resources.java.collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
public class SortingHashSetInDescendingOrder {
public static void main(String[] args) {
// creating HashSet object of type String
HashSet<String> onlineMart = new HashSet<String>();
// adding elements to HashSet 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 : Random 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 HashSet 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 : Random order
Myntra
PepperFry
Jabong
Flipkart
Amazon
Snapdeal
Shop Clues
After Sorting : Descending order
Snapdeal
Shop Clues
PepperFry
Myntra
Jabong
Flipkart
Amazon
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/class-use/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!