`
llmy
  • 浏览: 119804 次
  • 性别: Icon_minigender_2
  • 来自: 济南
社区版块
存档分类
最新评论

Collection中的removeAll方法

    博客分类:
  • J2EE
 
阅读更多

   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 中移除。

分享到:
评论

相关推荐

    Java集合框架Collection接口.pdf

    Collection接口概念 Java集合框架是Java编程中的一个非常重要的部分,提供了一组用于处理数据集合的接口和类。其中Collection接口是Java...4、removeAll(Collection c):移除集合c中的所有元素; 5、retainAll(Collect

    java基础——集合ArrayList ,LinkedList

    目录前言数组与集合区别集合描述集合格式方法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 ...

    2d toolkit 2.0 hotfix 2

    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...

    MongoDB查询的JavaScript实现Mingo.zip

    Mingo 是 MongoDB 查询语言的 JavaScript 实现。Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。...var result = query.remove(collection); 标签:Mingo

    连通子图个数leetcode-ithaka-digraph:有向图和杉山布局

    连通子图个数伊萨卡有向图 为有向图提供框架。 特征 简单有向图(无边权重) 加权有向图(具有整数边权重) 双链有向图(有效访问反向有向图和传入边) 基于地图的有向图实现 ...removeAll(Collection verti

    Nature Shaders v1.0.4

    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...

    Templates for MS Word by GN 5.0.2 MAS

    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 ...

    Android代码-SuperRecyclerView

    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 ...

    DotNetFtpSources

    this.RemoveSession(i); } //on d閏lenche l'関鑞ement Stopped if(this.Stopped!=null) { Stopped(this,EventArgs.Empty); } } } catch{} } /// /// ...

    MySQL and JSON A Practical Programming Guide 2018

    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 ...

    Hibernate注释大全收藏

    整个层次结构中的所有父类和子类属性都映射到同一个表中,他们的实例通过一个辨别符列(discriminator)来区分。 Plane 是父类。@DiscriminatorColumn 注解定义了辨别符列。对于继承层次结构中的每个类, @...

    ICS delphixe10源码版

    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...

Global site tag (gtag.js) - Google Analytics