iluwatar / 30 Seconds Of Java
Licence: mit
Collection of reusable tested Java 11 compatible code snippets that you can understand in 30 seconds or less.
Stars: ✭ 421
Programming Languages
java
68154 projects - #9 most used programming language
Projects that are alternatives of or similar to 30 Seconds Of Java
30 Seconds Of Code
Short JavaScript code snippets for all your development needs
Stars: ✭ 89,121 (+21068.88%)
Mutual labels: hacktoberfest, snippets
Lab
Lab wraps Git or Hub, making it simple to clone, fork, and interact with repositories on GitLab
Stars: ✭ 911 (+116.39%)
Mutual labels: hacktoberfest, snippets
30 Seconds Of Swift Code
A Swift implementation of 30-seconds-of-code: A curated collection of useful Swift 4 snippets that you can understand in 30 seconds or less.
Stars: ✭ 476 (+13.06%)
Mutual labels: hacktoberfest, snippets
Vscode Matlab
MATLAB support for Visual Studio Code
Stars: ✭ 114 (-72.92%)
Mutual labels: hacktoberfest, snippets
Bookmarks.dev
Bookmarks and Code Snippets Manager for Developers & Co
Stars: ✭ 218 (-48.22%)
Mutual labels: hacktoberfest, snippets
Beginners C Program Examples
Simple, Short and Sweet beginners friendly C language programs
Stars: ✭ 138 (-67.22%)
Mutual labels: hacktoberfest, snippets
Navi
An interactive cheatsheet tool for the command-line
Stars: ✭ 10,055 (+2288.36%)
Mutual labels: hacktoberfest, snippets
Java Design Patterns
Design patterns implemented in Java
Stars: ✭ 71,923 (+16983.85%)
Mutual labels: hacktoberfest, snippets
Packagedev
Tools to ease the creation of snippets, syntax definitions, etc. for Sublime Text.
Stars: ✭ 378 (-10.21%)
Mutual labels: hacktoberfest, snippets
Staged Recipes
A place to submit conda recipes before they become fully fledged conda-forge feedstocks
Stars: ✭ 415 (-1.43%)
Mutual labels: hacktoberfest
Tails
Tails is a (no-config) copy'n paste library of templates and components crafted using TailwindCSS
Stars: ✭ 416 (-1.19%)
Mutual labels: hacktoberfest
Awesome Persian Podcasts
لیست بهترین پادکستهای فارسی زبان 🔉
Stars: ✭ 414 (-1.66%)
Mutual labels: hacktoberfest
Bunit
A testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can easily interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJSRuntime, and perform snapshot testing.
Stars: ✭ 415 (-1.43%)
Mutual labels: hacktoberfest
Curriculum
The curriculum of Techtonica, a free tech training and job placement program for women and non-binary adults with low incomes.
Stars: ✭ 414 (-1.66%)
Mutual labels: hacktoberfest
Strongbox
Strongbox is an artifact repository manager.
Stars: ✭ 412 (-2.14%)
Mutual labels: hacktoberfest
Wordpress Plugin Boilerplate Powered
Wordpress Plugin Boilerplate but Powered with examples and a generator!
Stars: ✭ 413 (-1.9%)
Mutual labels: hacktoberfest
Digitalocean Cloud Controller Manager
Kubernetes cloud-controller-manager for DigitalOcean (beta)
Stars: ✭ 418 (-0.71%)
Mutual labels: hacktoberfest
30 Seconds of Java
Inspired by 30 seconds of code, this is a collection of reusable tested copy-pasteable Java 11 compatible code snippets that you can understand in 30 seconds or less. If you're interested in contributing to this library, please see the instructions.
Table of Contents
Algorithm
Array
Encoding
File
Math
Media
Networking
String
Class
I/O
Thread
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;
}
}
}
}
Selectionsort
public static void selectionSort(int[] arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
var minIndex = i;
for (var j = i + 1; j < len; j++) {
if(arr[j] < arr[minIndex])
minIndex = j;
}
var tmp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = tmp;
}
}
InsertionSort
public static void insertionSort(int[] arr) {
for (var i = 1; i < arr.length; i++) {
var tmp = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > tmp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 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;
}
Find maximum integer from the array
public static int findMax(int[] arr) {
return Arrays.stream(arr).reduce(Integer.MIN_VALUE, Integer::max);
}
Encoding
Base64 encode string
public static String encodeBase64(String input) {
return Base64.getEncoder().encodeToString(input.getBytes());
}
Base64 decode string
public static String decodeBase64(String input) {
return new String(Base64.getDecoder().decode(input.getBytes()));
}
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);
}
}
}
}
}
Zip a directory
public static void zipDirectory (String srcDirectoryName, String zipFileName) throws IOException {
var srcDirectory = new File(srcDirectoryName);
try (
var fileOut = new FileOutputStream(zipFileName);
var zipOut = new ZipOutputStream(fileOut)
) {
zipFile(srcDirectory, srcDirectory.getName(), zipOut);
}
}
public static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
throws IOException {
if (fileToZip.isHidden()) { // Ignore hidden files as standard
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName)); // To be zipped next
zipOut.closeEntry();
} else {
// Add the "/" mark explicitly to preserve structure while unzipping action is performed
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
var children = fileToZip.listFiles();
for (var childFile : children) { // Recursively apply function to all children
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
try (
var fis = new FileInputStream(fileToZip) // Start zipping once we know it is a file
) {
var zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
var bytes = new byte[1024];
var length = 0;
while ((length = fis.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;
}
Haversine formula
// Radius of sphere on which the points are, in this case Earth.
private static final double SPHERE_RADIUS_IN_KM = 6372.8;
public static double findHaversineDistance(double latA, double longA, double latB, double longB) {
if (!isValidLatitude(latA)
|| !isValidLatitude(latB)
|| !isValidLongitude(longA)
|| !isValidLongitude(longB)) {
throw new IllegalArgumentException();
}
// Calculate the latitude and longitude differences
var latitudeDiff = Math.toRadians(latB - latA);
var longitudeDiff = Math.toRadians(longB - longA);
var latitudeA = Math.toRadians(latA);
var latitudeB = Math.toRadians(latB);
// Calculating the distance as per haversine formula
var a = Math.pow(Math.sin(latitudeDiff / 2), 2)
+ Math.pow(Math.sin(longitudeDiff / 2), 2) * Math.cos(latitudeA) * Math.cos(latitudeB);
var c = 2 * Math.asin(Math.sqrt(a));
return SPHERE_RADIUS_IN_KM * c;
}
// Check for valid latitude value
private static boolean isValidLatitude(double latitude) {
return latitude >= -90 && latitude <= 90;
}
// Check for valid longitude value
private static boolean isValidLongitude(double longitude) {
return longitude >= -180 && longitude <= 180;
}
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);
}
Find Levenshtein distance
public static int findLevenshteinDistance(String word1, String word2) {
// If word2 is empty, removing
int[][] ans = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
ans[i][0] = i;
}
// if word1 is empty, adding
for (int i = 0; i <= word2.length(); i++) {
ans[0][i] = i;
}
// None is empty
for (int i = 1; i <= word1.length(); i++) {
for (int j = 1; j <= word2.length(); j++) {
int min = Math.min(Math.min(ans[i][j - 1], ans[i - 1][j]), ans[i - 1][j - 1]);
ans[i][j] = word1.charAt(i - 1) == word2.charAt(j - 1) ? ans[i - 1][j - 1] : min + 1;
}
}
return ans[word1.length()][word2.length()];
}
Compare Version
public static int compareVersion(String v1, String v2) {
Function<String, String[]> getVersionComponents = version -> version.replaceAll(".*?((?<!\\w)\\d+([.-]\\d+)*).*", "$1", "$1").split("\\.");
var components1 = getVersionComponents.apply(v1);
var components2 = getVersionComponents.apply(v2);
int length = Math.max(components1.length, components2.length);
for (int i = 0; i < length; i++) {
Integer c1 = i < components1.length ? Integer.parseInt(components1[i]) : 0;
Integer c2 = i < components2.length ? Integer.parseInt(components2[i]) : 0;
int result = c1.compareTo(c2);
if (result != 0) {
return result;
}
}
return 0;
}
Class
Get methods name
public static List<String> getAllMethods(final Class<?> cls) {
return Arrays.stream(cls.getDeclaredMethods())
.map(Method::getName)
.collect(Collectors.toList());
}
Get public field names
public static List<String> getAllFieldNames(final Class<?> cls) {
return Arrays.stream(cls.getFields())
.map(Field::getName)
.collect(Collectors.toList());
}
Get all field names
public static List<String> getAllFieldNames(final Class<?> cls) {
var fields = new ArrayList<String>();
var currentCls = cls;
while (currentCls != null) {
fields.addAll(
Arrays.stream(currentCls.getDeclaredFields())
.filter(field -> !field.isSynthetic())
.map(Field::getName)
.collect(Collectors.toList()));
currentCls = currentCls.getSuperclass();
}
return fields;
}
Create object
public static Object createObject(String cls)
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException,
ClassNotFoundException {
var objectClass = Class.forName(cls);
var objectConstructor = objectClass.getConstructor();
return objectConstructor.newInstance();
}
I/O
Read file by stream
public static List<String> readFile(String fileName) throws FileNotFoundException {
try (Stream<String> stream = new BufferedReader(new FileReader(fileName)).lines()) {
return stream.collect(Collectors.toList());
}
}
InputStream to String
public static String inputStreamToString(InputStream inputStream) throws IOException {
try (var reader = new BufferedReader(new InputStreamReader(inputStream))) {
var stringBuilder = new StringBuilder();
var data = reader.read();
while (data != -1) {
stringBuilder.append((char) data);
data = reader.read();
}
return stringBuilder.toString();
}
}
Thread
Create pool of threads
public static ExecutorService createFixedThreadPool() {
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].