必修 -- 常用笔试题

冒泡排序

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

    }
}
相关推荐
李慕婉学姐5 小时前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
奋进的芋圆7 小时前
Java 延时任务实现方案详解(适用于 Spring Boot 3)
java·spring boot·redis·rabbitmq
sxlishaobin7 小时前
设计模式之桥接模式
java·设计模式·桥接模式
model20057 小时前
alibaba linux3 系统盘网站迁移数据盘
java·服务器·前端
荒诞硬汉7 小时前
JavaBean相关补充
java·开发语言
提笔忘字的帝国8 小时前
【教程】macOS 如何完全卸载 Java 开发环境
java·开发语言·macos
2501_941882488 小时前
从灰度发布到流量切分的互联网工程语法控制与多语言实现实践思路随笔分享
java·开发语言
華勳全栈8 小时前
两天开发完成智能体平台
java·spring·go
alonewolf_998 小时前
Spring MVC重点功能底层源码深度解析
java·spring·mvc
沛沛老爹8 小时前
Java泛型擦除:原理、实践与应对策略
java·开发语言·人工智能·企业开发·发展趋势·技术原理