fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.LinkedHashSet;
  7. import java.util.Set;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. // LinkedHashSet de elementos tipo Integer.
  15. Set<Integer> lhset = new LinkedHashSet<Integer>();
  16. // Agrega elementos.
  17. lhset.add(12);
  18. lhset.add(1);
  19. lhset.add(57);
  20. lhset.add(0);
  21. lhset.add(9);
  22. lhset.add(34);
  23. System.out.println("Set original: " + lhset);
  24.  
  25.  
  26. //Crea un ArrayList a partir del Set conteniendo los valores originales.
  27. List<Integer> shuffleMe = new ArrayList<Integer>(lhset);
  28. //Permuta aleatoriamente la lista.
  29. Collections.shuffle(shuffleMe);
  30. //Crea un nuevo Set en donde se almacenarán los datos ordenados aleatoriamente.
  31. Set<Integer> shuffledSet = new LinkedHashSet<Integer>();
  32. //Agrega los valores ordenados alatoriamente a un nuevo Set.
  33. shuffledSet.addAll(shuffleMe);
  34.  
  35. System.out.println("Set desordenado : " + shuffledSet);
  36. }
  37. }
Success #stdin #stdout 0.09s 27484KB
stdin
Standard input is empty
stdout
Set original: [12, 1, 57, 0, 9, 34]
Set desordenado : [1, 0, 57, 34, 9, 12]