zoukankan      html  css  js  c++  java
  • Java 基础(集合一)

    一、集合框架的概述

    1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
     说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
    
    2.1 数组在存储多个数据方面的特点:
         > 一旦初始化以后,其长度就确定了。
         > 数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。
          比如:String[] arr;int[] arr1;Object[] arr2;
    2.2 数组在存储多个数据方面的缺点:
         > 一旦初始化以后,其长度就不可修改。
         > 数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高。
         > 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
         > 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
    

    二、集合框架

         |----Collection接口:单列集合,用来存储一个一个的对象
             |----List接口:存储有序的、可重复的数据。  -->“动态”数组
                 |----ArrayList:作为List接口的主要实现类:线程不安全的,效率高;底层使用Object[] elementData存储
                 |----LinkedList:对于频繁的插入,删除操作,使用此类效率比ArrayList高;底层使用双向链表存储
                 |----Vector:作为List接口的古老实现类;线程安全的,效率低;底层使用Object[]存储
    
             |----Set接口:存储无序的、不可重复的数据   -->高中讲的“集合”
                 |----HashSet:作为Set接口的主要实现类;线程不安全的;可以存储null值
                 |----LinkedHashSet:作为HashSet的子类;遍历其内部数据时,可以按照添加的顺序对于频繁的遍历操作,LinkedHashset效率高于HashSet。
                 |----TreeSet:可以按照添加对象的指定属性,进行排序。
    
         |----Map接口:双列集合,用来存储一对(key - value)一对的数据   -->高中函数:y = f(x)
                 |----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
    

    CollectionTest.java

    package com.klvchen.java2;
    
    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Date;
    
    public class CollectionTest {
    
        @Test
        public void test1(){
            Collection col1 = new ArrayList();
    
            // add(Object e): 将元素e添加到集合 col1中
            col1.add("AA");
            col1.add("BB");
            col1.add(123);
            col1.add(new Date());
    
            //size(): 获取添加的元素的个数
            System.out.println(col1.size());
    
            //addAll();
            Collection coll1 = new ArrayList();
            coll1.add(456);
            coll1.add("CC");
            col1.addAll(coll1);
    
            System.out.println(col1.size());
            System.out.println(col1);
    
            //clear(): 清空集合元素
            col1.clear();
    
            //isEmpty():判断当前集合是否为空
            System.out.println(col1.isEmpty());
        }
    }
    

    CollectionTest1.java

    package com.klvchen.java2;
    
    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    
    public class CollectionTest1 {
    
        @Test
        public void test1(){
            Collection col1 = new ArrayList();
            col1.add(123);
            col1.add(456);
            col1.add(new String("Tom"));
            col1.add(false);
            col1.add(new Person("Jerry", 20));
    //        Person p = new Person("jerry", 20);
    //        col1.add(p);
    
    
            //1.contains(Object obj):判断当前集合中是否包含obj
            //我们会在判断时调用obj对象所在类的equals()。
            boolean contains = col1.contains(123);
            System.out.println(contains); //true
            System.out.println(col1.contains(new String("Tom"))); //true
    //        System.out.println(col1.contains(p)); //true
            System.out.println(col1.contains(new Person("Jerry",20)));
    
            //2.containsAll(Collection coll1):判断形参coll1中的所有元素释放都存在于当前集合中
            Collection coll1 = Arrays.asList(123, 456);
            System.out.println(col1.containsAll(coll1));
        }
    
        @Test
        public void test2(){
            //3.remove(Object obj): 从当前集合中移除obj元素
            Collection col1 = new ArrayList();
            col1.add(123);
            col1.add(456);
            col1.add(new Person("Jerry", 20));
            col1.add(new String("Tom"));
            col1.add(false);
    
            col1.remove(1234);
            System.out.println(col1);
    
            col1.remove(new Person("Jerry", 20));
            System.out.println(col1);
    
        }
    
        @Test
        public void test3(){
            Collection col1 = new ArrayList();
            col1.add(123);
            col1.add(456);
            col1.add(new Person("Jerry", 20));
            col1.add(new String("Tom"));
            col1.add(false);
    
            //5.retainAll(Collection coll1): 交集:获取当前集合和coll1集合的交集,并返回当前集合
    //        Collection coll1 = Arrays.asList(123, 456, 789);
    //        col1.retainAll(coll1);
    //        System.out.println(col1);
    
            //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同,顺序也要一样。
            Collection coll1 = new ArrayList();
            coll1.add(123);
            coll1.add(456);
    
            coll1.add(new Person("Jerry", 20));
            coll1.add(new String("Tom"));
            coll1.add(false);
    
            System.out.println(col1.equals(coll1));
    
        }
    
        @Test
        public void test4(){
            Collection col1 = new ArrayList();
            col1.add(123);
            col1.add(456);
            col1.add(new Person("Jerry", 20));
            col1.add(new String("Tom"));
            col1.add(false);
    
            //7.hashCode():
            System.out.println(col1.hashCode());
    
            //8.集合 --> 数组 toArray()
            Object[] arr = col1.toArray();
            for (int i = 0; i < arr.length; i++){
                System.out.println(arr[i]);
            }
    
            //数组 --> 集合: 调用Arrays类的静态方法 asList()
            List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
            System.out.println(list);
    
            List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
            System.out.println(arr1);
            System.out.println(arr1.size());
    
            List arr2 = Arrays.asList(123, 456);
            System.out.println(arr2);
    
            List arr3 = Arrays.asList(new Integer[]{123, 456});
            System.out.println(arr3.size());
    
    
        }
    
    }
    
  • 相关阅读:
    【Leetcode_easy】720. Longest Word in Dictionary
    【Leetcode_easy】717. 1-bit and 2-bit Characters
    【Leetcode_easy】709. To Lower Case
    【Leetcode_easy】707. Design Linked List
    【Leetcode_easy】706. Design HashMap
    第38课 栈和队列的相互转化
    第7章 网络层协议(4)_IGMP协议
    第7章 网络层协议(3)_ARP协议
    第33课 双向循环链表的实现
    第32课 Linux内核链表剖析
  • 原文地址:https://www.cnblogs.com/klvchen/p/15306280.html
Copyright © 2011-2022 走看看