• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iluwatar/30-seconds-of-java: Collection of reusable tested Java 11 compatible co ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

iluwatar/30-seconds-of-java

开源软件地址:

https://github.com/iluwatar/30-seconds-of-java

开源编程语言:

Java 100.0%

开源软件介绍:

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

Quicksort link
Bubblesort link
Selectionsort link
Insertionsort link
Countingsort link

Array

Generic two array concatenation link
Generic N array concatenation link
Check if all elements of array are equal link
Find maximum integer from the array link

Encoding

Encode Base64 link
Decode Base64 link

File

List directories link
List files in directory link
List files in directory recursively link
Read lines from file to string list link
Zip file link
Zip multiple files link
Zip a directory link

Math

Factorial link
Fibonacci link
Haversine Formula link
Lottery link
[Luhn algorithm](#Luhn algorithm) link
Greatest Common Divisor link
Prime link
Natural Number Binary Conversion link

Media

Capture screen link

Networking

HTTP GET link
HTTP POST link

String

Palindrome check link
Reverse string link
String to date link
Anagram Check link
Find Levenshtein distance link
Compare Version link

Class

Get methods name link
Get public field names link
Get all field names link
Create object link

I/O

Read file by stream link
InputStream to String link

Thread

Create pool of threads link

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;
    }
  }

CountingSort

  public static void countingSort(int[] arr) {
    var max = Arrays.stream(arr).max().getAsInt();

    var count = new int[max + 1];

    for (var num : arr) {
      count[num]++;
    }

    for (var i = 1; i <= max; i++) {
      count[i] += count[i - 1];
    }

    var sorted = new int[arr.length];
    for (var i = arr.length - 1; i >= 0; i--) {
      var cur = arr[i];
      sorted[count[cur] - 1] = cur;
      count[cur]--;
    }

    var index = 0;
    for (var num : sorted) {
      arr[index++] = num;
    }
  }

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, se 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap