JAVA集合框架

List接口使用:
ArrayList部分:

package list;

import java.util.ArrayList;

public class ArrayListDemo01 {
    public static void main(String[] args) {


        String[]array01 = new String[10];
        array01[0] = "zhangsan";
        array01[1] = "list";
        array01[2] = "wangwu";

        ArrayList list01 = new ArrayList<>();    //数组列表
        //1  增加
    list01.add("zhangsan");
    list01.add("list");
    list01.add("wangwu");
        System.out.println(list01);
        //2  删除
    list01.remove(1);
        System.out.println(list01);
        //3 修改
        list01.set(1,"xiaowu");
        System.out.println(list01);
        //4 查询
        for (Object l : list01){
            System.out.println(l + "\t");

        }
        //5 求长度
        System.out.println(list01.size());
        //6  清空
        list01.clear();
        System.out.println(list01.size());
        System.out.println(list01.isEmpty());


        //7 addAll
        ArrayList list02 = new ArrayList<>();
        list02.add(111);
        list02.add(222);
        list01.addAll(1, list02);
        System.out.println(list01);



    }


}

 
 

LinkedList部分:

package list;

import java.util.ArrayList;
import java.util.LinkedList;

public class LinkedListDEmo02 {

        public static void main(String[] args) {


            String[]array01 = new String[10];
            array01[0] = "zhangsan";
            array01[1] = "list";
            array01[2] = "wangwu";

            LinkedList list01 = new LinkedList<>();    //链表的方式实现
            //1  增加
            list01.add("zhangsan");
            list01.add("list");
            list01.add("wangwu");
            System.out.println(list01);
            //2  删除
            list01.remove(1);
            System.out.println(list01);
            //3 修改
            list01.set(1,"xiaowu");
            System.out.println(list01);
            //4 查询
            for (Object l : list01){
                System.out.println(l + "\t");

            }
            //5 求长度
            System.out.println(list01.size());
            //6  清空
       /* list01.clear();
        System.out.println(list01.size());
        System.out.println(list01.isEmpty());*/


            //7 addAll
     LinkedList list02 = new LinkedList<>();
            list02.add(111);
            list02.add(222);
            list01.addAll(1, list02);
            System.out.println(list01);


    list01.addLast(111);
    list01.addFirst(222);
        }


    }


 
 

set接口:
HashSet部分:

package set;

import java.util.HashSet;

public class HashSetDemo {
    public static void main(String[] args) {

            HashSet set =  new HashSet<>();   //钻石语法
            set.add("111");
            set.add("222");
            set.add("333");
            set.add("小明");
             set.add("小花");
            set.add("aaa");
            set.add("ccc");
            set.add("ddd");
        System.out.println(set);
        set.remove("ccc");//根据内容移除
        System.out.println(set);
        int  size = set.size();
        boolean empty = set.isEmpty();
        System.out.println(size);
        System.out.println(empty);
        set.clear();    //清除
        System.out.println(set);
    }
}

Treeset部分:

package set;

import java.util.TreeSet;

public class TreeSetDEmo {
    public static void main(String[] args) {

        TreeSetset = new TreeSet<>();
        set.add("111");
        set.add("222");
        set.add("胡来");
        set.add("地理");
        set.add("yyds");
        set.add("ssee");
        System.out.println(set);
    }
}

Map接口:
HashMap部分:

package map;

import java.util.HashMap;
import java.util.Map;

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put("henan","河南");
        map.put("hebei","河北");
        map.put("hubei","湖北");
        map.put("henan","河南2");
        map.put("henan","河南1");
       /* map.put("null","空");
        map.put(null,"空");*/
        System.out.println(map);

        for (Map.Entry m : map.entrySet()){
            System.out.println(m);
        }
        for ( String  k: map.keySet()){
            System.out.println(k +"=" +map.get(k));
        }
        for (String v:map.values()){
            System.out.println(v);
        }
    }
}

TreeMap部分:

package map;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class TreemapDemo { public static void main(String[] args) {
    TreeMap map = new TreeMap<>();
    map.put("henan","河南");          //实例 entry
    map.put("hebei","河北");
    map.put("hubei","湖北");
    map.put("henan","河南2");
    map.put("henan","河南1");         //键重复会覆盖掉所有的值
       /* map.put("null","空");
        map.put(null,"空");*/
    System.out.println(map);

    for (Map.Entry m : map.entrySet()){
        System.out.println(m);
    }
    for ( String  k: map.keySet()){
        System.out.println(k +"=" +map.get(k));
    }
    for (String v:map.values()){
        System.out.println(v);
    }
}
}

tools:
Demo01:

package tools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Demo01 {
    public static void main(String[] args) {
        String str[]= {"中文","计算机","ABC","123","[email protected]"};
        Listlist = Arrays.asList(str);// 1 Arrays.asList()将数组转换成列表
        System.out.println(list);

        //2 将数组转换成数组列表(链式列表)  new ArrayList<>(Arrays.asList(str))
        ArrayList alist = new ArrayList<>(Arrays.asList(str));
        LinkedList llist = new LinkedList<>(Arrays.asList(str));
        alist.remove("ABC");
        llist.add("1321123");
        System.out.println(alist);
        System.out.println(llist);

        //3 将列表转换成数组  列表.toArray();
        Object[] str2 = alist.toArray();
        for (Object o : str2){
            System.out.print(o + " ");
        }
        System.out.println();
        Object[]srt3 = llist.toArray();
        for (Object o : srt3){
            System.out.print(o + " ");
        }
    }
}

你可能感兴趣的:(JAVA集合框架)