java学习--集合(一)

Collection接口中的常用方法:

Collection接口:单列集合,用来存储一个一个的对象
向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()
1.contains(Object obj):集合中是否含有指定元素
2.containsAll(Collection col):当前集合中是否含有col中的所有元素
3.remove(Object obj):从当前集合中删除obj元素,移除成功返回true,移除失败返回false
4.removeAll(Collection col):从当前集合中移除col中的所有元素
5.retainAll(Collection col):获取当前集合与col的交集
6.equals(Object obj):判断当前集合与形参中的集合是否相同,要想返回true,需要当前集合和形参的元素都相同(集合里元素的顺序和内容都相同)
7.hashCode():返回当前对象的哈希值
8.集合—>数组:toArray()
9.数组—>集合:调用Arrays类的静态方法asList()

 		Collection col1=new ArrayList();
        col1.add(123);
        col1.add(456);
        col1.add(false);
        col1.add("dfg");
        //数组--->集合
        Object[] obj = col1.toArray();
        for (int i = 0; i < obj.length; i++) {
            System.out.println(obj[i]);
        }
        //集合--->数组
        List<String> strings = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(strings);

使用Iterator遍历Collection

1.内部方法:hasNext()和next()
2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标在集合的第一个元素之前
3.内部定义了remove(),可以在遍历的时候删除集合中的元素,注意这里的remove()与集合的remove()不同
4.如果还未调用next()或在上一次调用next方法之后已经调用了remove方法,再调用remove都会报IllegalStateException错误。

 		System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        //四个元素只能有四个next(),第五个会报错:NoSuchElementException
        //System.out.println(iterator.next());

推荐方式:

		Iterator iterator = col1.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

Iteraror里的remove()方法:

		Collection col1=new ArrayList();
        col1.add(123);
        col1.add(456);
        col1.add(false);
        col1.add("dfg");

        Iterator iterator = col1.iterator();
        //删除dfg
        while(iterator.hasNext()){
            Object next = iterator.next();
            if ("dfg".equals(next)) {

                iterator.remove();
            }

        }

        //遍历
        iterator=col1.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

使用Foreach来遍历Collection:

		Collection col1=new ArrayList();
		col1.add(123);
        col1.add(456);
        col1.add(false);
        col1.add("dfg");
        //for(集合中的元素类型 局部变量i:集合对象)
        //集合的遍历
        for (Object i : col1) {
            System.out.println(i);
        } 

        //数组的遍历
        int[] arr=new int[]{1,2,3,4,5};
        for (int i : arr) {
            System.out.println(i);
        }

注意:

  		int[] arr1=new int[]{1,2,3,4,5};
        for (int i : arr1) {
            i=8;//这里只是把值给了i,并没有改变arr1里的元素
        }
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
        //结果为 1,2,3,4,5

Collection子接口之一:List接口

list接口的实现类常用的有第三个:
同:都实现了list接口,存储数据的特点相同:存储有序的、可重复的数据
ArrayList:作为list接口的主要实现类,线程不安全,效率高,底层使用Object[] elementData存储
LinkedList:
Vector:作为list接口的古老实现类,线程安全,效率低,底层使用Object[] elementData存储
LinkedList: 对于频繁的插入、删除操作,使用此类效率比Arraylist高,底层使用双向链表存储

list接口中常用的方法:
1.void add(int index,Object ele):在index位置插入ele元素
2.boolean addAll(int index,Collection eles):从index位置开始将eles中所有元素添加进来
3.Object get(int index):获取指定index位置的元素
4.int indexOf(Object obj):返回obj在当前集合中首次出现的位置
5.int lastIndexOf(Object obj):返回Obj在当前集合中末次出现的位置
6.Object remove(int index):移除指定index位置的元素,并返回此元素
7.Object set(int index,Object ele):设置指定index位置的元素ele
8.List subList(int fromIndex,int toIndex):返回从fromIndex到toIndex位置的左闭右开子集合。

总结:
增:add(Object obj)
删:remove(int index)/remove(Object obj)(collection中自带的)
改:set(int index,Object ele)
查:get(int index)
插:add(int index,Object ele)
长度:size
遍历:1.iterator迭代器方式 2.增强for循环(foreach) 3.普通for循环

 		Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        
        for(Object obj:list){
            System.out.println(obj);
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

Collection子接口之二:Set接口

Set接口:存储无序的、不可重复的数据
无序性:不等于随机性,存储的数据在底层数组中并非按照数组索引的顺序添加,而是根据数据的哈希值。
不可重复性:保证添加的元素按照aquals()判断是,不能返回true,即相同元素只能添加一个
Set接口常用的实现类有2个:
HashSet: 作为Set接口的主要实现类,线程不安全的,可以存储null值
LinkedHashSet:作为HashSet的子类,遍历其内部数据时,可以按照添加的顺序遍历
TreeSet:可以按照添加对象的制定属性进行排序
TreeSet中:
1.向TreeSet中添加的数据,要求是相同类的对象。
2.两种排序方式:自然排序(实现Comparable接口) 和 定制排序(Comparator)
3.自然排序中,比较两个对象是否相同的标准为:compareto()返回0,不再是equals()
4.定制排序,比较两个对象是否相同的标准为compare()返回0,不再是equals()

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值