Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Sort' - 10 code snippet(s) found

 Sample 1. Get all elements greater than 2, sort them and then push them to a new set, using Lambda Expression

// Declare and Initialize the Collection

Set<Integer> intSet = new HashSet<Integer>();

// Add Elements


// Set the predicate or the condition for filtering the elements.

Predicate<Integer> moreThan2Pred = (p) -> (p > 1);

// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.

intSet =;

System.out.println(intSet); // Prints [2, 3, 4]

   Like      Feedback     lambda expression   collections   set  hashset  generics  stream   predicate   filter   sort  java.util.hashset

 Sample 2. Sort a List using Collections.sort and comparator

Collections.sort(listClassInfo,new Comparator<ClassInfoBean>(){
public int compare(ClassInfoBean s1,ClassInfoBean s2){
if( < 0){
return -1;
} else {
return 1;


   Like      Feedback     sorting   sort   Collections.sort   sort a list   comparator   compare method  java.util.Collections

 Sample 3. Sorting elements of a set using TreeSet

Set<String> mySet = new HashSet<String>();

System.out.println(mySet); // May be Sorted but sorting is not guaranteed

mySet = new TreeSet<String>(mySet);

System.out.println(mySet); // Sorted

   Like      Feedback     sorting  collections  treeset  set  hashset

 Sample 4. Sorting an arraylist using Collections.sort

List<String> myList = new ArrayList<String>();

System.out.println(myList); // Unsorted


System.out.println(myList); // Sorted

   Like      Feedback     list  arraylist  collections.sort  collections

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 5. Order Collection using google.common.collect.Ordering

Collection<Employee> sortedEmployeess = Ordering.from(new Comparator<Employee>() {
public int compare(Employee employee1, Employee employee2) {
return employee1.getAge() - employee2.getAge();

   Like      Feedback     collections  sorting collections   sorting collections using google library  comparator  compare method  google.common.collect.Ordering  google guava

 Sample 6. Code Sample / Example / Snippet of org.apache.hadoop.util.MergeSort

  public RawKeyValueIterator sort() {

MergeSort m = new MergeSort(this);

int count = super.count;

if (count == 0) return null;

int [] pointers = super.pointers;

int [] pointersCopy = new int[count];

System.arraycopy(pointers, 0, pointersCopy, 0, count);

m.mergeSort(pointers, pointersCopy, 0, count);

return new MRSortResultIterator(super.keyValBuffer, pointersCopy,

super.startOffsets, super.keyLengths, super.valueLengths);


   Like      Feedback      org.apache.hadoop.util.MergeSort

 Sample 7. Code Sample / Example / Snippet of org.apache.calcite.rel.core.Sort

    public RelNode convert(RelNode rel) {

final Sort sort = (Sort) rel;

final RelTraitSet traitSet =



return new MongoSort(rel.getCluster(), traitSet,

convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)),

sort.getCollation(), sort.offset, sort.fetch);


   Like      Feedback      org.apache.calcite.rel.core.Sort

 Sample 8. Code Sample / Example / Snippet of org.apache.ace.range.SortedRangeSet

    private void verifyStoreContents(final LogStoreImpl store, final int count, Writer... writers) throws IOException {

List<Descriptor> descriptors = store.getDescriptors();

long expectedID = 0;

for (Descriptor desc : descriptors) {

SortedRangeSet rangeSet = desc.getRangeSet();

RangeIterator rangeIter = rangeSet.iterator();

while (rangeIter.hasNext()) {

long id =;

Event expectedEntry = null;

for (int i = 0; (expectedEntry == null) && i < writers.length; i++) {

expectedEntry = writers[i].m_written.remove(id);


assertNotNull(expectedEntry, "Event ID #" + id + " never written?!");

assertEquals(expectedEntry.getID(), expectedID++, "Entry ID mismatch?!");




   Like      Feedback      org.apache.ace.range.SortedRangeSet

 Sample 9. Write a Program for selection sort.

public class SelectionSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      for(int i=0;i<a.length;i++){
         for(int j=0;j<i;j++){
            if(a[j] > a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
      for(int i=0;i<a.length;i++){

   Like      Feedback     sorting algorithm  selection sort

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner
 Sample 10. Write a program for bubble sort.

public class BubbleSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      for(int i=0;i<a.length-1;i++){
         for(int j=i;j<a.length;j++){
            if(a[j] < a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
      for(int i=0;i<a.length;i++){

   Like      Feedback     sorting algorithm  Bubble sort

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner

comments powered by Disqus