package com.day11.json;
import org.junit.Test;
import java.util.*;
/**
* Author: Json
* Date: 2021/9/15
**/
public class Json {
public static void main(String[] args) {
System.out.println("加油咯");
//jdk8 注解的新特性 可重复注解 类型注解
//java 集合 可分为 collection 和 Map 两种体系
// Collection 单列数据 定义了存取一组对象的方法的集合
//List 元素有序 可重复的集合
//Set 元素无需,不可重复的集合
// Map接口 : 双列数据 保存具有映射关系 “key->value” 的集合
}
@Test
public void CollMap(){
Collection collection=new ArrayList();
//add() 看到e 的形参 一般就是 Obj
//添加元素
collection.add("AA");
collection.add("BB");
collection.add(111);
collection.add(new Date());
//获取元素的个数 size()
System.out.println(collection.size());
//addAll()
Collection collection1=new ArrayList();
collection1.add("cc");
collection1.add(222);
//合并元素
collection.addAll(collection1);
System.out.println(collection);
//isEmpty 判断当前集合是否为空
System.out.println(collection.isEmpty());
//清空元素
collection.clear();
System.out.println("=========================");
Collection coll=new ArrayList();
coll.add(123);
//判断集合中是否包含 某个元素 contains()
boolean collStatus=coll.contains(123); // true
System.out.println(collStatus);
coll.add(new String("Json"));
System.out.println(coll.contains(new String("Json"))); //true 实际是对比了内容 调用的是底层的重写的equals 方法
coll.add(new Person("Json",20));
System.out.println(coll.contains(new Person("Json",20))); //false 调用的是 底层 Object的 ==
// 如果自定义类中 new Person() 也想把 上面这个判断 变成true 需要在这个自定义类中 重写 equals 方法
//contains() 在判断时 会调用obj的equals() 方法
// 自定义类时 一般就会重写 equals() 方法
// containsAll();
//Arrays.asList(123,445); 另一种声明方式 多态的体现
Collection coll2 = Arrays.asList(123,445);
System.out.println(coll2.containsAll(Arrays.asList(456)));
//remove() 移除 Arrays.asList 这样声明的集合 不能做移除操作
// System.out.println(coll2.remove(123));
//removeAll() 全部多个
//求交集 retainAll() 返回当前集合
coll.retainAll(coll2);
System.out.println(coll);
// equals() 对比两个对象是否相等 他是一个位置 一个位置 比较的 有序的
// hashCode() 返回当前对象的哈希值
//Collection 子接口之一 List接口
//元素 有序的 可重复的 动态的数组
//具体有三个实现类
// ArrayList // 作为List 接口的主要实现类 线程不安全 效率高 使用Object[] 存储
// LinkedList 对于频繁的插入 删除操作 使用此类效率高 底层使用的双向链表存储
// Vector 古老实现类 线程安全 效率低
// 这三个类的异同?
// 同: 三个类都实现了List接口 存储特点相同: 有序的 可重复的
// 异: 见上
//问题的关键:
//
// 为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢?
// 问题就在于List接口有多个实现类,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类,如
//
//LinkedList或者Vector等等,这时你只要改变这一行就行了: List list = new LinkedList(); 其它使用了list地方的代码根
//
//本不需要改动。
//
// 假设你开始用ArrayList alist = new ArrayList(), 这下你有的改了,特别是如果你使用了ArrayList实现类特有的方法
//
//和属性。
//
//
//
// 个人见解
//
// 上面的说明,我是在看了设计模式才恍然大悟的,设计模式的原则和理念果然是强大的,只有好好学习了面向对
//
//象原则和设计模式,那么理解上面的就不再是难度,这样的好处是为了代码的可维护性,可复用性,可扩展性以及灵
//
//活性,再者就是这符合了里氏代换原则和开闭原则。看来学习设计模式好处是大大的。
ArrayList arrayList =new ArrayList();
arrayList.add("123");
System.out.println(arrayList);
//常用方法 在实际开发中再体会把
//Collection 子接口之二 Set接口
//1.Set 接口的框架结构
// 无序的 不可重复的
// HashSet 作为set接口主要实现类 线程不安全 可以存储null值
// LinkHashSet 作为HashSet的子类 遍历其内部数据时 可以按照添加的顺序遍历
// TreeSet 可以按照添加的对象的指定属性 进行排序
//重写方法
// 在自然排序中 比较两个对象是否相同的标准 : compareTo() 返回是0; 不在是equals();
// 在定制排序中 比较两个对象是否相同的标准 :compare() 返回是0;不再是equals()
// 使用说明
// 向TreeSet中添加的数据 要求是相同类的对象
// 两种排序方式 自然排序(实现Comparable 接口) 和 定制排序(comparator 接口)
//Set 没有额外的新添加的方法 他使用的Collection的方法
Set set =new HashSet();
set.add(123);
set.add("123123");
System.out.println(set);
}
}