集合Conllection

一、Collection

java.util.Collection
* -集合用于存放一组元素,提供了维护集合的相关方法
* -其派生了两个接口
* List:可重复
* Set:不可重复
* -元素是否重复依靠元素自身equals方法比较的结果

Point类(测试类):

package daay01;

public class Point {
public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
private int x;
private int y;

public int getX() {
	return x;
}
public void setX(int x) {
	this.x = x;
}
public int getY() {
	return y;
}
public void setY(int y) {
	this.y = y;
}
@Override
public String toString() {
	return " [" + x + ", " + y + "]";
}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + x;
	result = prime * result + y;
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Point other = (Point) obj;
	if (x != other.x)
		return false;
	if (y != other.y)
		return false;
	return true;
}


}

  1.常用方法操作

package daay01;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collection;

/**
 * java.util.Collection
 * -集合用于存放一组元素,提供了维护集合的相关方法
 * -其派生了两个接口
 *    List:可重复
 *    Set:不可重复
 * -元素是否重复依靠元素自身equals方法比较的结果
 * 
 * @author ajia
 *
 */
public class CollectionDemo01 {
public static void main(String[] args) {
	
	Collection c=new ArrayList();
	/*
	 * nooleam add (E e)
	 *   -向指定的结合中添加元素
	 *   -添加成功返回true
	 */
	c.add("one");
	c.add("two");
	c.add("three");
	System.out.println(c);
	
	
	/*
	 * int size()
	 * -返回当前集合中的元素个数
	 */
	System.out.println(c.size());
	
	/*
	 * boolean   isEmpty()
	 *   判断一个集合是否为空集
	 */
	boolean isEmpty=c.isEmpty();
	System.out.println("是否为空:"+isEmpty);
	
	
	/*
	 * void clear()
	 *  -清空集合
	 */
	c.clear();
	System.out.println("是否为空:"+c.isEmpty());
		
	
}
}

  

package daay01;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collection;

/**
 * java.util.Collection
 * -集合用于存放一组元素,提供了维护集合的相关方法
 * -其派生了两个接口
 *    List:可重复
 *    Set:不可重复
 * -元素是否重复依靠元素自身equals方法比较的结果
 * 
 * @author ajia
 *
 */
public class CollectionDemo01 {
public static void main(String[] args) {
	
	Collection c=new ArrayList();
	/*
	 * nooleam add (E e)
	 *   -向指定的结合中添加元素
	 *   -添加成功返回true
	 */
	c.add("one");
	c.add("two");
	c.add("three");
	System.out.println(c);
	
	
	/*
	 * int size()
	 * -返回当前集合中的元素个数
	 */
	System.out.println(c.size());
	
	/*
	 * boolean   isEmpty()
	 *   判断一个集合是否为空集
	 */
	boolean isEmpty=c.isEmpty();
	System.out.println("是否为空:"+isEmpty);
	
	
	/*
	 * void clear()
	 *  -清空集合
	 */
	c.clear();
	System.out.println("是否为空:"+c.isEmpty());

}
}

  

package daay01;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 删除集合中的元素
 * @author ajia
 *
 */
public class CollectionDemo04 {
public static void main(String[] args) {
	Collection c=new ArrayList();
	c.add(new Point(1,5));
	c.add(new Point(1,5));
	c.add(new Point(3,72));
	c.add(new Point(6,82));
	
	System.out.println(c);
	//[ [1, 5],  [1, 5],  [3, 72],  [6, 82]]
	
	Point p=new Point(1,5);
	System.out.println(p);
	/*
	 * boolean remove(E e)
	 *  -从集合中删除指定元素,删除成功返回true
	 *   -只删除集合中第一个与给定equals比较为true的元素
	 */
	c.remove(p);
	System.out.println(c);
	//[ [1, 5],  [3, 72],  [6, 82]]
	
}
}

  

package daay01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

/**
 * 集合的批量操作
 * @author ajia
 *
 */
public class CollectionDemo05 {
public static void main(String[] args) {
	Collection c1=new ArrayList();
	c1.add("java");
	c1.add("c++");
	c1.add(".net");
	c1.add("ios");
	
	
	Collection c2=new HashSet();
	//不可添加重复元素,并且以自己的方式顺序放入
	c2.add("ios");
	c2.add("android");
	c2.add("windowMobie");
	System.out.println(c2.add("ios"));
	System.out.println(c2);//[android, ios, windowMobie]
	
	/*
	 * 取并集
	 * boolean addAll(Collection c)
	 *  -将给定集合中所有的元素添加到当前集合中
	 *  只要当前集合中的元素个数发生变化就返回true
	 *  
	 *  
	 */
	
	//谁调用addAll并集就是谁的特点
	c1.addAll(c2);
	System.out.println(c1);
	//[java, c++, .net, ios, android, ios, windowMobie]
	c2.addAll(c1);
	System.out.println(c2);
	//[java, c++, .net, ios, android, ios, windowMobie]
	
	
	Collection c3=new ArrayList();
	c3.add("ios");
	c3.add("c++");
	c3.add(".net");
	/*
	 * boolean containsAll(Coleaction c)
	 * - 判断当前集合中是否包含给定集合中的所有元素
	 */
	boolean isContainsAll=c1.containsAll(c3);
	System.out.println("c1中是否包含c3中的所有元素:"+isContainsAll);
	//true
	
	/*
	 * 删除两个集合中共有的元素
	 */
	c1.removeAll(c3);
	System.out.println(c1);
	//[java, android, windowMobie]

	
}
}

2.迭代器  

package daay01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 集合的遍历
 * -Collection提供了一个遍历集合的方法:迭代器模式
 * 
 * Iterator iterator()
 * 
 * -java.util.Interator是一个接口,规定了用于遍历集合的方法,
 * 对于不同的集合提供了不同的实现类,不需要记住具体的实现类,把他们当做Interator
 * 
 * 便立即和遵循:问 取 删 步骤
 * @author ajia
 *
 */
public class CollectionDemo06 {
public static void main(String[] args) {
	Collection <String>c=new ArrayList<String>();
	c.add("one");
	c.add("#");
	c.add("two");
	c.add("#");
	c.add("three");
	c.add("#");
	c.add("four");
	
	//获取当前集合的迭代器
	Iterator it=c.iterator();
	/*
	 *boolean   hasNext() ---问   判断集合是否含有元素 
	 *  
	 */
	while(it.hasNext()){                   //问
		Object obj=it.next();              //取
		String str=(String)obj;
		System.out.print(str+",");
		if("#".equals(str)){
			/*
			 * 在使用迭代器遍历集合的时候,不能使用集合中的添加和删除的方法
			 * c.remove(str);
			 *抛异常:java.util.ConcurrentModificationException
			 */
			//但是可以使用迭代器中的方法增删元素    //删
			it.remove();
		}
	}
	System.out.println(c);

	
}
}

增强for循环foreach

不能代替传统的循环,仅仅用来遍历集合或数组

-新循环并非新的语法,新循环是编译器认可,虚拟机不认可的
* -新循环遍历集合时,编译器会将它改为迭代器的方式遍历,所以在使用
* 新循环遍历时不能使用集合的增加或者删除的方法。只能用来遍历集合或者
* 数组,不能用来增加或者修改

package daay01;

import java.util.ArrayList;
import java.util.Collection;

public class NewForDemo02 {
public static void main(String[] args) {
	Collection c=new ArrayList();
	c.add("#");
	c.add("two");
	c.add("three");
	c.add("four");
	c.add("#");
	c.add("six");
	c.add("#");
	c.add("one");
	/*
	 * -新循环并非新的语法,新循环是编译器认可,虚拟机不认可的
	 * -新循环遍历集合时,编译器会将它改为迭代器的方式遍历,所以在使用
	 * 新循环遍历时不能使用集合的增加或者删除的方法。只能用来遍历集合或者
	 * 数组,不能用来增加或者修改
	 */
	for(Object e:c){
		String str=(String)e;
		System.out.println(str);
	}
	
}
}

关于List集合比较器的重写:

大致步骤:1.实现Comparator接口

                 2.重写该接口中 的compare方法,写入自定义的排序方式

package day04;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
     public static void main(String[] args) {
    	 //创建排序的集合
    	 List <Point> list=new ArrayList<Point>();
    	     list.add(new Point(20,20));
    	     list.add(new Point(25,24));
    	     list.add(new Point(24,57));
    	     list.add(new Point(35,24));
    	     list.add(new Point(99,74));
    	     //利用工具类Collections里面的sort方法对list集合排序
    	     Collections.sort(list,
    	    		 /*
    	    		  * 因为此比较器只用一次,故采用匿名内部类的形式
    	    		  * 传参,Comparator为接口,否则的话需实现该接口.
    	    		  * 该接口中需重写compare方法,在该方法中自定义比较
    	    		  * 方法.
    	    		  */
    	    		 new Comparator<Point>() {
    	    		public int compare(Point o1, Point o2) {
    	    			if(o1==null){
    	    		           return -1;
    	    		       }
    	    		  if(o2==null){
    	    		           return -1;
    	    		        }
    	    		        int len=o1.x*o1.x+o1.y*o1.y;
    	    		        int olen=o2.x*o2.x+o2.y*o2.y;
    	    		        if(len>olen) {
    	    		        	return -1;
    	    		        }else {
    	    		        	return 1;
    	    		        }
    	    		}	
    	     }
    	    		 );
    	     System.out.println(list);
    	 //运行结果[(99,74), (24,57), (35,24), (25,24), (20,20)]
        }
}
/**
 * 用在于List集合中存放的元素
 * @param x
 * @param y
 */
class Point{
	 public Point(int x,int y) {
		 this.x=x;
		 this.y=y;
	 }
	int x;
	int y;
	public String toString() {
		return "("+this.x+","+this.y+")";
	}
}

  

原文地址:https://www.cnblogs.com/chenzhiwei/p/9522704.html