集合
特点: 提供一种存储空间可变的存储模型,存储的数据容量可以随时发生变化
集合类体系结构
集合
Collection 单列集合
List(可重复)
ArrayList
LinkedList
...
Set (不可重复)
HashSet
TreeSet
...
Map 双列集合
HashMap
...
Collection
是单列集合的顶层接口
创建Collection集合对象的方式:多态
常见方法:
boolean add(E e) //添加元素
boolean remove(Object o) //从集合中移除指定元素
void clear() //清空集合中的元素
boolean contains(Object o) //判断集合是否存在指定元素
boolean isEmpty() //判断集合是否为空
int size() //集合的长度
eg:
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
//boolean add(E e)
c.add("a");
c.add("b");
c.add("c");
System.out.println(c); //[a, b, c]
//remove
c.remove("a");
System.out.println(c); //[b, c]
//contains
System.out.println(c.contains("b")); //true
//size
System.out.println(c.size()); //2
//clear
c.clear();
//isEmpty
System.out.println(c.isEmpty()); //true
}
集合遍历
Iterator–迭代器 [集合专用遍历方法]
通过 Iterator iterator()来获取集合中元素的迭代器
Iterator的常见方法
E next() //返回迭代中的下个元素
boolean hasNext() //如果迭代具有更多元素,则返回true
eg:
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("a");
c.add("b");
c.add("c");
//获取迭代器
Iterator<String> iterator = c.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
}
}
List
有序集合、有索引、允许重复
特有方法
void add(int index,E element) //在指定位置插入指定元素
E remove(int index) //删除指定元素
E set(int index,E element) //修改指定位置的元素
E get(int index) //返回指定位置的元素
eg:
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
//add
list.add(1,"wow");
System.out.println(list); //[a, wow, b, c]
//remove
list.remove("b");
System.out.println(list);//[a, wow, c]
//set
list.set(1,"Hei");
System.out.println(list);//[a, Hei, c]
//get
System.out.println(list.get(2));//c
}
ArrayList
底层结构是数组。查询快、增删慢
LinkedList
底层结构是链表。查询慢、增删快
特有方法
public void addFirst(E e) //在开头插入指定元素
public void addLast(E e) //在末尾增加指定元素
public E getFirst() //返回第一个元素
public E getLast() //返回最后一个元素
public E removeFirst() //删除并返回第一个元素
public E removeLast() //删除并返回最后一个元素
Set
不允许重复,无索引
HashSet
由哈希表支持。
对迭代顺序不做保证。
eg:
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
hashSet.add("123");
hashSet.add("456");
hashSet.add("789");
for (String s : hashSet) {
System.out.println(s);
}
/*
123
456
789
*/
}
LinkedHashSet
底层是哈希表和链表
有可预测的迭代顺序
无重复值
eg:
public static void main(String[] args) {
LinkedHashSet<String> hashSet = new LinkedHashSet<>();
hashSet.add("123");
hashSet.add("456");
hashSet.add("789");
for (String s : hashSet) {
System.out.println(s);
}
/*
123
456
789
*/
}
TreeSet
按照一定的规则排序
无索引、无重复
public static void main(String[] args) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(10);
treeSet.add(20);
treeSet.add(40);
treeSet.add(30);
for (Integer integer : treeSet) {
System.out.println(integer);
}
/*
10
20
30
40
*/
}
Map
public interface <K,V> K:键的类型,V:值的类型
键不可重复,若键重复,则值将发生改变。
基本方法
V put(K key,V value) //添加元素
V remove(Object key) //根据键删除元素
void clear() //移除所有元素
boolean containsKey(Object key) //判断是否含有该减
boolean containsValue(Object value) //判断是否含有该值
boolean isEmpty() //判断集合是否为空
int size() //集合的长度
eg:
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
//放入元素
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
System.out.println(map); //{1=aaa, 2=bbb, 3=ccc}
System.out.println(map.size()); //3
//remove
map.remove(2);
System.out.println(map); //{1=aaa, 3=ccc}
//containsKey
System.out.println(map.containsKey(1)); //true
//containsValue
System.out.println(map.containsValue("aaa"));//true
//clear
map.clear();
System.out.println(map);//{}
//isEmpty
System.out.println(map.isEmpty());//true
}
获取方法
V get(Object key) //根据键获得值
Set<K> keySet() //获取所有键的集合
Collection<V> values() //获取所有值的集合
Set<Map.Entry<K,V>> entrySet() //获取所有键值对对象的集合
eg:
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
//放入元素
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
System.out.println(map); //{1=aaa, 2=bbb, 3=ccc}
//get
System.out.println(map.get(1)); //aaa
//keySet
System.out.println(map.keySet()); //[1, 2, 3]
//values
System.out.println(map.values()); //[aaa, bbb, ccc]
}
Map集合的遍历
第一种方式
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
//放入元素
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
System.out.println(map); //{1=aaa, 2=bbb, 3=ccc}
//获取所有键的结合
Set<Integer> integers = map.keySet();
//获取每一个键
for (Integer integer : integers) {
//通过键去寻找值
String s = map.get(integer);
System.out.println(s);
/*
aaa
bbb
ccc
*/
}
}
第二种方式
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
//放入元素
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
System.out.println(map); //{1=aaa, 2=bbb, 3=ccc}
//获取所有键值对对象的集合
Set<Map.Entry<Integer, String>> entries = map.entrySet();
//遍历键值对对象的集合
for (Map.Entry<Integer, String> entry : entries) {
//获取键值对对象获取键和值
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
/*
1=aaa
2=bbb
3=ccc
*/
}
}