
准备Java开发人员面试?Stream API是面试中常见的考点,它以优雅的方式处理数据集合而闻名。本文将带您了解15道真实的Stream API面试题,助您掌握Java Stream。
问题1:在数组中查找最大元素
int arr[] = {5,1,2,8};int max = Arrays.stream(arr).max().getAsInt();
问题2:打印字符串中每个字符的计数
String str = "now is the winter";Map charFreq = Arrays.stream(str.split("")) .collect(Collectors.groupingBy( Function.identity(), Collectors.counting() ));
问题3:合并两个Person对象数组,按年龄升序排序,年龄相同则按姓名升序排序
立即学习“Java免费学习笔记(深入)”;
class Person { String name; int age; // constructors and getters}Person[] plist1 = {new Person("alice", 25), new Person("bob", 30), new Person("charlie", 25)};Person[] plist2 = {new Person("david", 30), new Person("eve", 25), new Person("alice", 25)};Stream.concat(Arrays.stream(plist1), Arrays.stream(plist2)) .sorted(Comparator.comparingInt(Person::getAge) .thenComparing(Person::getName)) .forEach(System.out::println);
问题4:查找字符串列表中最长字符串的长度
List names = Arrays.asList("alice", "bob", "charlie", "david", "eva");int maxLength = names.stream() .mapToInt(String::length) .max() .orElse(0);
问题5:检查整数列表是否包含素数
List numbers = Arrays.asList(4, 6, 8, 11, 12, 13, 14, 15);boolean hasPrime = numbers.stream() .anyMatch(num -> isPrime(num));private static boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= Math.sqrt(num); i++) if (num % i == 0) return false; return true;}
问题6:计算多个句子中不同单词(不区分大小写)的总数
List sentences = Arrays.asList( "Java Stream API provides a fluent interface", "It supports functional-style operations on streams", "In this exercise, you need to count words");long uniqueWords = sentences.stream() .map(x -> x.toLowerCase().split(" ")) .flatMap(Arrays::stream) .distinct() .count();
问题7:查找并连接前两个长度为偶数的单词
List words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");String result = words.stream() .filter(x -> x.length() % 2 == 0) .limit(2) .collect(Collectors.joining(""));
问题8:给定交易列表,查找每天的交易总额,并按日期排序
class Transaction { String date; long amount; // constructors and getters}List transactions = Arrays.asList( new Transaction("2022-01-01", 100), new Transaction("2022-01-01", 200), new Transaction("2022-01-02", 300));Map dailyTotals = transactions.stream() .collect(Collectors.groupingBy( Transaction::getDate, TreeMap::new, // Use TreeMap for sorted order Collectors.summingLong(Transaction::getAmount) ));
问题9:合并两个整数数组,排序,并过滤掉大于指定阈值的数字
白瓜面试
白瓜面试 – AI面试助手,辅助笔试面试神器
40 查看详情
int[] array1 = {1, 5, 3, 9, 7};int[] array2 = {2, 4, 6, 8, 10};int threshold = 7;IntStream.concat(Arrays.stream(array1), Arrays.stream(array2)) .boxed() .sorted(Comparator.naturalOrder()) .filter(x -> x <= threshold) // Corrected filter condition .forEach(System.out::println);
问题10:将员工记录列表转换为部门到平均工资的地图
class Employee { String department; double salary; // constructor and getters}Map deptAvgSalary = employees.stream() .collect(Collectors.groupingBy( Employee::getDepartment, Collectors.averagingDouble(Employee::getSalary) ));
问题11:将数字列表分成两组:素数和非素数
List numbers = Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9, 10);Map<Boolean, List> partitioned = numbers.stream() .collect(Collectors.partitioningBy(num -> isPrime(num)));
问题12:使用Stream生成斐波那契数列,最多n项
Stream.iterate(new int[]{0, 1}, arr -> new int[]{arr[1], arr[0] + arr[1]}) .limit(10) .map(arr -> arr[0]) .forEach(System.out::println);
问题13:按首字母分组字符串,并计算每个组的出现次数
List words = Arrays.asList("apple", "banana", "bear", "cat", "apple");Map frequency = words.stream() .collect(Collectors.groupingBy( str -> str.charAt(0), Collectors.counting() ));// output: {a=2, b=2, c=1}
问题14:使用Java Stream查找两个列表的交集
List list3 = Arrays.asList(1, 2, 3, 4, 5);List list4 = Arrays.asList(3, 4, 5, 6, 7);List intersection = list3.stream().filter(list4::contains).toList();System.out.println(intersection);
问题15:在Java中处理重复键时,如何将对象列表转换为排序的地图?
class Employee { int id; String name; // constructor and getters}List employees = Arrays.asList( new Employee(101, "Alice"), new Employee(102, "Bob"), new Employee(101, "Charlie"), new Employee(103, "David"), new Employee(102, "Eve"));Map<Integer, List> employeeMap = employees.stream() .collect(Collectors.groupingBy( Employee::getId, TreeMap::new, Collectors.toList() ));
解释:
Collectors.groupingBy(Employee::getId, TreeMap::new, Collectors.toList()):
按id分组(键)。使用TreeMap确保按键排序。使用Collectors.toList()将多个员工存储在同一键下。处理重复:如果多个员工具有相同的id,则将其存储在该键下的列表中。
希望这些题目和解答能帮助您在Java Stream API面试中取得好成绩! 记住,理解背后的逻辑比仅仅记住代码更重要。 多练习,多思考,才能真正掌握Stream API的精髓。
以上就是Java Stream API:面试问题每个开发人员都应练习的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/362860.html
微信扫一扫
支付宝扫一扫