遍历一下list,判断对象的字段值,满足条件的remove

解决方案 »

  1.   

    额,这就是要遍历吧!找到了符合条件的就调用list的remove方法
      

  2.   


    package test;
    import java.util.ArrayList;
    import java.util.List;public class Test {
    public static void main(String[] args) {
    Test test = new Test();
    A a1 = test.new A("100", "200");
    A a2 = test.new A("100", "300");
    List<A> list = new ArrayList<A>();
    list.add(a1);
    list.add(a2);
    for (A tmpA : list) {
    if (tmpA.getKey1().equals("100") && tmpA.getKey2().equals("200")) {
    list.remove(tmpA);
    System.out.println("remove " + tmpA.getKey1() + "  " + tmpA.getKey2());
    }
    }
    } class A {
    A(String key1, String key2) {
    this.key1 = key1;
    this.key2 = key2;
    } public String getKey1() {
    return key1;
    } public String getKey2() {
    return key2;
    } String key1;
    String key2;
    String key3;
    String key4;
    String value1;
    int value2;
    }
    }
      

  3.   

    List a=new ArrayList();
    遍历原来的list,把符合条件的放到新的a的list中
      

  4.   

    按你的要求遍历了,删除了!没有问题!
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;public class Test {
    public static void main(String[] args) {
    A a1 = new A("100", "200");
            A a2 = new A("100", "300");
            List<A> list = new ArrayList<A>();
            list.add(a1);
            list.add(a2);
            Iterator<A> iter=list.iterator();
            while(iter.hasNext()){
             A a=iter.next();
             if(a.key1.equals("100")&&a.key2.equals("200")){
             iter.remove();
             }
            }
            System.out.println(list.size());
    }
    }class A{
    A(String key1, String key2) {
            this.key1 = key1;
            this.key2 = key2;
        }
    String key1;
    String key2;
    String key3;
    String key4;
    String value1;
    int value2;
    }
      

  5.   

    谢谢,是我把题目理解错了,如果是对list进行排序,优先级为key1,key2,key3,key4要怎么做?
      

  6.   

    谢谢,是我把题目理解错了,如果是对list进行排序,优先级为key1,key2,key3,key4要怎么做?
      

  7.   

    谢谢,是我把题目理解错了,如果是对list进行排序,优先级为key1,key2,key3,key4要怎么做?
      

  8.   


    package test;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    /**
     * @date 2014-06-05
     * @author amadowang
     *
     */
    public class Test {
    public static void main(String[] args) {
    Test test = new Test();
    A a1 = test.new A("100", "200", "200", "100");
    A a2 = test.new A("600", "600", "100", "400");
    A a3 = test.new A("200", "200", "300", "600");
    A a4 = test.new A("300", "800", "200", "200");
    A a5 = test.new A("200", "100", "700", "500");
    A a6 = test.new A("300", "800", "200", "500");
    A a7 = test.new A("300", "800", "400", "500");
    List<A> list = new ArrayList<A>();
    list.add(a1);
    list.add(a2);
    list.add(a3);
    list.add(a4);
    list.add(a5);
    list.add(a6);
    list.add(a7); ComparatorA comparator = test.new ComparatorA();
    Collections.sort(list, comparator);
    for (A tmpA : list) {
    System.out.println(tmpA.getKey1() + "  " + tmpA.getKey2() + " " + tmpA.getKey3() + "  " + tmpA.getKey4());
    }
    } class A {
    A(String key1, String key2, String key3, String key4) {
    this.key1 = key1;
    this.key2 = key2;
    this.key3 = key3;
    this.key4 = key4;
    } public String getKey1() {
    return key1;
    } public String getKey2() {
    return key2;
    } public String getKey3() {
    return key3;
    } public void setKey3(String key3) {
    this.key3 = key3;
    } public String getKey4() {
    return key4;
    } public void setKey4(String key4) {
    this.key4 = key4;
    } public void setKey1(String key1) {
    this.key1 = key1;
    } public void setKey2(String key2) {
    this.key2 = key2;
    } String key1;
    String key2;
    String key3;
    String key4; } class ComparatorA implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
    A oo1 = (A) o1;
    A oo2 = (A) o2;
    int flag1 = oo1.getKey1().compareTo(oo2.getKey1());
    if (flag1 == 0) {
    int flag2 = oo1.getKey2().compareTo(oo2.getKey2());
    if (flag2 == 0) {
    int flag3 = oo1.getKey3().compareTo(oo2.getKey3());
    if (flag3 == 0) {
    int flag4 = oo1.getKey4().compareTo(oo2.getKey4());
    return flag4;
    }
    return flag3;
    }
    return flag2;
    }
    return flag1;
    }
    }}运行结果:100  200 200  100
    200  100 700  500
    200  200 300  600
    300  800 200  200
    300  800 200  500
    300  800 400  500
    600  600 100  400