30 Seconds of Java
Inspired by 30 seconds of code, this is a collection of reusable tested Java 11 compatible code snippets that you can understand in 30 seconds or less.
How to contribute
Find an open issue that needs help at https://github.com/iluwatar/30-seconds-of-java/issues. Or if there's nothing that interests you, you can just raise a pull request. If you have an interesting snippet you would like to see in this library but can't implement yourself, please raise an issue.
For new snippets the general steps are
- Update
Library.java
with the new code snippet - Add a test for the new code snippet in
LibraryTest.java
- Finally insert the new snippet into this
README.md
- Submit pull request against
master
branch
Table of Contents
Algorithm
Array
- Generic two array concatenation
- Generic N array concatenation
- Check if all elements of array are equal
File
- List directories
- List files in directory
- List files in directory recursively
- Read lines from file to string list
- Zip file
- Zip multiple files
Math
Media
Networking
String
Class
Algorithm
Quicksort
public static void quickSort(int[] arr, int left, int right) {
var pivotIndex = left + (right - left) / 2;
var pivotValue = arr[pivotIndex];
var i = left;
var j = right;
while (i <= j) {
while (arr[i] < pivotValue) {
i++;
}
while (arr[j] > pivotValue) {
j--;
}
if (i <= j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
if (left < i) {
quickSort(arr, left, j);
}
if (right > i) {
quickSort(arr, i, right);
}
}
}
Bubblesort
public static void bubbleSort(int[] arr) {
var lastIndex = arr.length - 1;
for(var j = 0; j < lastIndex; j++) {
for(var i = 0; i < lastIndex - j; i++) {
if(arr[i] > arr[i + 1]) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
}
Array
Generic two array concatenation
public static <T> T[] arrayConcat(T[] first, T[] second) {
var result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
Generic N array concatenation
public static <T> T[] nArrayConcat(T[] first, T[]... rest) {
var totalLength = first.length;
for (var array : rest) {
totalLength += array.length;
}
var result = Arrays.copyOf(first, totalLength);
var offset = first.length;
for (var array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
Check if all elements of array are equal
public static <T> boolean allEqual(T[] arr) {
return Arrays.stream(arr).distinct().count() == 1;
}
File
List directories
public static File[] listDirectories(String path) {
return new File(path).listFiles(File::isDirectory);
}
List files in directory
public static File[] listFilesInDirectory(final File folder) {
return folder.listFiles(File::isFile);
}
List files in directory recursively
public static List<File> listAllFiles(String path) {
var all = new ArrayList<File>();
var list = new File(path).listFiles();
if (list != null) { // In case of access error, list is null
for (var f : list) {
if (f.isDirectory()) {
all.addAll(listAllFiles(f.getAbsolutePath()));
} else {
all.add(f.getAbsoluteFile());
}
}
}
return all;
}
Read lines from file to string list
public static List<String> readLines(String filename) throws IOException {
return Files.readAllLines(new File(filename).toPath());
}
Zip file
public static void zipFile(String srcFilename, String zipFilename) throws IOException {
var srcFile = new File(srcFilename);
try (
var fileOut = new FileOutputStream(zipFilename);
var zipOut = new ZipOutputStream(fileOut);
var fileIn = new FileInputStream(srcFile);
) {
var zipEntry = new ZipEntry(srcFile.getName());
zipOut.putNextEntry(zipEntry);
final var bytes = new byte[1024];
int length;
while ((length = fileIn.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
}
Zip multiple files
public static void zipFiles(String[] srcFilenames, String zipFilename) throws IOException {
try (
var fileOut = new FileOutputStream(zipFilename);
var zipOut = new ZipOutputStream(fileOut);
) {
for (var i=0; i<srcFilenames.length; i++) {
var srcFile = new File(srcFilenames[i]);
try (var fileIn = new FileInputStream(srcFile)) {
var zipEntry = new ZipEntry(srcFile.getName());
zipOut.putNextEntry(zipEntry);
final var bytes = new byte[1024];
int length;
while ((length = fileIn.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
}
}
}
Math
Fibonacci
public static int fibonacci(int n) {
if (n <= 1) return n;
else return fibonacci(n-1) + fibonacci(n-2);
}
Factorial
public static int factorial(int number) {
var result = 1;
for (var factor = 2; factor <= number; factor++) {
result *= factor;
}
return result;
}
Lottery
public static Integer[] performLottery(int numNumbers, int numbersToPick) {
var numbers = new ArrayList<Integer>();
for(var i = 0; i < numNumbers; i++) {
numbers.add(i+1);
}
Collections.shuffle(numbers);
return numbers.subList(0, numbersToPick).toArray(new Integer[numbersToPick]);
}
Greatest Common Divisor
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
Prime
public static boolean isPrime(int number) {
if (number < 3) {
return true;
}
// check if n is a multiple of 2
if (number % 2 == 0) {
return false;
}
// if not, then just check the odds
for (var i = 3; i * i <= number; i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
Media
Capture screen
public static void captureScreen(String filename) throws AWTException, IOException {
var screenSize = Toolkit.getDefaultToolkit().getScreenSize();
var screenRectangle = new Rectangle(screenSize);
var robot = new Robot();
var image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(filename));
}
Networking
HTTP GET
public static HttpResponse<String> httpGet(String uri) throws Exception {
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.build();
return client.send(request, BodyHandlers.ofString());
}
HTTP POST
public static HttpResponse<String> httpPost(String address, HashMap<String,String> arguments) throws IOException, InterruptedException{
var sj = new StringJoiner("&");
for(var entry : arguments.entrySet()) {
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
+ URLEncoder.encode(entry.getValue(), "UTF-8"));
}
var out = sj.toString().getBytes(StandardCharsets.UTF_8);
var request = HttpRequest.newBuilder()
.uri(URI.create(address))
.headers("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.POST(BodyPublishers.ofByteArray(out))
.build();
return HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
}
String
Palindrome check
public static boolean isPalindrome(String s) {
var sb = new StringBuilder();
for (var c : s.toCharArray()) {
if (Character.isLetter(c)) {
sb.append(c);
}
}
var forward = sb.toString().toLowerCase();
var backward = sb.reverse().toString().toLowerCase();
return forward.equals(backward);
}
Reverse string
public static String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}
String to date
public static Date stringToDate(String date, String format) throws ParseException {
var simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.parse(date);
}
Anagram Check
public boolean isAnagram(String s1, String s2) {
var l1 = s1.length();
var l2 = s2.length();
var arr1 = new int[256];
var arr2 = new int[256];
if (l1 != l2) {
return false;
}
for (var i = 0; i < l1; i++) {
arr1[s1.charAt(i)]++;
arr2[s2.charAt(i)]++;
}
return Arrays.equals(arr1, arr2);
}
Class
Get methods name
public static List<String> getAllMethods(final Class<?> cls) {
var list = new ArrayList<String>();
for (var method : cls.getDeclaredMethods()) {
list.add(method.getName());
}
return list;
}
Get fields name
public static List<String> getAllFieldNames(final Class<?> cls) {
return Arrays.stream(cls.getFields())
.map(Field::getName)
.collect(Collectors.toList());
}