Tutorials

Java Collection Programs


LinkedList Sort


In this chapter, we will

  • Create linkedlist
  • Add elements to linkedlist
  • Sort the current linkedlist
  • Travese the sorted list using iterator
  • Print list

Example:


package javaRadarLinkedList;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class SortLinkedList {

     public static void main(String[] args) {

       //create Linked List
         LinkedList javaRadarList = new LinkedList();

        //Add elements to LinkedList
          javaRadarList.add("Java");
          javaRadarList.add("Jquery");
          javaRadarList.add("Spring");
          javaRadarList.add("Hibernate");
          javaRadarList.add("EJB");

           System.out.println("Before sorting: " +javaRadarList);

          //sorting list element
            Collections.sort(javaRadarList);

             Iterator itrAfterSort = javaRadarList.iterator();
             System.out.println("After Sorting:");

             while(itrAfterSort.hasNext()){

                String token = itrAfterSort.next();
                System.out.println(token);

            }

     }

}

OUTPUT:

Before sorting: [Java, Jquery, Spring, Hibernate, EJB]
After Sorting:
EJB
Hibernate
Java
Jquery
Spring
 


Please Share this page
Views : 65
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!