Java集合collection之List

2019-12-30 10:41:05 浏览数 (1)

Collection接口--Sort和List接口的父接口

List:

代码语言:javascript复制
package learn.collection;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;

public class list {

     private List<String> list = new ArrayList<String>();

     @Test(groups = {"list"},priority = 0)
     public void addmethod(){
         int n = 1;
         String param = "add";
         String param2 = "abb";
         list.add(param);
         System.out.println("add方法:list最后一位之后插入" param ":" list);
         list.add(n,param2);
         System.out.println("list第" n "位插入" param2 ":" list);
     }

     @Test(groups = {"list"},priority = 1)
     public void addAllmethod(){
         int n = 1;
         List<String> addlist = new ArrayList<String>();
         addlist.add("addAll");
         list.addAll(addlist);
         System.out.println("list最后一位之后插入list集合数据" addlist ":" list);
         list.addAll(n,addlist);
         System.out.println("list第" n "位插入list集合数据" addlist ":" list);
     }

     @Test(groups = {"list"},priority = 2)
     public void containsmethod(){
         List<String> judgelist = new ArrayList<>();
         judgelist.add("addAll");
         judgelist.add("abbAll");
         for(Object o:judgelist){
             boolean judge = list.contains(o);
             if(judge){
                 System.out.println("list数据中包含" o);
             }
             else{
                 System.out.println("list数据中不包含" o);
             }
         }
     }

     @Test(groups = {"list"},priority = 3)
     public void containsAllmethod(){
         String add = "add";
         String addAll = "addAll";
         List<String> containsAllList = new ArrayList<String>();
         containsAllList.add(add);
         containsAllList.add(addAll);
         boolean part_agree = list.containsAll(containsAllList);
         if(part_agree){
             System.out.println(list "包含顺序一致的" containsAllList "数据:" part_agree);
         } else{
              System.out.println(list "不包含顺序一致的" containsAllList "数据:" part_agree);
              containsAllList.clear();
              System.out.println("调用clear()方法清空containsAllList数据:" containsAllList);
              containsAllList.add(addAll);
              containsAllList.add(add);
              boolean part_disagree = list.containsAll(containsAllList);
              if(part_disagree){
                  System.out.println(list "包含顺序不一致的" containsAllList "数据:" part_disagree);
              }
              else{
                  System.out.println(list "不包含顺序不一致的" containsAllList "数据:" part_disagree);
              }
         }
         containsAllList.clear();
         containsAllList.add(add addAll);
         boolean part_disagree = list.containsAll(containsAllList);
         if(!part_disagree) {
             System.out.println(list   "不包含"   containsAllList   "数据:"   part_disagree);
             containsAllList.add(add);
             System.out.println(list   "不包含"   containsAllList   "部分数据:"   part_disagree);
         }
     }

    @Test(groups = {"list"},priority = 4)
     public void removemethod(){
         String add = "addAll";
         List<String> newlist = list;
         int index = 1;
         System.out.println("remove()去掉列表中第一次出现的" add ",去掉" add "前:" newlist);
         newlist.remove(add);
         System.out.println("去掉" add "后:" newlist);
         System.out.println("去掉第" index "位数据前" list);
         list.remove(index);
         System.out.println("去掉第" index "位数据后" list);
     }

     @Test(groups = {"list"},priority = 5)
     public void removemeAllthod(){
        String param1 = "abb";
        String param2 = "add";
        List<String> removeList = new ArrayList<String>();
        List<String> newlist  = list;
        removeList.add(param1);
        removeList.add(param2);
        System.out.println(newlist "去掉" removeList "前:" newlist);
        newlist.removeAll(removeList);
        System.out.println(newlist "去掉" removeList "后" newlist);
     }

     /*todo remove get equals*/
    @Test(groups = {"list"},priority = 6)
    public void removeIfmethod(){
        list.add("removeif");
        list.add("张三");
        list.add("李四");
        System.out.println(list "调用removeif方法前:" list);
        boolean removeifjudge = list.removeIf(param ->param.equals("张三"));
        System.out.println(list "如果filer = true,调用removeif方法后:" removeifjudge "|" list);
        boolean removeifjudge2 = list.removeIf(param ->param.equals("张三"));
        System.out.println(list "如果filer = false,调用removeif方法后:" removeifjudge2 "|" list);
    }

    @Test(groups = {"list"},priority = 7)
    public void equalsmethod(){
        /*size
        *get
        * equals:两列表内容及顺序一致
        * */
        int index = list.size()-1;
        List<String> newlist = new ArrayList<String>();
        newlist.add("addAll");
        System.out.println(list "长度为:" list.size());
        String getparam = list.get(index);
        System.out.println(list "第" index "位:" getparam);
        boolean equalresult = list.equals(newlist);
        if(!equalresult){
            System.out.println(list "不等于:" newlist);
        }
        newlist.add("123");
        boolean equalresult2 = list.equals(newlist);
        if(!equalresult2){
            System.out.println(list "不等于:" newlist);
        }
        newlist.remove("123");
        newlist.add("removeif");
        newlist.add("李四");
        boolean equalresult3 = list.equals(newlist);
        if(equalresult3){
            System.out.println(list "等于:" newlist);
        }
        newlist.remove("removeif");
        newlist.remove("李四");
        newlist.add("李四");
        newlist.add("removeif");
        boolean equalresult4 = list.equals(newlist);
        if(!equalresult4){
            System.out.println(list "不等于:" newlist);
        }
    }

    @Test(groups = {"list"},priority = 8)
    public void emptymethod(){
        boolean emptyjudge2 = list.isEmpty();
        System.out.println("Empty方法" list "非空" emptyjudge2);
        list.clear();
        boolean emptyjudge = list.isEmpty();
        System.out.println("Empty方法" list "为空" emptyjudge);
    }

    @Test(groups = {"list"},priority = 9)
    public void sortmethod(){
        list.add("b");
        list.add("c");
        list.add("a");
        list.add("3");
        list.add("1");
        list.add("2");
        System.out.println("sort前:" list);
        list.sort(null);
        System.out.println("sort后:" list);
    }

    @Test(dependsOnMethods = {"dependsOnGroupsMethod"})
    public void dependsOnMethodsMethod(){
        System.out.println("强依赖2fangfa");
    }

    @Test(dependsOnGroups = {"list"})
    public void dependsOnGroupsMethod(){
        System.out.println("强依赖");
    }


}

     

拓展:

filter流

0 人点赞