容器Collection

一、定义

Collection接口是层次结构中的根接口,是一组允许重复的对象。
Set接口继承Collection,存放无序不重复的数据,在内部的排序机制排序。
List接口继承Collection,存放有序可重复的数据,按照次序放置元素。




二、Collection接口

Collection可根据数据的大小改变容量,且只能存储引用类型的数据(基本数据类型可以使用自动装箱 )。

(一)使用

public static void main(String[] args) {
	Collection<Integer> con=new ArrayList();
	//调用Collection接口中的方法
	//<>中的为泛型,规范容器中所有数据的数据类型
	con.add(1);
	con.add(2);
	System.out.println(con);//[1,2]
}

(二)方法

  1. boolean add(E e) 确保此 collection 包含指定的元素(可选操作)。
  2. boolean addAll(Collection<? extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
Collection<Integer> con1=new ArrayList();
con1.add(3);
con1.addAll(con);
System.out.println(con1);//[3,1,2]
  1. void clear() 移除此 collection 中的所有元素(可选操作)。
con1.clear();
System.out.println(con1);//[]
  1. boolean contains(Object o) 如果此 collection 包含指定的元素,则返回 true。
System.out.println(con.contains(1));//true
  1. boolean containsAll(Collection<?> c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。
Collection<Integer> con1=new ArrayList();
con1.addAll(con);
System.out.println(con1.containsAll(con));//true
  1. boolean equals(Object o) 比较此 collection 与指定对象是否相等。
System.out.println(con.equals(con1));//true
  1. int hashCode() 返回此 collection 的哈希码值。
  2. boolean isEmpty() 如果此 collection 不包含元素,则返回 true。
System.out.println(con.isEmpty(5));//true
  1. Iterator<E> iterator() 返回在此 collection 的元素上进行迭代的迭代器。
  2. boolean remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
con1.clear(3);
System.out.println(con1);//[1,2]
  1. boolean removeAll(Collection<?> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
Collection<Integer> con2=new ArrayList();
Collection<Integer> con3=new ArrayList();
con2.add(1);
con2.add(2);
con3.add(2);
con3.add(3);
System.out.println(con2.removeAll(con3));//[1]
  1. boolean retainAll(Collection<?> c) 仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 (求交集)
Collection<Integer> con2=new ArrayList();
Collection<Integer> con3=new ArrayList();
con2.add(1);
con2.add(2);
con3.add(2);
con3.add(3);
System.out.println(con2.retainAll(con3));//[2]
  1. int size() 返回此 collection 中的元素数。
System.out.println(con3.size());//2
  1. Object[] toArray() 返回包含此 collection 中所有元素的数组。
System.out.println(con3.toArray());//[2,3]数组
  1. <T> T[] toArray(T[] a) 返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

(三)迭代器Iterator

使用
Iterator 迭代器对象=容器对象.iterator();//每一个Collection对象都有自己的迭代器
例:Iterator it=con.iterator();//在使用迭代器时不允许修改添加数据,如有需要可使用ListIterator
方法
  1. boolean hasNext() 如果仍有元素可以迭代,则返回 true。
  2. E next() 返回迭代的下一个元素。
  3. void remove() 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。

(四)遍历

方法一:增强for
for(Object o:con){
	System.out.println(o);
}
方法二:迭代器Iterator
Iterator it=con.iterator();
//循环判断是否存在下一个元素
while(it.hasNext()){
	//如果存在,就获取
	System.out.println(it.next());
}





三、List接口(实现自Collection接口)

(一)使用

public static void main(String[] args) {
	List<Integer> li=new ArrayList();
	//调用List接口中的方法
	//<>中的为泛型,规范容器中所有数据的数据类型
	li.add(1);
	li.add(2);
	li.add(3);
	li.add(4);
	System.out.println(li);//[1,2,3,4]
}

(二)新增方法

List中的方法实现Collection中的所有方法,且新增了一些方法。

  1. void add(int index, E element) 在列表的指定位置插入指定元素(可选操作)。
li.add(2,5);
System.out.println(li);//[1,2,5,3,4]
  1. boolean addAll(int index, Collection<? extends E> c) 将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。
List<Integer> li1=new ArrayList();
li1.add(3);
li1.add(7);
li.add(2,li1);
System.out.println(li);//[1,2,3,7,5,3,4]
  1. E get(int index) 返回列表中指定位置的元素。
System.out.println(li.get(3));//7
  1. int indexOf(Object o) 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
System.out.println(li.indexOf(3));//2
  1. int lastIndexOf(Object o) 返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
System.out.println(li.lastIndexOf(3));//5
  1. ListIterator<E> listIterator() 返回此列表元素的列表迭代器(按适当顺序)。
  2. ListIterator<E> listIterator(int index) 返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。
  3. E remove(int index) 移除列表中指定位置的元素(可选操作)。
System.out.println(li.remove(3));//数据和索引相同情况,优先删除索引。[1,2,3,5,3,4]
  1. E set(int index, E element) 用指定元素替换列表中指定位置的元素(可选操作)。
System.out.println(li.set(3,7));//[1,2,3,7,3,4]
  1. List<E> subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 (左闭右开)
System.out.println(li.subList(1,4));//[2,3,7]

(三)列表迭代器ListIterator

允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置。继承自Iterator

使用
ListIterator 迭代器对象=list容器对象.ListIterator();//每一个Collection对象都有自己的迭代器
例:ListIterator it = li.ListIterator();
新增方法
  1. void add(E e) 将指定的元素插入列表(可选操作)。
  2. boolean hasPrevious() 如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。
  3. int nextIndex() 返回对 next 的后续调用所返回元素的索引。
  4. E previous() 返回列表中的前一个元素。
  5. int previousIndex() 返回对 previous 的后续调用所返回元素的索引。
  6. void set(E e) 用指定元素替换 next 或 previous 返回的最后一个元素(可选操作)。

(四)遍历

方法一:增强for
for(Object o:li){
	System.out.println(o);
}
方法二:普通for
for(int i=0;i<li.size();i++){
	System.out.println(li.get(i));
}
方法三:迭代器Iterator
Iterator it=li.iterator();
//循环判断是否存在下一个元素
while(it.hasNext()){
	//如果存在,就获取
	System.out.println(it.next());
}
方法四:列表迭代器ListIterator
ListIterator it = li.ListIterator();
//循环判断是否存在下一个元素
while(it.hasNext()){
	//如果存在,就获取
	System.out.println(it.next());
}

四、ArrayList类(实现自List接口)

ArrayList底层使用数组实现
优点:根据索引遍历,获取效率高
缺点:添加,删除,需要大量拷贝数据,效率低
扩容机制:根据空构造器创建对象,默认构建空数组,初始容量是10,当第一次添加以后才会扩容,扩容机制每次扩容的是原容量的1.5倍(int newCapacity = oldCapacity + (oldCapacity >> 1)?,
使用了copyOf方法进行动态扩容
推荐使用的情况:大量做查询多推荐使用,做增删多不推荐使用

(一)使用

  1. ArrayList() 构造一个初始容量为 10 的空列表。ArrayList ar0=new ArrayList();
  2. ArrayList(Collection<? extends E> c) 构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。ArrayList ar1=new ArrayList(con);
  3. ArrayList(int initialCapacity) 构造一个具有指定初始容量的空列表。ArrayList ar2=new ArrayList(10);

(二)新增方法

  1. Object clone() 返回此 ArrayList 实例的浅表副本。
ar.add(1);
ar.add(2);
ar.add(3);
System.out.println(ar.clone());[1, 2, 3]
  1. void ensureCapacity(int minCapacity) 如有必要,增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。
  2. protected void removeRange(int fromIndex, int toIndex) 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
  3. void trimToSize() 将此 ArrayList 实例的容量调整为列表的当前大小。

(三)存储对象类型数据

public class Person {
	private String name;
	private int age;
	public Person() {
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age +"]";
	}
ArrayList<Person> persons=new ArrayList();
persons.add(new Person("张三",20));
persons.add(new Person("李四",21));
persons.add(new Person("王五",22));
System.out.println(persons.indexOf(new Person("李四",21)));//默认使用Object中的equals方法比较的是地址,会返回-1,所以需要在person类中重写equals方法。
public class Person {
	...
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return false;
	}
}

五、Vector类(实现自List接口)

Vector类与ArrayList类基本相同,但是有以下不同:

  1. Vector线程安全,ArrayList线程不安全。
  2. Vector每次扩容的是原容量的2倍。

(一)使用

  1. Vector() 构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。 Vector ve0=new Vector();
  2. Vector(Collection<? extends E> c) 构造一个包含指定 collection 中的元素的向量,这些元素按其 collection 的迭代器返回元素的顺序排列。 Vector ve1=new Vector (con);
  3. Vector(int initialCapacity) 使用指定的初始容量和等于零的容量增量构造一个空向量。 Vector ve2=new Vector (20);
  4. Vector(int initialCapacity, int capacityIncrement) 使用指定的初始容量和容量增量构造一个空的向量。

(二)新增方法

  1. void addElement(E obj) 将指定的组件添加到此向量的末尾,将其大小增加 1。
  2. int capacity() 返回此向量的当前容量。
  3. Object clone() 返回向量的一个副本。
  4. void copyInto(Object[] anArray) 将此向量的组件复制到指定的数组中。
  5. E elementAt(int index) 返回指定索引处的组件。
  6. Enumeration<E> elements() 返回此向量的组件的枚举。
  7. void ensureCapacity(int minCapacity) 增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。
  8. E firstElement() 返回此向量的第一个组件(位于索引 0) 处的项)。
  9. void insertElementAt(E obj, int index) 将指定对象作为此向量中的组件插入到指定的 index 处。
  10. E lastElement() 返回此向量的最后一个组件。
  11. int lastIndexOf(Object o, int index) 返回此向量中最后一次出现的指定元素的索引,从 index 处逆向搜索,如果未找到该元素,则返回 -1。
  12. void removeAllElements() 从此向量中移除全部组件,并将其大小设置为零。
  13. boolean removeElement(Object obj) 从此向量中移除变量的第一个(索引最小的)匹配项。
  14. boolean removeElement(Object obj) 从此向量中移除变量的第一个(索引最小的)匹配项。
  15. void removeElementAt(int index) 删除指定索引处的组件。
  16. protected void removeRange(int fromIndex, int toIndex) 从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。
  17. void setElementAt(E obj, int index) 将此向量指定 index 处的组件设置为指定的对象。
  18. void setSize(int newSize) 设置此向量的大小。
  19. String toString() 返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。
  20. void trimToSize() 对此向量的容量进行微调,使其等于向量的当前大小。

六、LinkedList类(实现自List接口)

底层实现:双向链表
优点:做增删效率高
缺点:根据索引获取或遍历效率低(本身链表结构不存在索引,模拟索引使用)
使用:大量做增删推荐使用LinkedList,大量做查询推荐使用ArrayList

(一)使用

  1. LinkedList() 构造一个空列表。 LinkedList lin=new LinkedList ();
  2. LinkedList(Collection<? extends E> c) 构造一个包含指定 collection 中的元素的列表,这些元素按其 collection 的迭代器返回的顺序排列。 LinkedList lin1=new LinkedList (con);
public static void main(String[] args) {
	LinkedList lin=new LinkedList ();
	lin.add(1);
	lin.add(2);
	lin.add(3);
	lin.add(4);
	lin.add(5);
	System.out.println(lin);//[1, 2, 3, 4, 5]
}

(二)新增方法

  1. void addFirst(E e) 将指定元素插入此列表的开头。
lin.addFirst(9);
System.out.println(lin);//[9, 1, 2, 3, 4, 5]
  1. void addLast(E e) 将指定元素添加到此列表的结尾。
lin.addLast(2);
System.out.println(lin);//[9, 1, 2, 3, 4, 5, 2]
  1. Object clone() 返回此 LinkedList 的浅表副本。
  2. Iterator descendingIterator() 返回以逆向顺序在此双端队列的元素上进行迭代的迭代器。
  3. E element() 获取但不移除此列表的头(第一个元素)。
System.out.println(lin.element());//9
  1. E getFirst() 返回此列表的第一个元素。
System.out.println(lin.getFirst());//9
  1. E getLast() 返回此列表的最后一个元素。
System.out.println(lin.getLast());//2
  1. boolean offer(E e) 将指定元素添加到此列表的末尾(最后一个元素)。
System.out.println(lin.offer(11));//true
System.out.println(lin);//[9, 1, 2, 3, 4, 5, 2, 11]
  1. boolean offerFirst(E e) 在此列表的开头插入指定的元素。
System.out.println(lin.offerFirst(12));//true
System.out.println(lin);//[12, 9, 1, 2, 3, 4, 5, 2, 11]
  1. boolean offerLast(E e) 在此列表末尾插入指定的元素。
System.out.println(lin.offerLast(13));//true
System.out.println(lin);//[12, 9, 1, 2, 3, 4, 5, 2, 11, 13]
  1. E peek() 获取但不移除此列表的头(第一个元素)。
System.out.println(lin.peek());//12
  1. E peekFirst() 获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
System.out.println(lin.peekFirst());//12
  1. E peekLast() 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
System.out.println(lin.peekLast());//13
  1. E poll() 获取并移除此列表的头(第一个元素)
System.out.println(lin.poll());//12
System.out.println(lin);//[9, 1, 2, 3, 4, 5, 2, 11, 13]
  1. E pollFirst() 获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
System.out.println(lin.pollFirst());//9
System.out.println(lin);//[1, 2, 3, 4, 5, 2, 11, 13]
  1. E pollLast() 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
System.out.println(lin.pollLast());//13
System.out.println(lin);//[1, 2, 3, 4, 5, 2, 11]
  1. E pop() 从此列表所表示的堆栈处弹出一个元素。
System.out.println(lin.pop());//1(按顺序弹出)
System.out.println(lin);//[2, 3, 4, 5, 2, 11]
  1. void push(E e) 将元素推入此列表所表示的堆栈。
lin.push(5);//从头推入
System.out.println(lin);//[5, 2, 3, 4, 5, 2, 11]
  1. E remove() 获取并移除此列表的头(第一个元素)。
System.out.println(lin.remove());//5
System.out.println(lin);//[2, 3, 4, 5, 2, 11]
  1. E removeFirst() 移除并返回此列表的第一个元素。
System.out.println(lin.removeFirst());//2
System.out.println(lin);//[3, 4, 5, 2, 11]
  1. boolean removeFirstOccurrence(Object o) 从此列表中移除第一次出现的指定元素(从头部到尾部遍历列表时)。
lin.add(5);
lin.removeFirstOccurrence(5);
System.out.println(lin);//[3, 4, 2, 11, 5]
  1. E removeLast() 移除并返回此列表的最后一个元素。
System.out.println(lin.removeLast());//5
System.out.println(lin);//[3, 4, 2, 11]
  1. boolean removeLastOccurrence(Object o) 从此列表中移除最后一次出现的指定元素(从头部到尾部遍历列表时)。
lin.add(3);
lin.removeLastOccurrence(3);
System.out.println(lin);//[3, 4, 2, 11]

(三)存储对象类型数据

LinkedList<Person> persons=new LinkedList();
persons.add(new Person("张三",20));
persons.add(new Person("李四",21));
persons.add(new Person("王五",22));
System.out.println(persons.indexOf(new Person("李四",21)));//默认使用Object中的equals方法比较的是地址,会返回-1,所以需要在person类中重写equals方法。
public class Person {
	...
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return false;
	}
}

七、Set接口(实现自Collection接口)

(一)使用

Set<String> set=new HashSet();
		set.add("qqq");
		set.add("www");
		set.add("eee");
		set.add("rrr");
		System.out.println(set);//[qqq, rrr, eee, www]  set的添加顺序和内部真实存储的顺序不一定相同
		System.out.println(set);//[qqq, rrr, eee, www]  一旦存储不会改变

(二)方法

无新增方法,方法与Collection接口的方法一致。

(三)迭代器Iterator

与Collection接口的迭代器一致。

(四)遍历

与Collection接口的遍历方法一致。

八、TreeSet类(实现自Set接口)

TreeSet: 特点是会自动去重
底层由红黑树构成,由TreeMap维护
特点: 有序的,默认升序排序 ,查询等效率高

(一)使用

  1. TreeSet() 构造一个新的空 set,该 set 根据其元素的自然顺序进行排序。 TreeSet tre=new TreeSet();
  2. TreeSet(Collection<? extends E> c) 构造一个包含指定 collection 元素的新 TreeSet,它按照其元素的自然顺序进行排序。 TreeSet tre=new TreeSet(con);
  3. TreeSet(Comparator<? super E> comparator) 构造一个新的空 TreeSet,它根据指定比较器进行排序。
  4. TreeSet(SortedSet<E> s) 构造一个与指定有序 set 具有相同映射关系和相同排序的新 TreeSet。
TreeSet<Double> tre=new TreeSet();
tre.add(1.11);
tre.add(5.55);
tre.add(3.34);
tre.add(6.66);
System.out.println(tre);//[1.11, 3.34, 5.55, 6.66]  默认升序排序

(二)新增方法

  1. E ceiling(E e) 返回此 set 中大于等于给定元素的最小元素;如果不存在这样的元素,则返回 null。
System.out.println(tre.ceiling(3.0));//3.34
System.out.println(tre.ceiling(8.0));//null
  1. Object clone() 返回 TreeSet 实例的浅表副本。
  2. Comparator<? super E> comparator() 返回对此 set 中的元素进行排序的比较器;如果此 set 使用其元素的自然顺序,则返回 null。
  3. Iterator descendingIterator() 返回在此 set 元素上按降序进行迭代的迭代器。
  4. NavigableSet descendingSet() 返回此 set 中所包含元素的逆序视图。
System.out.println(tre.descendingSet());//[6.66, 5.55, 3.34, 1.11]
  1. E first() 返回此 set 中当前第一个(最低)元素。
System.out.println(tre.first());//1.11
  1. E floor(E e) 返回此 set 中小于等于给定元素的最大元素;如果不存在这样的元素,则返回 null。
System.out.println(tre.floor(8.0));//6.66
System.out.println(tre.floor(1));//null
  1. SortedSet headSet(E toElement) 返回此 set 的部分视图,其元素严格小于 toElement。
System.out.println( tre.headSet(5.55));//[1.11, 3.34]
  1. NavigableSet headSet(E toElement, boolean inclusive) 返回此 set 的部分视图,其元素小于(或等于,如果 inclusive 为 true)toElement。
System.out.println( tre.headSet(5.55,true));//[1.11, 3.34, 5.55]
  1. E higher(E e) 返回此 set 中严格大于给定元素的最小元素;如果不存在这样的元素,则返回 null。
System.out.println( tre.higher(5.54));//5.55
  1. E last() 返回此 set 中当前最后一个(最高)元素。
System.out.println( tre.last());//6.66
  1. E lower(E e) 返回此 set 中严格小于给定元素的最大元素;如果不存在这样的元素,则返回 null。
System.out.println( tre.lower(5.54));//3.34
  1. E pollFirst() 获取并移除第一个(最低)元素;如果此 set 为空,则返回 null。
System.out.println(tre.pollFirst());//1.11
System.out.println(tre);//[3.34, 5.55, 6.66]
  1. E pollLast() 获取并移除最后一个(最高)元素;如果此 set 为空,则返回 null。
System.out.println(tre.pollLast());//6.66
System.out.println(tre);//[3.34, 5.55]
  1. NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。
tre.add(2.22);
tre.add(3.36);
tre.add(8.89);
System.out.println(tre.subSet(3.34,true,6.66,true));//[3.34, 3.36, 5.55]
System.out.println(tre.subSet(3.34,false,6.66,false));//[3.36, 5.55]
  1. SortedSet subSet(E fromElement, E toElement) 返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。
System.out.println(tre.subSet(3.34,6.66));//[3.34, 3.36, 5.55]
  1. SortedSet tailSet(E fromElement) 返回此 set 的部分视图,其元素大于等于 fromElement。
System.out.println(tre.tailSet(3.36));//[3.36, 5.55, 8.89]
  1. NavigableSet tailSet(E fromElement, boolean inclusive) 返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。
System.out.println(tre.tailSet(3.36,false));//[5.55, 8.89]

(三)存储对象类型数据排序

TreeSet<Person> tre=new TreeSet();
tre.add(new Person("张三",20));
tre.add(new Person("李四",19));
tre.add(new Person("王五",22));

1)指定比较规则

TreeSet<Person> set=new TreeSet((o1,o2)-> ((Person)o2).getName()-((Person)o1).getAge());

2)内部比较器

