
您有一个字符串列表:[“apple”、“banana”、“cherry”、“date”、“fig”、“grape”]。
编写一个代码片段来过滤掉以字母“b”开头的字符串,并将剩余的字符串收集到一个以逗号分隔的单个字符串中。
import java.util.arrays;import java.util.list;import java.util.stream.collectors;public class streamexample { public static void main(string[] args) { list fruits = arrays.aslist("apple", "banana", "cherry", "date", "fig", "grape"); // filter strings not starting with 'b' and join them into a single string string result = fruits.stream() .filter(fruit -> !fruit.startswith("b")) // exclude strings starting with 'b' .collect(collectors.joining(", ")); // join remaining strings with ", " system.out.println(result); // output: apple, cherry, date, fig, grape }}
说明
filter(fruit -> !fruit.startswith(“b”)):过滤掉以字母 ‘b’ 开头的字符串。
collectors.joining(“, “):将剩余字符串组合成一个字符串,用”, “分隔。
system.out.println(result):打印最终结果。
白瓜面试
白瓜面试 – AI面试助手,辅助笔试面试神器
40 查看详情
输出
对于输入 [“apple”, “banana”, “cherry”, “date”, “fig”, “grape”],输出将是:
apple, cherry, date, fig, grape
以上就是基于Java场景的面试题的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/614058.html
微信扫一扫
支付宝扫一扫