类集框架-容器类
位于java.util包中,用于存储和管理对象。
主要分为三大类-集合、列表和映射。
集合Set-对象无序,无重复对象
列表List-对象按照索引位置排序,可以有重复对象
映射Map-每一个元素包含一个键对象和一个值对象,键对象不可以重复,值对象可以重复
ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("a"); arrayList.add("b"); arrayList.add("c"); arrayList.add("d"); arrayList.remove(1); arrayList.size(); arrayList.get(1);
Collection接口中的主要方法(Set、List的父类)
add(Object o);
remove(Object o);
clear();
isEmpty();
size();
Iterator接口中的主要方法
hasNext();
next();
Iterator -> Collection -> Set -> HashSet;
Set<String> set = new HashSet<String>(); set.add("a"); set.add("b"); set.add("c"); set.add("d"); Iterator<String> it = set.iterator(); while(it.hasNext()){ String s = it.next(); System.out.println(s); }
映射Map<K,V>,如果键已经存在,新的值将覆盖原来的值。