必修 -- 常用笔试题

冒泡排序

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

    }
}
相关推荐
Leo July5 小时前
【Java】Spring Security 6.x 全解析:从基础认证到企业级权限架构
java·spring·架构
星火开发设计6 小时前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
码道功成6 小时前
Pycham及IntelliJ Idea常用插件
java·ide·intellij-idea
消失的旧时光-19436 小时前
第四篇(实战): 订单表索引设计实战:从慢 SQL 到毫秒级
java·数据库·sql
それども7 小时前
@ModelAttribute vs @RequestBody
java
雨中飘荡的记忆7 小时前
深度详解Spring Context
java·spring
Tao____7 小时前
JAVA开源物联网平台
java·物联网·mqtt·开源·ruoyi
yqd6667 小时前
SpringSecurity的使用
java·spring
仙俊红8 小时前
Java Map 家族核心解析
java·开发语言
一嘴一个橘子8 小时前
springMvc 接收参数、cookie、header
java