就字符串去重问题(求大神指点)越简单越好。。。

冲上云霄 、 2018-08-15 06:27:07
给出一个string="opt,pot,hi,max,ih,pot,tpo,mxa,hide",对其进行分组去重排序并输出,输出效果如下
opt,pot,tpo
hi,ih
max,mxa
hide
求大神给一个简单易懂的解决方法。。。
...全文
283 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_43062975 2018-08-25
  • 打赏
  • 举报
回复
private static Map<String, Set<String>> handle(String txt) {
//构建一个Key为所有字母,Value为Set集合的Map
Map<String, Set<String>> listMap = new HashMap<>();
//以逗号分隔字符串
String[] strings = txt.split(",");
//遍历所有数组
for (String s : strings) {
//将字符串排序
char[] chars = s.toCharArray();
Arrays.sort(chars);
String sortString = String.valueOf(chars);
//判断集合中是否存在以这些字母为Key的Set集合
//存在则在Set中插入这条数据,如果相同则为无效操作
//不存在则新建一个以字母为Key的Set集合,将此条String插入
if (listMap.containsKey(sortString)) {
Set<String> set = listMap.get(sortString);
set.add(s);
} else {
Set<String> set = new HashSet<>();
set.add(s);
listMap.put(String.valueOf(sortString), set);
}
}
return listMap;
}
冲上云霄 、 2018-08-16
  • 打赏
  • 举报
回复
引用 3 楼 qq_38182820 的回复:

private static Map<String, Set<String>> handle(String txt) {
//构建一个Key为所有字母,Value为Set集合的Map
Map<String, Set<String>> listMap = new HashMap<>();
//以逗号分隔字符串
String[] strings = txt.split(",");
//遍历所有数组
for (String s : strings) {
//将字符串排序
char[] chars = s.toCharArray();
Arrays.sort(chars);
String sortString = String.valueOf(chars);
//判断集合中是否存在以这些字母为Key的Set集合
//存在则在Set中插入这条数据,如果相同则为无效操作
//不存在则新建一个以字母为Key的Set集合,将此条String插入
if (listMap.containsKey(sortString)) {
Set<String> set = listMap.get(sortString);
set.add(s);
} else {
Set<String> set = new HashSet<>();
set.add(s);
listMap.put(String.valueOf(sortString), set);
}
}
return listMap;
}



感觉这个解决方法很巧妙,学习了...
冲上云霄 、 2018-08-16
  • 打赏
  • 举报
回复
感谢大佬们的回复学习了。。。。
Nautron_ 2018-08-16
  • 打赏
  • 举报
回复

private static Map<String, Set<String>> handle(String txt) {
//构建一个Key为所有字母,Value为Set集合的Map
Map<String, Set<String>> listMap = new HashMap<>();
//以逗号分隔字符串
String[] strings = txt.split(",");
//遍历所有数组
for (String s : strings) {
//将字符串排序
char[] chars = s.toCharArray();
Arrays.sort(chars);
String sortString = String.valueOf(chars);
//判断集合中是否存在以这些字母为Key的Set集合
//存在则在Set中插入这条数据,如果相同则为无效操作
//不存在则新建一个以字母为Key的Set集合,将此条String插入
if (listMap.containsKey(sortString)) {
Set<String> set = listMap.get(sortString);
set.add(s);
} else {
Set<String> set = new HashSet<>();
set.add(s);
listMap.put(String.valueOf(sortString), set);
}
}
return listMap;
}
  • 打赏
  • 举报
回复


String str="opt,pot,hi,max,ih,pot,tpo,mxa,hide";
Set<String> set=null;
Matcher m=null;
String[] arr=null;
List<Set<String>> list=new ArrayList<Set<String>>();
while(str.matches("[^,]+(?:,[^,]+)*")){
arr=str.split(",");
m=Pattern.compile("\\b["+arr[0]+"]+\\b").matcher(str);
set=new HashSet<String>();
while(m.find()){
set.add(m.group());
}
list.add(set);
str=str.replaceAll("\\b["+arr[0]+"]+,?\\b|,?["+arr[0]+"]+$", "");
}
System.out.println(list);

nayi_224 2018-08-16
  • 打赏
  • 举报
回复
	/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "opt,pot,hi,max,ih,pot,tpo,mxa,hide";
String[] strs = str.split(",");

String[][] arr2 = new String[strs.length][2];

for(int i = 0; i < strs.length; i++){
char[] tempArr = strs[i].toCharArray();
Arrays.sort(tempArr);
arr2[i][0] = strs[i];
arr2[i][1] = Arrays.toString(tempArr);
}

Map<String, String> map = new HashMap();

for(String[] arr1: arr2){
map.put(arr1[1], map.get(arr1[1]) == null ? arr1[0] : map.get(arr1[1]) + "," + arr1[0]);
}

System.out.println(map);
}


输出
{[d, e, h, i]=hide, [a, m, x]=max,mxa, [h, i]=hi,ih, [o, p, t]=opt,pot,pot,tpo}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