一、Collection
集合主要分为两大系列:Collection和Map,Collection 表示一组对象(无序可重复),
Map表示一组映射关系或键值对。
public class CollectionText {
@Test
public void test1(){
//Collection 中得方法
//定义一个集合
Collection<String> coll = new ArrayList<>();
//添加元素
coll.add("小迪");
coll.add("小七");
coll.add("小强");
coll.add("小国");
coll.add("小国");
System.out.println("coll = " + coll);
//判断元素是否在集合中
boolean 效果 = coll.contains("效果");
System.out.println("效果 = " + 效果);
boolean b = coll.contains("小七");
System.out.println("b = " + b);
System.out.println("--------------------------" );
//size()看集合中有几个元素
int size = coll.size();
System.out.println("size = " + size);
System.out.println("------------------------------" );
//删除元素
boolean 小强 = coll.remove("小强");
System.out.println("小强 = " + 小强);
System.out.println("coll = " + coll);
//size()看集合中有几个元素
int siz1e = coll.size();
System.out.println("size = " + siz1e);
System.out.println("*-----------------------------*" );
Object[] objects = coll.toArray();
for (Object object : objects) {
System.out.print("object = ," + object);
}
System.out.println("----------------- >");
//清空集合
coll.clear();
System.out.println("coll = " + coll);
//判断是否为空
System.out.println("coll.isEmpty() = " + coll.isEmpty());
System.out.println(" ----------------------------------" );
//将旧得集合得元素复制到新得集合
Collection<String> coll2 = new ArrayList<>();
coll2.addAll(coll);
System.out.println("coll2 = " + coll2);
//返回hashcode值
int i = coll.hashCode();
System.out.println("i = " + i);
int i1 = coll2.hashCode();
System.out.println("i1 = " + i1);
}
}
二、Set集合
Collection子接口之一:Set接口
Set : 无序 : 使用者无法决定放在哪个位置, 位置由对象自身决定.(元素无序、不可重复的集合 )
* HashSet : 使用哈希算法实现的Set集合.优点 : 全是优点.
* 缺点 : 基于数组.
* TreeSet : 基于二叉搜索树(红黑树)实现的Set集合.TreeSet实现了Compare接口
* 优点 : 对内存要求低, 检索性能非常好, 因为它是使用二分查找法.
* 缺点 : 遍历性能差, 插入和删除都性能不好.
* 适用于什么场景: 偶尔修改数据, 频繁地检索数据.
三、List集合
Collection子接口之二: List接口
List : 有序, 由使用者决定放在哪个位置(下标) (元素有序,可重复的集合)
* ArrayList : 基于数组实现的List集合
* 缺点 : 对内存要求高, 要求连续, 非末端操作效率最低(因为会有大量元素的移动)
* 优点 : 末端操作很快, 遍历速度不错, 检索速度一般.
* 适用于什么场景 : 存档数据, 不怎么删除和插入, 偶尔检索.
//list接口中得常用方法
List<String> list = new ArrayList<>();
// 往 尾部添加 指定元素
list.add("123");
list.add("456");
list.add("789");
list.add("101");
list.add("102");
list.add("101");
list.add("102");
System.out.println("list = " + list);
//往指定位置添加元素
list.add(1,"没头脑");
System.out.println("list = " + list);
System.out.println("----------------------->" );
//删除指定索引位置得元素
String remove = list.remove(3);
System.out.println("list = " + list);
System.out.println("----------------------->" );
//修改指定位置元素
list.set(3,"小迪都");
System.out.println("list = " + list);
//遍历
for (int i = 0; i < list.size(); i++) {
System.out.println("list.get(i) = " + list.get(i));
}
System.out.println("===============================>" );
//增强for循环
for (String s : list) {
System.out.println("s = " + s);
}
System.out.println("------------------------->" );
//返回元素得下标
int i = list.indexOf("456");
System.out.println("i = " + i);
System.out.println(" ========================》 ");
//返回对象最后一次出现得下标,注意范围是左闭右开
int i1 = list.lastIndexOf("102");
System.out.println("i1 = " + i1);
System.out.println(" -----------------------?" );
List<String> strings = list.subList(2, 4);
for (String string : strings) {
System.out.println("string = " + string);
}
}
ArrayList的底层源码大概理解
我们向数组中添加一个元素,会在数组的末尾来进行插入,
如果容量不够的话,我们会会数组进行一个扩容,扩容标准是1.5倍,
然后将添加的那个元素放在扩容后的下标位置上,无论我们对数组中的数据进行一个什么样子的操作,
都会记录一下modCount(修改次数),将modCount 赋值给期望的修改次数,
最后会对这两个值进行一个判断,如果相等正常输出,如果不相等就不能愉快的输出了。
LinkedList : 基于链表实现的List集合.
* 优点 : 对内存要求低.插入和删除数据非常快.
* 缺点 : 检索和遍历是最慢.
* 适用于什么场景 : 频繁插入或删除数据, 几乎不检索.
public void test3(){
//LinkedList 接口中得一些方法
LinkedList<String> strings = new LinkedList<>();
strings.add("萧小");
strings.add("小七");
strings.add("西奥");
//在首元素添加元素
strings.addFirst("果果");
//在末尾元素添加元素
strings.addLast("小琪琪");
//获取第一个元素
String first = strings.getFirst();
System.out.println("first = " + first);
//获取最后一个元素
String last = strings.getLast();
System.out.println("last = " + last);
//移除第一个元素
String s = strings.removeFirst();
//移除最后一个元素
String s1 = strings.removeLast();
System.out.println("--------------》" );
//遍历看效果
for (String string : strings) {
System.out.println("string = " + string);
}
}
LinkedList 的源码大概理解
四、Map
Map是与Collection并列存在,用于保存映射关系的Key-Value的数据,Key 和Value都可以是任意类型的数据,Key是可以重复的并且Key用Set来存放,Value是不允许重复的
Map的实现类之一:HashMap 中的常用方法
@Test public void text(){ Map<Integer, String> map = new HashMap<>(); //添加操作 map.put(1,"张文强"); map.put(2,"张文倩"); map.put(3,"崔钰"); map.put(4,"重庆啊"); //删除操作 // map.clear(); //全部删除 map.remove(1); //元素查询操作 System.out.println("map.get(2) = " + map.get(2)); //判断key是否包含3 System.out.println("map.containsKey(3) = " + map.containsKey(3)); //判断value是否包含张文强 System.out.println("map.containsValue= " + map.containsValue("张文强")); System.out.println("map.isEmpty() = " + map.isEmpty()); //遍历所有的key System.out.println(" 获取所有的Keu" ); Set<Integer> integers = map.keySet();//获取所有的key Iterator<Integer> iterator = integers.iterator(); while (iterator.hasNext()){ Integer next = iterator.next(); System.out.println("next = " + next); } //遍历所有的value System.out.println(" 获取所有的value" ); Collection<String> values = map.values(); Iterator<String> iterator1 = values.iterator(); while (iterator1.hasNext()){ String next = iterator1.next(); System.out.println("next = " + next); } //遍历所有的映射关系 System.out.println(" 遍历所有的映射关系 " ); Set<Map.Entry<Integer, String>> entries = map.entrySet(); Iterator<Map.Entry<Integer, String>> iterator2 = entries.iterator(); while (iterator2.hasNext()){ Map.Entry<Integer, String> next = iterator2.next(); System.out.println( next.getKey()+"---> "+next.getValue()); } //获取元素个数 System.out.println("map.size() = " + map.size()); }
Map的实现类之二:TreeMap
基本方法都类似,主要是TreeMap的排序
1.自然排序:
TreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastExceptionTreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException
2.定制排序:
创建 TreeMap 时,传入一个 Comparator 对象,该对象负责对 TreeMap 中的所有 key 进行排序。此时不需要 Map 的 Key 实现 Comparable 接口