将这个list里面多个重复的值分别放入别的list
list{q,q,s,s,w,w,}
接着将重复的值取出来分别放入别的list里去
list{q.q}
list{w.w}
list{s.s}
4 个解决方案
#1
for (int i: list)
{
if (map.get(i) != null)
{
map.get(i).add(i);
} else
{
List<Integer> os= new ArrayList<Integer>();
os.add(i);
map.put(i, os);
}
}
map里就有以值为键的列表了。
#2
哥
求全代码
我MAP用的不怎么熟啊
刚测试报错
求解脱
求全代码
我MAP用的不怎么熟啊
刚测试报错
求解脱
#3
String [] arr = {"q","q","s","s","w","w"};
Map <String, List<String>> hm = new HashMap<String , List<String>>();
List <String>l = Arrays.asList(arr);
for(Iterator <String>it = l.iterator();it.hasNext();){
String a = it.next();
List <String> temp = hm.get(a);
if(temp == null){
temp = new ArrayList<String>();
hm.put(a, temp);
}
temp.add(a);
}
Map <String, List<String>> hm = new HashMap<String , List<String>>();
List <String>l = Arrays.asList(arr);
for(Iterator <String>it = l.iterator();it.hasNext();){
String a = it.next();
List <String> temp = hm.get(a);
if(temp == null){
temp = new ArrayList<String>();
hm.put(a, temp);
}
temp.add(a);
}
#4
放set里,然后再取出来放入list就不会重复
#1
for (int i: list)
{
if (map.get(i) != null)
{
map.get(i).add(i);
} else
{
List<Integer> os= new ArrayList<Integer>();
os.add(i);
map.put(i, os);
}
}
map里就有以值为键的列表了。
#2
哥
求全代码
我MAP用的不怎么熟啊
刚测试报错
求解脱
求全代码
我MAP用的不怎么熟啊
刚测试报错
求解脱
#3
String [] arr = {"q","q","s","s","w","w"};
Map <String, List<String>> hm = new HashMap<String , List<String>>();
List <String>l = Arrays.asList(arr);
for(Iterator <String>it = l.iterator();it.hasNext();){
String a = it.next();
List <String> temp = hm.get(a);
if(temp == null){
temp = new ArrayList<String>();
hm.put(a, temp);
}
temp.add(a);
}
Map <String, List<String>> hm = new HashMap<String , List<String>>();
List <String>l = Arrays.asList(arr);
for(Iterator <String>it = l.iterator();it.hasNext();){
String a = it.next();
List <String> temp = hm.get(a);
if(temp == null){
temp = new ArrayList<String>();
hm.put(a, temp);
}
temp.add(a);
}
#4
放set里,然后再取出来放入list就不会重复