62,623
社区成员
发帖
与我相关
我的任务
分享
package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class Example2 {
public int checksame(List list1,List list2) {
int result = 0;
HashMap hashMap1 = check(list1);
HashMap hashMap2 = check(list2);
for (Iterator iter = hashMap1.keySet().iterator(); iter.hasNext();) {
Object ob = (Object) iter.next();
if (hashMap2.containsKey(ob) && hashMap2.get(ob).equals(hashMap1.get(ob))) {
result++;
}
}
return result;
}
public HashMap check(List list) {
HashMap hashMap = new HashMap();
for (int i = 0; i < list.size(); i++) {
Object ob = list.get(i);
int s = 0;
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object obj = (Object) iter.next();
if (obj.equals(ob)) {
s++;
}
}
hashMap.put(ob, s);
}
return hashMap;
}
public static void main(String[] args) {
Example2 example = new Example2();
List col1 = new ArrayList();
col1.add("x");
col1.add("y");
col1.add("x");
col1.add("y");
col1.add("z");
List col2 = new ArrayList();
col2.add("z");
col2.add("x");
col2.add("x");
col2.add("q");
col2.add("y");
col2.add("y");
col2.add("y");
col2.add("y");
System.out.println(example.checksame(col1, col2));
}
}