JavaDay4
集合
描述多种不同种类事物特点的容器:集合
对不同的容器也会提供不同的集合类
Collection接口
Collection接口中存在两个子接口:List和Set
首先来学习Collection接口
/*
Collection接口中的方法:
boolean add(Object e)
boolean remove(Object o)
void clear()
boolean contains(Object o)
boolean isEmpty()
int size()
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean containsAll(Collection c)
boolean retainAll(Collection c)
*/
public class CollectionDemo1 {
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add(11);
c1.add(22);
c1.add(33);
c1.add(44);
System.out.println("c1: " + c1);
Collection c2 = new ArrayList();
c2.add(33);
c2.add(44);
c2.add(55);
c2.add(66);
System.out.println("c2: " + c2);
//boolean remove(Object o) ;// 从集合中移除某一个元素
c1.remove(100);
System.out.println("c1: " + c1);
//void clear() 将集合中元素清空
c1.clear();
System.out.println("c1: " + c1);
//boolean contains(Object o) 判断集合中是否包含某个类型的元素
System.out.println(c1.contains("hello"));
//boolean isEmpty() 判断集合中是否有元素存在
System.out.println(c1.isEmpty());
//int size() 获取集合中元素的个数【集合的长度】
System.out.println(c1.size());
//boolean addAll(Collection c) 将另一个集合中的元素批量添加
c1.addAll(c2);
System.out.println("c1: "+c1);
System.out.println("c2: "+c2);
//boolean removeAll(Collection c) 从一个集合中移除另一个集合所有的元素
c1.removeAll(c2);
System.out.println("c1: "+c1);
System.out.println("c2: "+c2);
//boolean containsAll(Collection c) 判断A集合中是否完整包含集合B中的元素
System.out.println(c1.containsAll(c2));
}
}
对Collection集合的遍历,我们利用Object[]和c1.toArray() 把集合转成数组,可以实现集合的遍历
-
迭代器
Iterator iterator() 迭代器,Collection集合的专用遍历方式
遍历迭代器对象,就可以获取迭代器中的元素了
我们点进Iterator观察发现,Iterator是一个接口,c1.iterator()结果一定是一个实现了Iterator接口的实现类对象
要想使用迭代器对象中的方法,就去iterator()如何实现的。
我们这里是借助了ArrayList子类创建的对象,去到ArrayList类中观察发现该方法返回的是一个new Itr()对象
因为Itr类实现了Iterator接口,可以直接接收
去Itr类中查找方法的实现,去使用并获取迭代器中的元素值
我们发现Itr中有两个重要的方法:
hasNext()和next()public class CollectionDemo4 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("hello"); c1.add("world"); c1.add("java"); c1.add("hadoop"); c1.add("redis"); System.out.println("c1: " + c1); System.out.println("-------------------------"); //调用方法获取迭代器对象 Iterator iterator = c1.iterator();//Iterator iterator = new Itr() // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); while (iterator.hasNext()){ Object o = iterator.next(); //Object o = "hello" String s = (String)o; System.out.println(s+": "+s.length()); } } }
List接口
List集合中元素特点是有序、队列存储、允许元素重复,同时具有索引的概念
同时List集合具有三个子类:
ArrayList
Vector
LinkdedList
public class ListDemo2 {
public static void main(String[] args) {
//借助ArrayList实现子类创建List接口的对象
List list1 = new ArrayList();
list1.add("hello");
list1.add("world");
list1.add("apple");
list1.add("hadoop");
list1.add("redis");
list1.add("world"); // 集合的末尾处添加
System.out.println("list1: " + list1);
//[hello, world, apple, hadoop, redis, world]
// void add(int index,Object element)
list1.add(3,"查哥");
System.out.println("list1: " + list1);//[hello, world, apple, 查哥, hadoop, redis, world]
//Object remove(int index) 根据索引移除元素
System.out.println(list1.remove(10)); // 返回被删除的元素
System.out.println("list1: " + list1);
// Object get(int index) 通过索引获取元素
System.out.println(list1.get(3));
System.out.println("list1: " + list1);
// Object set(int index,E element) 根据索引修改值
System.out.println(list1.set(3, "zharx")); //返回索引位置上的旧值
System.out.println("list1: " + list1);
// ListIterator listIterator() 是List集合专有的迭代器对象 要想倒着遍历,必须得先正着遍历一次
ListIterator listIterator = list1.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("--------------------------------------");
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
}
}
List集合的遍历方式:
1、先转数组再遍历
2、获取迭代器遍历
3、根据索引和长度使用for循环遍历【List集合专属遍历方式】
ArrayList
底层为数组,内存连续,查询快,增删慢,现成不安全,效率高。
Vector
底层为数组,查询快,增删慢,线程安全,效率低。【一般不使用】
LinkedList
底层为链表,内存不连续,查询慢,增删快,线程不安全,效率高。
Set接口
元素无序,且唯一。
同时Set具有两个子类:
HashSet
TreeSet
HashSet
底层为HashMap,为数组+列表的形式,元素不重复,无序。
适用于需要快速查找、插入和删除的场景,且不关心元素的顺序。
TreeSet
底层为红黑树,元素按照自然顺序或比较器顺序进行排序。
适用于需要元素有序且支持范围查询的场景。
Map接口
每一个元素由一个键和一个值组成。在同一个Map集合中,键是唯一的,值可以发生重复。
同时Map接口也有三个子类:
HashMap
TreeMap
LinkedHashMap
/*
Map接口中的方法:
V put(K key,V value)
V remove(Object key)
void clear()
boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
int size()
V get(Object key)
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K,V>> entrySet()
*/
public class MapDemo {
public static void main(String[] args) {
//借助Map子类HashMap来创建对象
Map<Integer, String> map1 = new HashMap<>();
//String put(Integer key,String value) // 返回被覆盖的旧值
map1.put(1001, "张三");
map1.put(1002, "李四");
map1.put(1001, "王五");
map1.put(1003, "赵六");
map1.put(1004, "王二麻");
map1.put(1005, "王林");
System.out.println("map1: " + map1);
System.out.println("-----------------");
//map1: {1001=王五, 1002=李四, 1003=赵六}
//V remove(Object key) 根据键删除一个键值对, 返回对应的值
System.out.println(map1.remove(1001));
System.out.println("map1: " + map1);
//void clear() 清空map集合中所有的键值对元素
map1.clear();
System.out.println("map1: " + map1);
// boolean containsKey(Object key) 判断是否包含某个键
System.out.println(map1.containsKey(1001));
// boolean containsValue(Object value) 判断是否包含某个值
System.out.println(map1.containsValue("李四"));
//boolean isEmpty() 判断集合中是否有键值对
System.out.println(map1.isEmpty());
// int size() 获取键值对个数
System.out.println(map1.size());
// V get(Object key) 根据键获取值
System.out.println(map1.get(1005));
System.out.println("map1: " + map1);
// Set<K> keySet() 将所有的键拿出来放到一个Set集合中
Set<Integer> keys = map1.keySet();
for (Integer key : keys) {
System.out.println(key);
}
// Collection<V> values() 将所有的值拿出来放到一个Collection集合中
Collection<String> values = map1.values();
for (String value : values) {
System.out.println(value);
}
// Set<Map.Entry<K,V>> entrySet() 将每个键值对拿出来放入到Set集合中
// map集合遍历方式1:一次获取所有的键值对,依次遍历得到每个键值对的键和值
Set<Map.Entry<Integer, String>> entries = map1.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
// entry - 键值对
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "-" + value);
}
// map集合遍历方式2:先获取所有的键,遍历键,根据键得到对应的值
for (Integer key : keys) {
// 根据键获取值
String value = map1.get(key);
System.out.println(key + "-" + value);
}
}
}
HashMap
底层数组+链表实现,线程不安全,效率高。
TreeMap
底层红黑二叉树,可实现对元素的排序。
LinkedHashMap
底层HashMap+LinkedList实现,通过HashMap实现key+value存储
来源链接:https://www.cnblogs.com/Roxan-bd/p/18669356
没有回复内容