java Collection集合常用功能
java.util.Collection接口
所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
任意的单列集合都可以使用Collection接口中的方法
共性的方法:
public boolean add(E e):把给定的对象添加到当前集合中 。
public void clear() :清空集合中所有的元素。
public boolean remove(E e): 把给定的对象在当前集合中删除。
public boolean contains(E e): 判断当前集合中是否包含给定的对象。
public boolean isEmpty(): 判断当前集合是否为空。
public int size(): 返回集合中元素的个数。
public Object[] toArray(): 把集合中的元素,存储到数组中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package com.itheima.demo01.Collection;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;public class Demo01Collection { public static void main (String[] args) { Collection<String> coll = new HashSet<>(); System.out.println(coll); boolean b1 = coll.add("张三" ); System.out.println("b1:" +b1); System.out.println(coll); coll.add("李四" ); coll.add("李四" ); coll.add("赵六" ); coll.add("田七" ); System.out.println(coll); boolean b2 = coll.remove("赵六" ); System.out.println("b2:" +b2); boolean b3 = coll.remove("赵四" ); System.out.println("b3:" +b3); System.out.println(coll); boolean b4 = coll.contains("李四" ); System.out.println("b4:" +b4); boolean b5 = coll.contains("赵四" ); System.out.println("b5:" +b5); boolean b6 = coll.isEmpty(); System.out.println("b6:" +b6); int size = coll.size(); System.out.println("size:" +size); Object[] arr = coll.toArray(); for (int i = 0 ; i < arr.length; i++) { System.out.println(arr[i]); } coll.clear(); System.out.println(coll); System.out.println(coll.isEmpty()); } }