单列集合的顶层接口是iava.util.Collection,Collection下有两个子接口:List和Set
java.util.Map接口是双列集合的顶层接口,Map接口下常用的实现类
Collection接口是所有单列集合的顶级接口,该接口没有直接的实现类,如果要想使用Collection接口中的方法,那么就要使用Set接口例如HashSet或者是List接口的实现类,例如ArrayList。
因为接口是不能被实例化,而且ArrayList实现类实现了Collection接口的所有抽象方法。Collection接口中的抽象方法,所有的单列集合实现类都实现了。
Collection con = new ArrayList();con.add("北京");con.add("上海");con.add("广州");con.add("深圳");con.add("东北");System.out.println("初始化的Collection"+con);Collection con2 = new ArrayList();con2.add("东京");con2.add("首尔");System.out.println("初始化的子集con2"+con2);con.addAll(con2);System.out.println("添加集合后的con"+ con );System.out.println("集合中是否存在元素:" + con.contains("东北"));System.out.println("集合中是否存在集合:" + con.containsAll(con2));System.out.println("当前集合的元素个数" + con.size());con.remove("东北");System.out.println("删除元素之后" + con);con.removeAll(con2);System.out.println("删除集合之后" + con);con.clear();System.out.println("清空之后" + con);System.out.println("集合是否为空"+ con.isEmpty());
初始化的Collection[北京, 上海, 广州, 深圳, 东北]
初始化的子集con2[东京, 首尔]
添加集合后的con[北京, 上海, 广州, 深圳, 东北, 东京, 首尔]
集合中是否存在元素:true
集合中是否存在集合:true
当前集合的元素个数7
删除元素之后[北京, 上海, 广州, 深圳, 东京, 首尔]
删除集合之后[北京, 上海, 广州, 深圳]
清空之后[]
集合是否为空true
toArray() 将集合转换成数组
Arrays.asList() 将数组转换成集合