容器API_Collection_1

时间:2019-08-15 12:14:24   收藏:0   阅读:110

j2sdk所提供的容器API位于java.util包内。

容器API的类图结构如下图所示:

Collection接口一定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。

  Set中的数据对象没有顺序且不可以重复。(通过equals来判断)

  List中的数据对象有顺序且可以重复。

Map接口定义了存储“键(key)——值(value)映射对”的方法。

技术分享图片

Collection接口中所定义的方法:

int size();//大小

boolean isEmpty();

void clear();//清除

boolean contains(Object element);

boolean add (Object element);

boolean remove(Object element);

Iterator iterator();

boolean containsAll(Collection c);

boolean addAll(Collection c);

boolean removeAll(Collection c);

boolean retainAll(Collection c);接口的交集

Object [] toArray();

 

 

Collection 方法举例

import java.util.*;
public class Test {
	public static void main(String[] args) {
		Collection c = new HashSet();
		c.add("hello");
		c.add(new Name("f1","11"));
		c.add(new Integer(100));
		c.remove("hello");
		c.remove(new Integer(100));
		System.out.println(c.remove(new Name("f1","11")));
		System.out.println(c);
	}
}
class Name {
	private String firstName,secondName;
	public Name(String firstName,String secondName) {
		this.firstName = firstName;
		this.secondName = secondName;
	}
	public String getfirstName() {return firstName;}
	public String getsecondName() {return secondName;}
	public String toString() {
		return firstName+" "+secondName;
	}
}

 

原文:https://www.cnblogs.com/lsswudi/p/11357169.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!