必修 -- 常用笔试题

冒泡排序

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

    }
}
相关推荐
0712210981 分钟前
Spring Data Redis常见操作总结
java·redis·spring
查理不安生10 分钟前
【Mac】卸载JAVA、jdk
java·开发语言·macos
宝宝1.015 分钟前
Spring Boot出现java: 错误: 无效的源发行版:16的解决方式
java·spring boot·后端
《源码好优多》1 小时前
基于Java Springboot美食食谱推荐系统
java·spring boot·美食
程序媛小果1 小时前
基于java+ssm+Vue的校园美食交流系统设计与实现
java·vue.js·美食
it-fans1 小时前
IDEA 开发工具常用快捷键有哪些?
java·ide·intellij-idea
重生之绝世牛码1 小时前
Java设计模式 —— Java七大设计原则详解
java·大数据·开发语言·设计模式·设计原则
2402_857589361 小时前
汽车资讯新探索:Spring Boot技术引领
java·spring boot·后端
四月天行健1 小时前
【JavaEE】—— 创建线程的三种方式(Thread、Runnable、Callable)
java·开发语言
琪露诺大湿2 小时前
JavaEE-网络编程(2)
java·开发语言·网络·jvm·java-ee·tcp·1024程序员节