Sort BigInteger in Java

In this tutorial, I am attaching simple Java Program about, How to Sort BigInteger in Java. The example program were tested and shared in the same post.

Sorting BigInteger List Object

The following program is about, Sorting BigInteger List Object. The list object sorted by using predefined utility method called Collections.sort(List), Which is used to sort the given list in Java.

SortBigIntegerList.java

package com.dineshkrish.math;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 
* @author Dinesh Krishnan
*
*/
public class SortBigIntegerList {
public static void main(String[] args) {
// Defining BigInteger List Object
List<BigInteger> bigInterList = new ArrayList<BigInteger>();
bigInterList.add(new BigInteger("10000000000000000000001"));
bigInterList.add(new BigInteger("10000000000000000000004"));
bigInterList.add(new BigInteger("10000000000000000000002"));
bigInterList.add(new BigInteger("10000000000000000000007"));
bigInterList.add(new BigInteger("10000000000000000000003"));
bigInterList.add(new BigInteger("10000000000000000000009"));
bigInterList.add(new BigInteger("10000000000000000000008"));
bigInterList.add(new BigInteger("10000000000000000000006"));
bigInterList.add(new BigInteger("10000000000000000000005"));		
// Sorting
Collections.sort(bigInterList);
System.out.println(bigInterList);
}
}

Output

[10000000000000000000001, 10000000000000000000002, 10000000000000000000003, 10000000000000000000004, 10000000000000000000005, 10000000000000000000006, 10000000000000000000007, 10000000000000000000008, 10000000000000000000009]

Sorting BigInteger Set Object

The following program is about, Sorting BigInteger Set Object. The java.util.TreeSet class ability to sort the elements by default in Java, you can find the sample example below.

SortBigIntegerSet.java

package com.dineshkrish.math;
import java.math.BigInteger;
import java.util.Set;
import java.util.TreeSet;
/**
* 
* @author Dinesh Krishnan
*
*/
public class SortBigIntegerSet {
public static void main(String[] args) {
// Defining BigInteger Set Object
Set<BigInteger> bigInterSet = new TreeSet<BigInteger>();
bigInterSet.add(new BigInteger("10000000000000000000001"));
bigInterSet.add(new BigInteger("10000000000000000000004"));
bigInterSet.add(new BigInteger("10000000000000000000002"));
bigInterSet.add(new BigInteger("10000000000000000000007"));
bigInterSet.add(new BigInteger("10000000000000000000003"));
bigInterSet.add(new BigInteger("10000000000000000000009"));
bigInterSet.add(new BigInteger("10000000000000000000008"));
bigInterSet.add(new BigInteger("10000000000000000000006"));
bigInterSet.add(new BigInteger("10000000000000000000005"));
System.out.println(bigInterSet);
}
}

Output

[10000000000000000000001, 10000000000000000000002, 10000000000000000000003, 10000000000000000000004, 10000000000000000000005, 10000000000000000000006, 10000000000000000000007, 10000000000000000000008, 10000000000000000000009]

References

1. Java BigInteger API Documentation
2. Java Collections API Documentation
3. Java ArrayList API Documentation
4. Java TreeSet API Documentation
5. Java List API Documentation
6. Java Set API Documentation

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *