java.util.Collection学习 - 认识Collection接口
博客专区 > crowley 的博客 > 博客详情
java.util.Collection学习 - 认识Collection接口
crowley 发表于2年前
java.util.Collection学习 - 认识Collection接口
  • 发表于 2年前
  • 阅读 22
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: Collection接口,是java.util包下的一个非常重要的接口。这篇文章浅谈下我对它的认识。 转载注意标明出处http://my.oschina.net/crowley/

Collection是java集合的超类(List,Set等)

注:以下是依据JDK 7 Collection接口来说明的。

public interface  Collection<E>  extends  Iterable <E>

有如下方法:

int size();
返回集合中元素的总数,如果集合的总数比Integer.MAX_VALUE大,则返回Integer.MAX_VALUE

boolean isEmpty();
集合中没有元素,则返回true

boolean contains(Object o);
集合中包含指定元素,则返回true, 具体的说,如果collection中就一个元素e, 就是比较o.equals(e)

我们可以重写o.equals()方法来定制化 o 和 e 是否相等。

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * contains 方法例子
 * @author wangsy
 */
public class Collection1 {
	public static void main(String[] args) {
		test1();//return true
		test2();//return true
		test3();//return false
		test4();//return false
		test5();//return true
		test6();//return false
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		System.out.println(collection.contains(2));
	}
	
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(null);
		collection.add(2);
		
		System.out.println(collection.contains(null));
	}
	
	static void test3() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		System.out.println(collection.contains(null));
	}
	
	static void test4() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "aa"));
		collection.add(new Apple("green", "bb"));
		
		Apple apple = new Apple("red", "aa");
		System.out.println(collection.contains(apple));
	}
	static void test5() {
		Collection<Apple> collection = new ArrayList<Apple>();
		Apple apple1 = new Apple("red", "aa");
		Apple apple2 = new Apple("green", "bb"); 
		collection.add(apple1);
		collection.add(apple2);
		
		System.out.println(collection.contains(apple1));
	}
	
	static void test6() {
		Collection<Apple> collection = new ArrayList<Apple>();
		Apple apple1 = new Apple("red", "aa");
		Apple apple2 = new Apple("green", "bb"); 
		collection.add(apple1);
		collection.add(apple2);
		
		System.out.println(collection.contains(null));
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





Iterator<E> iterator();
返回当前集合的迭代器对象

Object[] toArray();

返回集合所有元素

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * toArray 方法例子
 * @author wangsy
 */
public class Collection2 {
	public static void main(String[] args) {
		test1();
		test2();
		//test3();//java.lang.ArrayStoreException
		//test4();//java.lang.NullPointerException
		test5();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
			System.out.println(Integer.toString((int)obj));
		}
	}
	
	static void test2() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		for(Object obj : collection.toArray()) {
			Apple _obj = (Apple)obj;
			System.out.println(_obj.toString());
		}
	}
	
	static void test3() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		for(String obj : collection.toArray(new String[0])) {
			System.out.println(obj.toString());
		}
	}
	
	static void test4() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		Apple[] apples = null;
		for(Apple obj : collection.toArray(apples)) {
			System.out.println(obj.toString());
		}
	}
	
	static void test5() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		
		Apple[] apples = new Apple[0];
		for(Apple obj : collection.toArray(apples)) {
			System.out.println(obj.toString());
		}
	}
	
	
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





<T> T[] toArray(T[] a);
按照T返回对应泛型的所有元素

boolean add(E e);
新增新元素e

boolean remove(Object o);

移除一个和e.equals(o)的元素 e

package com.conlection;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 
 * remove 方法例子
 * @author wangsy
 */
public class Collection3 {
	public static void main(String[] args) {
		//test1();
		//test2();
		test3();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		collection.remove(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
		}
	}
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		collection.add(2);
		collection.add(3);
		
		collection.remove(2);
		
		for(Object obj : collection.toArray()) {
			System.out.println(obj);
		}
	}
	
	static void test3() {
		Collection<Apple> collection = new ArrayList<Apple>();
		collection.add(new Apple("red", "name1"));
		collection.add(new Apple("red", "name2"));
		collection.add(new Apple("red", "name3"));
		collection.add(new Apple("red", "name3"));
		collection.add(new Apple("red", "name3"));
		
		collection.remove(new Apple("red", "name3"));
		
		for(Apple apple : collection.toArray(new Apple[0])) {
			System.out.println(apple.toString());
		}
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public int hashCode() {
			return name.hashCode()*3 + color.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(this == obj) {
				return true;
			}
			if(obj == null) {
				return false;
			}
			
			if(this.getClass() != obj.getClass()) {
				return false;
			}
			Apple _obj = (Apple)obj;
			return name.equals(_obj.getName()) && color.equals(_obj.getColor());
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}





boolean containsAll(Collection<?> c);
如果c是当前collection的子集,返回true
package com.conlection;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 
 * containsAll 方法例子
 * @author wangsy
 */
public class Collection4 {
	public static void main(String[] args) {
//		test1();
//		test2();
//		test3();
		test4();
	}
	
	static void test1() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(1);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test2() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(4);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test3() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(2);
		collection1.add(3);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static void test4() {
		Collection<Integer> collection = new ArrayList<Integer>();
		collection.add(1);
		collection.add(2);
		collection.add(3);
		
		Collection<Integer> collection1 = new ArrayList<Integer>();
		collection1.add(2);
		collection1.add(3);
		collection1.add(4);
		
		System.out.println(collection.containsAll(collection1));
	}
	
	static class Apple {
		String color;
		String name;
		
		public Apple() {};
		
		public Apple(String color, String name) {
			this.color = color;
			this.name = name;
		}

		String getColor() {
			return color;
		}

		void setColor(String color) {
			this.color = color;
		}

		String getName() {
			return name;
		}

		void setName(String name) {
			this.name = name;
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Apple) {
				Apple _obj = (Apple)obj;
				if(_obj == this) {
					return true;
				} else {
					if(name.equals(_obj.getName()) && color.equals(_obj.getColor())) {
						return true;
					} else 
						return false;
				}
			} else {
				return false;
			}
		}
		
		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("color:").append(color).append(" name:").append(name);
			return sb.toString();
		}
	}
}




boolean addAll(Collection<? extends E> c);
把指定集合的所有元素add到当前集合中
The behavior of this operation is undefined if  the specified collection is modified while the operation is in progress.(This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.) 没有验证过。未完待续

boolean removeAll(Collection<?> c);
在当前集合中删除子集c

boolean retainAll(Collection<?> c);
当前集合保留子集c,也就是删除除了子集c的其他元素

void clear();
清空所有元素,此操作后,集合清空

boolean equals(Object o);

int hashCode();

下一篇博文:java.util.Collection学习 - 浅谈ArrayList

共有 人打赏支持
粉丝 0
博文 3
码字总数 1851
×
crowley
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: