package com.sf.day14;

import org.junit.Test;

import javax.swing.*;
import java.security.PublicKey;
import java.util.*;
import java.util.function.Predicate;

public class Test01 {
   @Test
    public void t1(){
       // arrayList 是Collection接口的子接口的典型实现类之一
       Collection collection = new ArrayList();  //多态
       collection.add("小李广");
       collection.add("扫地僧");
       collection.add("石破天");
       collection.add("石破天");
      System.out.println(collection);

      //addAll
       Collection collection1 = new ArrayList();
       collection1.add("扫地僧1");
       collection1.add("石破天1");
       collection.addAll(collection1);
       System.out.println(collection);
       System.out.println(collection1);
       collection1.addAll(collection);
       System.out.println(collection);
       System.out.println(collection1);
       //判断集合中元素的个数
       System.out.println(collection.size());
   }


   @Test
   public void t2(){
       Collection<Integer> coll = new ArrayList();
       //当前集合为空  返回true   否则返回false
       System.out.println("coll在添加元素之前，isEmpty = " + coll.isEmpty());
//       coll.add(Integer.valueOf("A"));
       //string类型转换成integer类型   Integer.valueof()
//       coll.add("扫地僧");
//       coll.add("佛地魔");
       //当前集合中的元素个数
       System.out.println("coll的元素个数" + coll.size());

       System.out.println("coll在添加元素之后，isEmpty = " + coll.isEmpty());

       System.out.println("当前集合是否包含 小李广"+coll.contains("小李广"));
       System.out.println("当前集合是否包含 小李"+coll.contains("小李"));


       Collection collection = new ArrayList();
       collection.add("小李广");
       collection.add("扫地僧");
       collection.add("石破天");
       collection.add("佛地魔");
       collection.add("佛地魔");

       System.out.println("coll集合中是否包含collection集合"+coll.containsAll(collection));
       System.out.println(collection.equals(coll));

       System.out.println("=================================");
//       boolean remove = collection.remove("佛地魔");
//       System.out.println(remove);
//       System.out.println(collection);
//       collection.clear();
//       System.out.println("判断当前集合是否为空"+collection.isEmpty());
//       System.out.println(collection);  //[]

//       collection.removeAll(coll);  //从当前集合中删除所有与coll集合中相同的元素
//       System.out.println(collection);

       //从当前集合中删除两个集合中不同的元素，使得当前集合仅保留与coll集合中的元素相同的元素，即当前集合中仅保留两个集合的交集；保留两个集合中交集的元素。
//       boolean b = collection.retainAll(coll);
//       System.out.println(collection);

       System.out.println(collection);
       List<String> list = new ArrayList<>();
       list.add("zhangsan");
       list.add("lisi");
       list.add("wangwu");
       System.out.println(list.size());
       String s = list.get(0);
       System.out.println(s);
       list.set(1,"zhanghsan33");
       System.out.println(list.get(1));

       //第一种方法遍历
       for (int i = 0;i<list.size();i++){
           String s1 = list.get(i);
           System.out.println(s1);
       }
       System.out.println("========");
       //    iter
       for (String s1 : list) {
           System.out.println(s1);
       }
   }

   @Test
   public void t(){
       Collection collection = new ArrayList();
       collection.add("小李广");
       collection.add("扫地僧");
       collection.add("石破天");
       collection.add("佛地魔");
       collection.add("佛地魔1");
       collection.add("佛地魔2");
       collection.add("佛地魔3");
       //将集合转换成数组
       Object[] objects = collection.toArray();
       System.out.println(objects);
       for (Object object : objects) {
           System.out.println(object);
       }

       //数组转换成集合的方法
       List<Object> objects1 = Arrays.asList(objects);
       String o1 = (String) objects1.get(1);
       System.out.println("==========="+o1);
       for (Object o : objects1) {
           System.out.println(o);
       }
   }

   @Test
   public void t4(){
       //字符串数组
       List<String> listString = new ArrayList<>();
       listString.add(1,"zhangsan");
       listString.add(0,"lisa");


//       List<Object> obj = new ArrayList<>();
//       obj.add("zahngsan");
//       obj.add(12);
//       obj.add(new String());

       List<Person> personList = new ArrayList<>();
       personList.add(new Person(12,"name"));
       personList.add(new Person(12,"name"));
       personList.add(new Person(12,"name"));
       System.out.println(personList);

       /**
        *
        */
       List<Worker> workerList  = new ArrayList<>();
//       workerList.add(new Worker())
   }


   @Test
   public void t5(){
       Collection<Integer> collection = new ArrayList<>();
       for (int i = 0 ;i<20;i++){
           collection.add(i+1);
       }

       //第一种遍历的方式
//       for (Integer integer : collection) {
//           System.out.println(integer);
//       }

       Iterator<Integer> iterator = collection.iterator();
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
       while (iterator.hasNext()){ //判断是否有下一个元素
           System.out.println(iterator.next());   //获取下一个元素
       }
   }

   @Test
   public void t6(){
       List<String> list = new ArrayList<>();
       list.add("abc");
       list.add("afg");
       list.add("amf");
       list.add("mfg");
       list.add("age");
       list.add("ofh");

       Iterator<String> iterator = list.iterator();
       while (iterator.hasNext()){
           String next = iterator.next();
           if(next.contains("a")){
               iterator.remove();
           }
           System.out.println(next);
       }
       System.out.println(list);
   }

   @Test
   public void t7(){
       List<String> listString = new ArrayList<>();
       listString.add("zhangsan");
       listString.add("lisa");
       Iterator<String> iterator = listString.iterator();
       while (iterator.hasNext()){
           String next = iterator.next();
           if(next.equals("lisa")){
               iterator.remove();
           }
       }

       for (String s : listString) {
           System.out.println(listString);
       }
   }

   @Test
   public void t8(){
       Collection coll = new ArrayList();
       coll.add(1);
       coll.add(2);
       coll.add(3);
       coll.add(4);
       coll.add(5);
       coll.add(6);
       Iterator iterator = coll.iterator();
       while (iterator.hasNext()){
           Integer next = (Integer) iterator.next();
           if(next%2==0){
               iterator.remove();
           }
       }
       System.out.println(coll);
   }

   @Test
   public void t9(){
       Collection coll = new ArrayList();
       coll.add("小李广");
       coll.add("扫地僧");
       coll.add("石破天");
       coll.add("佛地魔");
       System.out.println(coll);
        coll.removeIf(new Predicate() {
            @Override
            public boolean test(Object o) {
                String str = (String) o;
                boolean flag = str.contains("小");
                return flag;
            }
        });
       System.out.println(coll);
   }

   @Test
   public void t10(){
       List<String> list  =  new ArrayList<>();
       list.add("Hello");
       list.add("World");
       list.add("Hello");
       list.add("Learn");
       list.remove("Hello");
       list.remove(0);
       for (String s : list) {
           System.out.println(s);
       }
   }

}
