Collection_List_Set_Map

集合:Java中提供的一种容器,可以用来存储多个数据
     集合存储的数据类型都是对象,集合定义的元素类型都是引用类型
     集合容器的长度是可变的

     Collection集合类
     特点:1.它是一个接口,使用时需要借助它的子类
     public interface Collection<E> extends Iterable<E>
     2.Iterable 继承了迭代器  具有迭代的功能
     3.Collection<E>  当子类在使用的时候需要指定类型
     4.集合层次结构中的根界面 。  collection是集合中的根接口
     5.集合表示一组被称为其元素的对象。  collection集合存储存储的数据是一组对象
     6.一些集合允许重复元素,而其他集合不允许。 collection集合的子类可以存储重复的元素也可以存储不重复的元素
     7.有些被命令和其他无序。 collection集合的子类有的是有序的,如List,有的是无序的如Set
     8.JDK不提供此接口的任何直接实现,提供了更具体的子接口的实现,如Set和List 。

 

 

List 和 Set 都是collection 集合的子接口,数据结构都是单列的
List 集合 元素有序,有索引,元素可重复 ArrayList LinkedList
Set 集合 元素无序,无索引,元素不可重复 HashSet LinkedHashSet TreeSet
collections 集合工具类 可以实现排序 shuffle()打乱元素

 

public class CollectionDemo {

    public static void main(String[] args) {
        test02();
    }

    public static void test02(){
        //collection集合(有序的)  添加add() 删除remove() 为空校验isEmpty() 清空集合clear() 元素包含判断contains("庄庄")  获取集合长度size()  集合转成数组toArray()  取出元素和修改元素不行
        Collection<String> col = new ArrayList<>();
        //添加
        col.add("张张");
        col.add("庄庄");
        col.add("周周");
        //删除集合中的元素 remove("张张")
        boolean b = col.remove("张张");
        System.out.println(b);
        System.out.println(col);//[庄庄, 周周]
        //获取集合容器长度大小 size()
        int size = col.size();
        System.out.println(size);//2

        //集合类型转成数组类型  toArray()
        Object[] objects = col.toArray();

        for (Object object : objects) {
            System.out.println(object);
        }
        //是否包含 contains("庄庄")
        boolean b2 = col.contains("庄庄");//true
        System.out.println(b2);
        //清空所有元素   removeAll()和clear()
//        col.removeAll(col);
//        System.out.println(col);
        col.clear();
        System.out.println(col);
        //是否为空集合 isEmpty()
        boolean empty = col.isEmpty();
        System.out.println(empty);//true

        col.add("张张");
        col.add("庄庄");
        col.add("周周");

        //增强for循环  (循环元素的数据类型 循环容器得到的元素  :  循环遍历的目标容器对象)
        //不允许在遍历中修改元素
        for (String s : col) {
//            col.remove("庄庄"); java.util.ConcurrentModificationException  并发修改异常
            System.out.println(s);
        }
    }


    public static void test01(){
        Collection<String> col = new ArrayList<>();
        //添加
        col.add("张张");
        col.add("庄庄");
        col.add("周周");

        //遍历集合  利用迭代器  Iterator<> iterator
        //Iterator<>  提供了两个方法  hasNext()-->判断集合是否有下一个元素   next获取集合中的下一个元素
        Iterator<String> iterator = col.iterator();
        for (int i = 0; i < col.size(); i++) {
            System.out.println(iterator.next());
        }
        //while 循环遍历  hasNext()-->判断集合是否有下一个元素   next获取集合中的下一个元素
        while (iterator.hasNext()){
            System.out.println( iterator.next());
        }
    }
}

 

list 接口    实现:ArrayList
LinkedList 链表集合 内部是一个链表结构 特点:增删快,查询慢(从首节点开始查)
双链表,有序的
LinkedList 常用api
public static void test01(){
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("1");
        linkedList.add("2");
        linkedList.add("1");
        //首位添加元素
        linkedList.addFirst("3");//[3, 1, 2, 1]
        //末尾添加元素
        linkedList.addLast("10");//[3, 1, 2, 1, 10]
        //指定位置添加元素
        linkedList.add(2, "5");//[3, 1, 5, 2, 1, 10]
        System.out.println(linkedList);

        //获取下标为3的元素
        String s = linkedList.get(3);//2
        System.out.println(s);
        //检索第一个元素并返回
        String peek = linkedList.peek();//3
        System.out.println(peek);
        //删除第一个元素并且返回该元素的值
        String poll = linkedList.poll();//3
        System.out.println(poll);
        //删除指定位置的元素
        linkedList.remove(2);
        System.out.println(linkedList);//[1, 5, 1, 10]
        //修改元素
        linkedList.set(2, "9");//[1, 5, 9, 10]
        System.out.println(linkedList);
    }

 


