In Java, you can use
collections.shuffle
to clean or rerun a ArrayList
TestApp.java
package com.mkyong.utils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestApp {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D", "1", "2", "3");
//before shuffle
System.out.println(list);
// again, same insert order
System.out.println(list);
// shuffle or randomize
Collections.shuffle(list);
System.out.println(list);
System.out.println(list);
// shuffle again, different result
Collections.shuffle(list);
System.out.println(list);
}
}
Output
[A, B, C, D, 1, 2, 3]
[A, B, C, D, 1, 2, 3]
[2, B, 3, C, 1, A, D]
[2, B, 3, C, 1, A, D]
[C, B, D, 2, 3, A, 1]
References
- Collections.shuffle JavaDoc
Read More:
- java.lang.UnsupportedOperationException resolvent
- Mybatis custom list collection parser
- Java 8 Stream – Read a file line by line
- Conversion between list and string array
- Java String.split () special character processing
- [Two Sigma OA] Longest Chain
- Java uses regular expressions to intercept the contents between specified strings
- Getting started with jmu-java-m01-scanner
- Java uses lock to realize multithread sequential alternate execution mode 2
- Java retainAll throws an unsupported operation exception record
- Java gets the type t.class of generic t
- Java compareto() method
- Implementation of multithread sequential alternate execution by using lock in Java
- Three ways to get form data in struct2
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Related configuration of mybatis project
- Struts 2 encapsulates form data into list and map sets
- How to call stored procedure in Hibernate
- Java uses the same event listener for the same type of component
- Compare whether two sets are the same in Java