遍历一下list,判断对象的字段值,满足条件的remove
解决方案 »
- 请教:在jdk的核心库rt.jar中能否扩展新的类?
- 外行提问一个java程序应用的问题
- 如何在ComboBox中列出图片
- java怎么取TAB鍵與TAB鍵之間的字符串?
- 关于运行java的问题
- 100求一JAVA程序,对大家来说应该简单,对我个初学的来说就有点困难了。
- 初学者的问题:http://localhost:8080/index.html成功显示,但http://localhost:8080/index.jsp不能成功显示
- 夜深了,送道题给赔我过夜的人们。
- 为什么Java的Socket I/O通信这么慢啊?怎样进行高性能数据传输?
- JAVA的消息发送
- 大企业的算法面试题,等你来挑战。
- 求助!大神帮忙!java poi 大数据量(3-4万条)导出Excel2003版本,导入Excel2007版本时内存溢出
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;
}
}
遍历原来的list,把符合条件的放到新的a的list中
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;
}
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