ArrayList
public static void test02(){
        //元素可重复,有索引有下标、通过数组的方式实现的
        ArrayList<String> list = new ArrayList<>();
        list.add("张张");
        list.add("庄庄");
        list.add("周周");
        list.add("庄庄");
        list.add("周周");
        list.add("庄庄");
        list.add("周周");
        //是否包含
        boolean b = list.contains("庄庄");//true
        System.out.println(b);
        //返回第一次出现的索引
        int index = list.indexOf("庄庄");//1
        System.out.println(index);
    }

 


Set 接口中元素无序,并且都会以某种规则保证存入的元素不出现重复。 无索引
HashSet 所存储的元素是不可重复的,并且元素都是无序的(即存取顺序不一致)。
是根据对象的哈希值来确定元素在集合中的存储位置,因此具有良好的存取和查找性能。
保证元素唯一性的方式依赖于: hashCode 与 equals 方法。
方法与collection类似。

LinkedHashSet 哈希表和链表实现了Set接口,具有可预测的迭代次序。 这种实现不同于HashSet,它维持于所有条目的运行双向链表。
该链表定义了迭代排序
该类提供了所有可选的 set操作,并允许null元素
存储元素不允许重复
 public static void test02(){
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("张张");
        set.add("庄庄");
        set.add("张张");
        set.add("张张");
        set.add("周周");
        //添加元素不可重复
        System.out.println(set);//[张张, 庄庄, 周周]
        //迭代
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

 

 
可变参数 当声明的时候,对于参数的类型是一致的,但是其参数的个数是不确定的,可以选择使用可变参数  和数组类似
public static void test03(String ...player){
        for (String s : player) {
            System.out.println(s);
        }
    }

collections 集合工具类 shuffle()打乱顺序 sort()排序

public static void test04(){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(1);
        list.add(22);
        list.add(82);
        list.add(122);
        list.add(172);
        list.add(128);
        list.add(102);
        System.out.println(list);//[12, 1, 22, 82, 122, 172, 128, 102]
        //集合升序排列
        Collections.sort(list);
        System.out.println(list);//[1, 12, 22, 82, 102, 122, 128, 172]
        //元素顺序颠倒
        Collections.reverse(list);
        System.out.println(list);//[172, 128, 122, 102, 82, 22, 12, 1]
        //集合随机打乱
        Collections.shuffle(list);
        System.out.println(list);//[128, 102, 12, 82, 122, 22, 172, 1]
        //按照降序  public static <T> void sort(List<T> list,Comparator<? super T> )
        //采用匿名内部类的方式实现定义顺序规则  new 抽象类/接口
        Collections.sort(list, new Comparator<Integer>() {
            //指定排序规则
            @Override
            public int compare(Integer num1, Integer num2) {
                //num1-num2    num2-num1
                // 两个对象比较的结果有三种:大于,等于,小于。 如果要按照升序排序, 则o1 小于o2,返回(负数),相等返回0,01大于02返回(正数) 如果要按照 降序排序 则o1 小于o2,返回(正数),相等返回0,01大于02返回(负数)
                return num2-num1;
            }
        });
        System.out.println(list);//[172, 128, 122, 102, 82, 22, 12, 1]

    }

 




map集合是一种键值对形式存储数据的引用类型,key值不能重复
value可以重复 无下标 无索引 从map集合取值,通过键可以找对
所对应的值。

public class MapDemo {

    public static  void test01(){
        Map<String, String> map = new HashMap<>();
        //增加 插入
        map.put("1", "张张");
        map.put("2", "庄庄");
        map.put("3", "周周");
        map.put("4", "朱朱");
        System.out.println(map);//{1=张张, 2=庄庄, 3=周周, 4=朱朱}
        //
        System.out.println(map.get("1"));//张张
        //通过key值删除value  remove(key)
        String remove = map.remove("4");//朱朱
        System.out.println(remove);
        System.out.println(map);//{1=张张, 2=庄庄, 3=周周}
        //修改  返回的都是被替换的内容
        String str = map.replace("3", "丽丽");//{1=张张, 2=庄庄, 3=周周}
        System.out.println(str);//周周
        System.out.println(map);
        map.put("2", "朱朱");//{1=张张, 2=朱朱, 3=丽丽}
        System.out.println(map);

        //是否包含key值
        boolean b = map.containsKey("2");
        System.out.println(b);//true
        //是否包含value值
        boolean b1 = map.containsValue("朱朱");
        System.out.println(b1);//true
        //增强for循环遍历
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println(key+":"+map.get(key));//1:张张  2:朱朱  3:丽丽
        }

        //迭代器遍历
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next+map.get(next)); //1张张 2朱朱 3丽丽
        }

        //在Map接口里面有一个内部接口, Entry<key,value>
        //直接拿key值和value值
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.print(key+":"+value+"\t");//1:张张    2:朱朱    3:丽丽
        }
    }

 





 


posted @ 2020-07-04 21:04  怎漾  阅读(169)  评论(0)    收藏  举报