Skip to content

Instantly share code, notes, and snippets.

@ifly6
Last active December 25, 2017 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifly6/aba75d7df292c840a69bab9ce596f5ea to your computer and use it in GitHub Desktop.
Save ifly6/aba75d7df292c840a69bab9ce596f5ea to your computer and use it in GitHub Desktop.
Randomly select 3 chaps
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Random3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // input
List<String> list = Stream.of(scan.nextLine().split(",")) // stream
.map(String::trim) // trim
.collect(Collectors.toList()); // collect to list
Collections.shuffle(list); // shuffle
System.out.println(list.stream().limit(3).collect(Collectors.toList())); // print first 3
scan.close(); // close, get rid of warning
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment