初学java,在collections碰到两个题目,不知道该怎么做,求高手解决。谢谢。第一道
编写一个类,用equals()和hashCode()方法判断在两个集合中出现的次数相同的元素。例如{‘x’,’y’,’x’,’y’,’z’}和{‘z’,’x’,’x’,’q’,’y’,’y’,’y’,’y’}应返回2。因为’x’,’z’在两个集合中出现的次数相同第二道
编写一个类,封装一个规则{‘x’,’z’,’x’,’y’},定义’z’不能在’x’后边,’x’不能在’z’后边, ’y’不能在’x’后边,规则里的元素可以是任何类型,但非空。
方法donotFollow(‘object’).根据规则返回不能在传递的参数后面的元素集合。例如, 对于{‘x’,’z’,’x’,’y’}, donotFollow(‘x’)应返回{‘z’,’y’}
方法delete(List<T>)对元素迭代删除违反以上规则的相邻元素。例如,对{"x", "z", "y", "a", "z", "x"},zyx应被删除,返回{‘x’,’a’,’z’}.

解决方案 »

  1.   

    以下是第二道的解法,虽然可以,但是可能效率不是很高package com;import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;public class Example { public ArrayList donotfollow(Object ob) {

    Object[][] standard = {
    {"x","z"},
    {"y","x"},
    {"z","x"}
    };

    ArrayList result = new ArrayList();

    for (int i = 0; i < standard.length; i++) {
    if (ob.equals(standard[i][1])) {
    result.add(standard[i][0]);
    }
    }

    return result;
    }

    public List delete(List list) {

    for (int i = 0; i < list.size(); i++) {
    Object ob = list.get(i);
    if (i+1 < list.size()&& donotfollow(ob).contains(list.get(i+1))) {
    list.remove(i+1);
    i--;
    }
    }

    return list;
    }

    public static void main(String[] args) {

    List list = new ArrayList();
    list.add("x");
    list.add("z");
    list.add("y");
    list.add("a");
    list.add("z");
    list.add("x");

    Example example = new Example();
    List result = example.delete(list);

    for (Iterator iter = result.iterator(); iter.hasNext();) {
    String str = (String) iter.next();
    System.out.println(str);
    }
    }
    }
      

  2.   

    第一道:
    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));
    }
    }