给定的字符串中根据输入字母或单词进行查找或模糊查找
用到集合技术:
TreeMap 集合嵌套
TreeSet 优点:针对于这个问题来讲 自动排序,且不会出现重复元素
Collection 集合接收对应Map的值
具体代码如下
public static void main(String[] args) {
String source = "We've all met the type of individual who simply must take charge. Whether it's a" +
"decision-making session, a basketball game, or a family outing, they can't help grabbing the lead" +
"dog position and clinging on to it for dear life. They believe they're natural born leaders.";
//1 清理原始字符串的标点符号
source = source.toString().replaceAll(",","")
.replaceAll("'s", "")
.replaceAll("'t", "")
.replaceAll("'", "")
.replaceAll("\\.", "")
.toLowerCase();
//将整理好的单词放入数组中去
String []wordArray = source.split(" ");

本文介绍如何在Java中使用集合(如TreeMap和TreeSet)实现字符串中按字母或单词的查找和模糊查找功能。重点在于利用TreeMap的自动排序和去重特性,以及Collection接收Map值的方法。
最低0.47元/天 解锁文章
906

被折叠的 条评论
为什么被折叠?



