Java LinkedList shuffle

Introduction

To shuffle LinkedList in Java

Collections.shuffle(ll); 

Full source

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

public class Main {
  public static void main(String args[]) {

    // Create and initialize linked list.
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//from  w  ww .  j  av  a  2 s  . c  om
    ll.add(321);
    ll.add(20);
    ll.add(-20);
    ll.add(123);
    ll.add(8);

    // Sort list.
    Collections.sort(ll);
    System.out.println(ll);
    
    // Shuffle list. 
    Collections.shuffle(ll);  
    
    System.out.println(ll);
  }
}



PreviousNext

Related