Java之容器

1. 容器的概念:

class Name {
	private String firstName, lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName+"  "+lastName;
	}
}
public class Test {
	public static void main(String[] args) {
		Name n1 = new Name("f1","l1");
		Name n2 = new Name("f2","l2");
		Name n3 = new Name("f3","l3");
	}
}

  容器:Java API 所提供的一系列类的实例,用于在程序中存放对象.

     数组一旦定义大小,无法改变.

2. 容器 API:

  2.1 J2SDK 所提供的容器 API 位于 java.util 包内.

  2.2 容器 API 的类图结构如下:

              

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

     2.3.1 Set 中的数据对象没有顺序,且不可以重复;

              2.3.2 List 中的数据对象有顺序,且可以重复.

          2.4 Map 接口定义了存储“键(Key)-值(Value)映射对” 的方法.

          2.5 Collection 接口中所定义的方法:

      int size():返回此 collection 中的元素数;

        boolean isEmpty():如果此 collection 不包含元素,则返回 true;

      boolean contains(Object o):如果此 collection 包含指定的元素,则返回 true;

      Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器;

                 Object[] toArray():返回包含此 collection 中所有元素的数组;

       <T> T[] toArray(T[] a):返回包含此 collection 中所有元素的数组;

      boolean add(E e):确保此 collection 包含指定的元素(可选操作)。如果此 collection 由于调用而发生更改,则返回 true;

      boolean remove(Object o):从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作),采用 == 判断是否存在;

      boolean containsAll(Collection<?> c):如果此 collection 包含指定 collection 中的所有元素,则返回 true;

      boolean addAll(Collection<? extends E> c):将指定 collection 中的所有元素都添加到此 collection 中(可选操作);

      boolean removeAll(Collection<?> c):移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作);

      boolean retainAll(Collection<?> c):仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作);// 求集合交集

      void clear():移除此 collection 中的所有元素(可选操作);

      boolean equals(Object o):比较此 collection 与指定对象是否相等;

      int hashCode():返回此 collection 的哈希码值

3. Collection 方法举例

Demo_1:

import java.util.*;
class Name {
	private String firstName, lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName+"  "+lastName;
	}
}
public class Test {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add("hello");
		c.add(new Name("f1","l1"));
		c.add(new Integer(100));
		c.add(15);
		c.add(15.268);
		System.out.println(c.size());
		System.out.println(c);
	}
}

 运行结果:

5
[hello, f1  l1, 100, 15, 15.268]

Demo_2:

import java.util.*;
class Name {
	private String firstName, lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName+"  "+lastName;
	}
}
public class Test {
	public static void main(String[] args) {
		Collection c = new HashSet();
		c.add("hello");
		c.add(new Name("f2","l2"));
		c.add(120.235);
		c.add(new Integer(123));
		System.out.println(c); // 输出:[120.235, f2  l2, hello, 123]
		c.remove("hello");
		c.remove(123);
		c.remove(120.235);
		c.add(new Name("f2","l2"));
		System.out.println(c); // 输出:[f2  l2, f2  l2]
		System.out.println(c.remove(new Name("f2","l2"))); // false
          // Name 方法并没有重写equals方法没有写,则 ==,即是否指向同一个对象; System.out.println(c); // 输出:[f2 l2, f2 l2] } }

 Demo_3

import java.util.*;
class Name {
	private String firstName, lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName+"  "+lastName;
	}
}
public class Test {
	public static void main(String[] args) {
		Collection c = new HashSet();
		Name s = new Name("f2","l2");
		c.add(s);
		c.add(s);
		System.out.println(s); // 输出:f2  l2
		c.remove(s);
		System.out.println(c); // 输出:[]
	}
}

 4. 容器类中对象相等问题

    容器类对象在调用 remove、contains 等方法时需要比较两个对象是否相等,这就会涉及到对象类型的 equals 方法和 hashcode 方法;对于自定义的类型,需要    重写 equals 方法和 hashcode 方法以实现自定义的对象相等规则.

【注】相等的对象应该具有相等的 hashcode. 特别是当这个对象当作键(索引)的时候.

 Demo_4

import java.util.*;
class Name {
	private String firstName, lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName+"  "+lastName;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Name){
			Name name = (Name) obj;
			return firstName.equals(name.firstName) && lastName.equals(name.lastName) ;
		}
		return super.equals(obj);
	}
	@Override
	public int hashCode() {
		return firstName.hashCode();
	}
}
public class Test {
	public static void main(String[] args) {
		Collection c = new LinkedList();
		c.add(new Name("f1","l1"));
		c.add(new Name("f1","l1"));
		c.add(new Name("f2","l2"));
		System.out.println(c); // 输出 [f1  l1, f1  l1, f2  l2]
		System.out.println(c.contains(new Name("f1","l1"))); // 输出 true
		System.out.println(c.contains(new Name("f2","l2"))); // 输出 true
		c.remove(new Name("f1","l1")); 
		System.out.println(c); // 输出 [f1  l1, f2  l2]
		c.remove(new Name("f1","l1"));
		System.out.println(c); // 输出 [f2  l2]
		c.remove(new Name("f2","l2"));
		System.out.println(c.size()); // 输出 0
	}
}
原文地址:https://www.cnblogs.com/bosongokay/p/6763858.html