package com.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class ThreadTest10
{
/**
* @param args
*/
public static void main(String[] args)
{
Collection users = /*new CopyOnWriteArrayList();*/new ArrayList();
users.add(new User("张三",28));
users.add(new User("李四",25));
users.add(new User("王五",31));
Iterator it = users.iterator();
while(it.hasNext()){
User user = (User)it.next();
if("张三".equals(user.getName())){
users.remove(user);
}else{
System.out.println(user);
}
}
}
}
class User implements Cloneable
{
private String name;
private int age;
public User(String name, int age)
{
this.name = name;
this.age = age;
}
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof User))
{
return false;
}
User user = (User) obj;
if (this.name.equals(user.name) && this.age == user.age)
{
return true;
}
return false;
}
public int hashCode(){
return name.hashCode()+age;
}
public String toString(){
return "(name:'"+name+"',age:"+age+")";
}
@Override
protected Object clone()
{
Object object = null;
try{
object = super.clone();
}catch(CloneNotSupportedException e){
}
return object;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
遍历集合增删改所引发的问题
最新推荐文章于 2020-05-08 16:33:11 发布