public boolean removeAll(Collection<?> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。此调用返回后,collection 中将不包含任何与指定 collection 相同的元素。
import java.util.*;
import java.io.Serializable;
public class TestRemoveAll{
public static void main(String[] args){
//测试removeAll方法
/*
//removeAll在数据量很大的情况下是不适合用的
List<Double> testList = new ArrayList<Double>();
for(Double i=0.0;i<655355;i++){
testList.add(i);
}
List<Double> test = new ArrayList<Double>();
for(Double i=0.0;i<6555;i++){
test.add(i);
}
Long l = System.currentTimeMillis();
testList.removeAll(test);
System.out.println(System.currentTimeMillis()-l);
*/
List<Student> lists = new ArrayList<Student>();
lists.add(new Student("1","Lucy",20));
lists.add(new Student("2","Mary",23));
lists.add(new Student("3","Neck",22));
List list1 = new ArrayList();
list1.add(new Student("2","Mary",23));
list1.add(new Student("3","Neck",22));
lists.removeAll(list1);
for(Student list: lists){
System.out.println(list.getId()+" "+list.getName()+" "+list.getAge());
}
}
}
class Student implements Serializable{
private String id;
private String name;
private Integer age;
public Student(){}
public Student(String id,String name ,Integer age){
this.id = id;
this.name = name;
this.age = age;
}
public void setId(String id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setAge(Integer age){
this.age = age;
}
public String getId(){
return id;
}
public String getName(){
return name;
}
public Integer getAge(){
return age;
}
//覆盖equals方法 注意参数为:Object类型
public boolean equals(Object obj){
Student stu = (Student)obj;
boolean flag = true;
if(!stu.getId().equals(id)){
flag = false;
}else if(!stu.getName().equals(name)){
flag = false;
}else if(stu.getAge()!=age){
flag = false;
}
return flag;
}
/*public int hashCode(){
return 0;
}*/
}
问题1:覆盖了equals方法是否一定覆盖hashCode方法
http://www.exam8.com/computer/djks/dj2/Java/ziliao/200810/1314425.html
问题2:removeAll方法在数据量很大的情况下,是不适合用的,该用什么方法来解决呢
另外还有retainAll
public boolean retainAll(Collection<?> c)
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。
此实现在此 collection 上进行迭代,依次检查该迭代器返回的每个元素,以查看其是否包含在指定的 collection 中。如果不是,则使用迭代器的 remove 方法将其从此 collection 中移除。
分享到:
相关推荐
Collection接口概念 Java集合框架是Java编程中的一个非常重要的部分,提供了一组用于处理数据集合的接口和类。其中Collection接口是Java...4、removeAll(Collection c):移除集合c中的所有元素; 5、retainAll(Collect
目录前言数组与集合区别集合描述集合格式方法ArrayList实例1.add(E e)2.add(int index, E element)3.addAll(Collection c)4.clear() 与 isEmpty()5.contains(Object o)6.indexOf(Object o)7.iterator()8.remove(int ...
提示:利用addAll()、removeAll()、retainAll()方法。 请查阅帮助文档,了解Collection接口中这几个方法的用法。 【输入形式】 第1行是第1个HashSet对象的元素,0 表示结束 第2行是第2个HashSet对象的元素,0 表示...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Remove Setting Method 去除设置方法 Hide Method 隐藏方法 Replace Constructor with Factory Method 用工厂方法代替构造器 Encapsulate Downcast 封装向下转型 Replace Error Code with ...
Static sprite batcher - supports all sprite types (clipped, tiled, sliced, normal) and text meshes too. Multiple materials & sprite collections supported. Animated sprite deprecated in favour of tk2...
Mingo 是 MongoDB 查询语言的 JavaScript 实现。Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。...var result = query.remove(collection); 标签:Mingo
连通子图个数伊萨卡有向图 为有向图提供框架。 特征 简单有向图(无边权重) 加权有向图(具有整数边权重) 双链有向图(有效访问反向有向图和传入边) 基于地图的有向图实现 ...removeAll(Collection verti
Nature Shaders is a collection of shaders for your vegetation that brings you player interaction, wind simulations, better quality shading, and more. You can easily convert and import models from any...
Practices templates for MS Word of Graphic Node Pack provides a collection of over 3,000 easy to use templates for creating business and personal documents with MS Word. Templates for MS Word by GN ...
just using annotations on your class, creating Collection of that class and creating Adapter that parametrize that class, and its all. Features No adapter code required Click and Long Click events ...
this.RemoveSession(i); } //on d閏lenche l'関鑞ement Stopped if(this.Stopped!=null) { Stopped(this,EventArgs.Empty); } } } catch{} } /// /// ...
JSON_REMOVE JSON_SET JSON_UNQUOTE The Three JSON_MERGE Functions JSON_MERGE JSON_MERGE_PRESERVE JSON_DEPTH JSON_LENGTH JSON_TYPE JSON_VALID JSON_STORAGE_SIZE JSON_STORAGE_FREE 6 JSON and Non-JSON ...
整个层次结构中的所有父类和子类属性都映射到同一个表中,他们的实例通过一个辨别符列(discriminator)来区分。 Plane 是父类。@DiscriminatorColumn 注解定义了辨别符列。对于继承层次结构中的每个类, @...
ICS - Internet Component Suite - V8 - Delphi 7 to RAD Studio 10 Seattle =============================================...the component package and click Remove). Rename the old ICS directory and unzip to...