Java基础知识学习——集合一

知识点汇总一

集合的概述:集合、数组都是对多个数据进行存储操作的结构简称java容器
Java集合可分为Collection和Map两种体系:
Collection接口 单列数据存取一组对象的方法的集合:
一:List元素有序可重复的集合存储有序的可重复的数据
ArrayList,LinkedList,Vector
二:Set元素无序不可重复的集合存储无序的不可重复的数据
HashSet,LinkedHashSet,TreeSet
集合元素的遍历操作使用Iterator接口
注意:向Collection接口的实现类的对象中添加数据obj时,需要obj所在类重写equals()方法。
Map接口双列数据具有映射关系“key-value”对的集合
HashMap,LinkedHashMap,TreeMap,HashTable,Properties
代码示例:

public class CollectionTest {
    public static void main(String[] args) {
        /*
        * Collection集合中的常用方法
        * */
        Collection collection=new ArrayList();
        //add(object e)
        collection.add("qw");
        collection.add(121);
        collection.add(new Date());
        //获取元素的个数
        System.out.println(collection.size());
        //addAll(Collection coll)
        Collection collection1=new ArrayList();
        collection1.add(99);
        collection1.add("hi");
        collection.addAll(collection1);
        System.out.println(collection.size());
        //clear()清空当前集合
        collection1.clear();
        System.out.println(collection1.isEmpty());
        //isEmpty()判断当前集合是否为空
        System.out.println(collection1.isEmpty());
        //Contains(Object obj)判断当前集合是否包含obj
        collection.contains(123);
        //ContainsAll(Collection coll)判断形参coll中的所有元素是否都在当前集合中
        System.out.println(collection.containsAll(collection1));
        //remove(Object obj)移除原有数据
        collection.remove(121);
        //remove(Collection coll)移除集合coll中的所有元素
        //retainAll(Collection coll)求交集
        System.out.println(collection.retainAll(collection1));
        //equals(Object obj)判断是否相同且有序
        //hashCode返回当前对象的hash值
        System.out.println(collection.hashCode());
        //集合——》数组toArray()
        Object[] ar=collection.toArray();
        for (int i = 0; i < ar.length; i++) {
            System.out.println(ar[i]);
        }
        //数组-》集合
        List<String> list=Arrays.asList(new String[]{"qq","hh"});
        List list1=Arrays.asList(new Integer[]{777,666});
        System.out.println(list1);
        System.out.println("***************************");
        //iterator():返回iterator接口的实例用于遍历集合元素
        Collection collection2=new ArrayList();
        collection2.add("asdf");
        collection2.add(567);
            Iterator iterator=collection2.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }
            //iterator中的remove()方法
        collection2.add("sky");
            Iterator iterator1=collection2.iterator();
            while (iterator1.hasNext()){
                Object obj=iterator1.next();
                if("sky"==obj){
                    iterator1.remove();
                }
            }
            Iterator iterator2=collection2.iterator();
            while (iterator2.hasNext()){
                System.out.println(iterator2.next());
            }
        //jdk 5.0 新增了foreach循环用来遍历数组,集合
        //格式:for(集合元素类型   局部变量 : 集合对象)
        for(Object coll : collection2){
            System.out.println(coll);
        }

    }
}

知识点汇总二

1.List接口:存储有序的、可重复的数据。
ArrayList线程不安全,效率高、底层使用Object[] elementData数据。
LinkedList底层使用双向链表对于频繁的插入删除操作使用此类效率较高。
Vector线程安全效率低,底层使用Object[] elementData数据。
2.Set接口:存储无序的,不可重复的数据。
HashSet:作为Set的主要实现类线程不安全的可以存储null值。
LinkedHashSet:作为HashSet的子类。
TreeSet:可按照指定属性进行排序。
注意:向Set中添加的数据所在类一定要重写hashCode()方法和equals()方法。
代码示例:

public class CollectionSub {
    public static void main(String[] args) {
   //List接口中的常用方法
        ArrayList arrayList = new ArrayList();
        arrayList.add(12);
        arrayList.add(45);
        arrayList.add("He");
        arrayList.add("She");
        arrayList.add("who");
        //add()方法
        arrayList.add(2, "insert");
        List list = Arrays.asList(4, 5, 6);
        arrayList.addAll(3, list);
        //get()方法
        System.out.println(arrayList.get(3));
        //indexOf()返回首次出现的位置
        //lastIndexOf()最后一次出现的位置
        System.out.println(arrayList.indexOf(45));
        //remove()移除指定元素
        System.out.println(arrayList.remove(0));
        //subList()返回指定位置区间的子串
        System.out.println(arrayList.subList(3, 6));
        //set()修改指定位置的元素
        arrayList.set(2, "change");
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("************************");
        for (Object obj : arrayList) {
            System.out.println(obj);
        }
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("****************Set接口***********************");
        Set set = new HashSet();
        set.add(22);
        set.add(11);
        set.add("hello");
        set.add("we");
        Iterator iterator1 = set.iterator();
        while (iterator1.hasNext()) {
            System.out.println(iterator1.next());
        }
        //LinkedHashSet
        Set set1 = new LinkedHashSet();
        set1.add(22);
        set1.add(11);
        set1.add("hello");
        set1.add("we");
        Iterator iterator2 = set1.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }
        /*
         * TreeSet:向其中添加数据要求是相同类的对象
         * 两种排序方式:自然排序(实现Comparable接口)和定制排序(实现Comparator)
         * */
        //自然排序
        TreeSet treeSet = new TreeSet();
        treeSet.add(23);
        treeSet.add(-11);
        treeSet.add(33);
        treeSet.add(423);
        //定制排序
        Comparator comparator=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return 0;
            }
        };
        TreeSet treeSet1=new TreeSet(comparator);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值