冒泡排序
            
            
              java
              
              
            
          
          public class BubbleSort {
    public static void main(String[] args) {
        int[] numbers = {64, 34, 25, 12, 22, 11, 90};
        bubbleSort(numbers);
        for (int number : numbers) {
            System.out.print(number + " ");
        }
    }
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}二分算法
            
            
              java
              
              
            
          
           public static int binarySearch(int[] arr, int target) {
        int left = 0, right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2; //拿到二分下表
            if (arr[mid] == target) {
                return mid;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }大文件拆分合并
            
            
              java
              
              
            
          
          /**
 * Author: 徐寿春
 * Date:    2024/11/15 10:33
 * <p>
 * 名称
 */
public class FileSplitter {
    public static void splitFile(File inputFile, int chunkSize) throws Exception {
        @Cleanup FileInputStream fis = new FileInputStream(inputFile);
        @Cleanup BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] buffer = new byte[chunkSize];
        String fileName = inputFile.getName();
        int tmp;
        while ((tmp = bis.read()) != -1) {
            String filePartName = String.format("%s.%03d", fileName, tmp);
            //创建小文件
            File newFile = new File(inputFile.getParent(), filePartName);
            @Cleanup FileOutputStream out = new FileOutputStream(newFile);
            out.write(buffer, 0, tmp); // tmp是实际读取的字节数
        }
    }
    public static void mergeFiles(File[] inputFiles, File outputFile) throws Exception {
        @Cleanup FileOutputStream fos = new FileOutputStream(outputFile);
        @Cleanup BufferedOutputStream mergingStream = new BufferedOutputStream(fos);
        for (File file : inputFiles) {
            @Cleanup FileInputStream fis = new FileInputStream(file);
            @Cleanup BufferedInputStream mingStream = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = mingStream.read(buffer)) != -1) {
                mergingStream.write(buffer, 0, bytesRead);
            }
        }
    }
    public static void main(String[] args) throws Exception {
        File inputFile = new File("path/to/large/file.txt");
        int chunkSize = 1024 * 1024 * 10; // 10MB
        splitFile(inputFile, chunkSize);
        File outputFile = new File("path/to/output/file.txt");
        File[] inputFiles = new File[]{
                new File("path/to/large/file.txt.001"),
                new File("path/to/large/file.txt.002"),
                // ...
        };
        mergeFiles(inputFiles, outputFile);
    }
}统计并返回出现次数最多的单词及其出现次数
给定一个包含多行文本的字符串(每行以换行符\n分隔),要求编写一个Java方法来实现以下功能:统计并返回字符串中所有单词的总数(假设单词由空格分隔,且单词之间可以有多个空格,单词只有英文字符)。统计并返回出现次数最多的单词及其出现次数(如果有多个单词出现次数相同且最多,则返回其中任意一个)。统计并返回字符串中所有不同单词的数量。
例子:String text = "Hello world. This is a test.\n Another test, with Hello again.";
            
            
              java
              
              
            
          
          /**
 * Author: 徐寿春
 * Date:    2024/11/15 11:09
 * <p>
 * 名称
 */
public class WordStatistics {
    public static void wordStatistics(String str) {
        Map<String, Long> collect = Arrays.stream(str.split("")).filter(i ->
                !Objects.equals(i, ",")
                        && !Objects.equals(i, ".")
                        && Strings.isNotBlank(i)
        ).collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));
        collect.entrySet().stream().max(Map.Entry.comparingByValue()).ifPresent(i -> {
            System.out.println("出现最多的单词 = " + i.getKey());
            System.out.println("出现最多的数量 = " + i.getValue());
        });
        System.out.println("共出现的数量 = " + collect.entrySet().size());
    }
    public static void main(String[] args) {
        String text = "Hello world. This is a test.\n Another test, with Hello again.";
        wordStatistics(text);
    }
}