public class Person implements Comparable<Person>{
	...
	@Override
	public int compareTo(Person o) {
		return this.name.compareTo(o.name);
	}
}

(四)存储对象类型数据去重

九、HashSet类(实现自Set接口)

HashSet : 无序 去重
底层: 有哈希表结构实现的(数组+链表+红黑树), 内部是由HashMap维护
优点: 查询,增加,删除效率高
缺点: 无序,去重

(一)使用

  1. HashSet() 构造一个新的空 set,其底层 HashMap 实例的默认初始容量是 16,加载因子是 0.75。HashSet has=new HashSet ();
  2. HashSet(Collection<? extends E> c) 构造一个包含指定 collection 中的元素的新 set。 HashSet has=new HashSet (con);
  3. HashSet(int initialCapacity) 构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和默认的加载因子(0.75)。 HashSet has=new HashSet (20);
  4. HashSet(int initialCapacity, float loadFactor) 构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和指定的加载因子。 HashSet has=new HashSet (16,0.75);

(二)新增方法

(三)存储对象类型数据排序、去重

HashSet has=new HashSet ();
has.add(new Person("张三",20));
has.add(new Person("李四",19));
has.add(new Person("王五",22));

重写hashCode()和equals()方法

public class Person{
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode()); //字符串的内容相同,hashcode就相同
		return 0;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return false;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值