JAVA集合框架概述

集合框架涉及到的api



List接口是继承Collection接口,Set接口是继承Collection接口,

ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。

ArrayList 继承了 AbstractList ,并实现了 List 接口。

Collection接口中的常用方法

Collection接口中的常用方法代码测试1:CollectionTest

package com.atguigu.java2;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;/*** 一、集合框架的概述** 1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。*  说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)** 2.1 数组在存储多个数据方面的特点:*      > 一旦初始化以后,其长度就确定了。*      > 数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。*       比如:String[] arr;int[] arr1;Object[] arr2;* 2.2 数组在存储多个数据方面的缺点:*      > 一旦初始化以后,其长度就不可修改。*      > 数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高。*      > 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用*      > 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。** 二、集合框架*      |----Collection接口:单列集合,用来存储一个一个的对象*          |----List接口:存储有序的、可重复的数据。  -->“动态”数组(不用考虑长度越不越界,超不超的问题)*              |----ArrayList、LinkedList、Vector**          |----Set接口:存储无序的、不可重复的数据   -->高中讲的“集合”*              |--Set接口三个主要的实现类--HashSet、LinkedHashSet、TreeSet**      |----Map接口:双列集合,用来存储一对(key - value)一对的数据   -->高中函数:y = f(x), value = f(key)*              |--Map接口几个实现类--HashMap、LinkedHashMap、TreeMap、Hashtable、Properties*** 三、Collection接口中的方法的使用**/
public class CollectionTest {@Testpublic void test1(){Collection coll = new ArrayList();//add(Object e):将元素e添加到集合coll中coll.add("AA");coll.add("BB");coll.add(123);//自动装箱coll.add(new Date());//size():获取添加的元素的个数System.out.println(coll.size());//4//addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中Collection coll1 = new ArrayList();coll1.add(456);coll1.add("CC");coll.addAll(coll1);System.out.println(coll.size());//6System.out.println(coll);//[AA, BB, 123, Sun Jan 30 23:17:11 CST 2022, 456, CC]//clear():清空集合元素coll.clear();//isEmpty():判断当前集合是否为空System.out.println(coll.isEmpty());//true //是空的}}

Collection接口中的常用方法代码测试2(下面的CollectionTest会用到 此Person类):

Person.java

package com.atguigu.java;import java.util.Objects;/*** CollectionTest会用到 此Person类*/
public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {System.out.println("调用了Person类的equals()方法");if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return age == person.age &&Objects.equals(name, person.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}
}

CollectionTest.java

package com.atguigu.java;import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;/*** Collection接口中声明的方法的测试** 结论:* 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().**/
public class CollectionTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);//自动装包coll.add(456);coll.add(new String("Tom"));coll.add(false);//     Person p=new Person("Jerry",20);
//     coll.add(p);
//     System.out.println(coll.contains(p));//truecoll.add(new Person("Jerry",20));//1.contains(Object obj):判断当前集合中是否包含obj//我们在判断时会调用obj对象所在类的equals()。boolean contains = coll.contains(123);System.out.println(contains);//trueSystem.out.println(coll.contains(new String("Tom")));//true 判断的是 是否有一样内容的 在里面,而不是比较地址System.out.println(coll.contains(new Person("Jerry",20)));//contains内调的是当前传入对象的equals()//false (Person类中默认没重写equals方法,而Object类的默认的equals方法是比较地址的 “=”) -->true(实际拿着这个contains的参数这个对象所在类的equals方法,// 即new Person("Jerry",20)去 点equals(),// 把集合中的数据依次放在equals的形参比较,一旦找到相同的就终止了,就不用往后去比了)//2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中,这里同上也是调用了 参数的equals()方法Collection coll1 = Arrays.asList(123,4567);//System.out.println(coll1);//[123, 4567]System.out.println(coll.containsAll(coll1));// false ,有一个数据不在coll中}@Testpublic void test2(){//3.remove(Object obj):从当前集合中移除obj元素。Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);coll.remove(1234);//返回值boolean类型,表示是否移除,如果存在且移除成功就返回true,不存在就返回falseSystem.out.println(coll);//1234不存在,所以没有删除。// [123, 456, Person{name='Jerry', age=20}, Tom, false]coll.remove(new Person("Jerry",20));System.out.println(coll);//因为重写了equals,所以这里移除了内容相同的元素。//[123, 456, Tom, false]//4. removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素,也就是移除共有的Collection coll1 = Arrays.asList(123,456);coll.removeAll(coll1);//只能移除共有的。System.out.println(coll);//[Tom, false]}@Testpublic void test3(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);System.out.println(coll);//[123, 456, Person{name='Jerry', age=20}, Tom, false]//6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。Collection coll2 = new ArrayList();coll2.add(456);coll2.add(123);coll2.add(new Person("Jerry",20));coll2.add(new String("Tom"));coll2.add(false);System.out.println(coll.equals(coll2));//false,因为ArrayList是有序的,coll是 123,456,而coll2是456,123,两个不是一个顺序,所以是false//5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合Collection coll1 = Arrays.asList(123,456,789);coll.retainAll(coll1);//求交集 ,然后修改coll这个集合System.out.println(coll);//[123, 456]}@Testpublic void test4(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);//7.hashCode():返回当前对象的哈希值,这个方法在Object中有,所以任何对象都能调用这个求hashcode 的方法System.out.println(coll.hashCode());//-1200490100//8.集合 --转换为->数组:toArray()//add()函数添加的是Object类型的元素,toArray()函数返回的是Object类型的数组Object[] arr = coll.toArray();for(int i = 0;i < arr.length;i++){System.out.println(arr[i]);//123//456//Person{name='Jerry', age=20}//Tom//false}//拓展:数组 --->集合:调用Arrays类的静态方法asList()//new String[]{"AA", "BB", "CC"}是一个String类型的数组,//List是一个“动态数组”,与数组对标,所以这里返回类型是ListList<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});System.out.println(list);//[AA, BB, CC]//new int[]{123, 456}是int型数组List arr1 = Arrays.asList(new int[]{123, 456});//new int[]{123, 456}这种写法会被asList()方法认为是一个元素System.out.println(arr1);//[[I@3d82c5f3] 这是一个 元素,这个元素是一维数组(由左边第二个[可知是一维数组,由左边第三个I知是int类型)的地址System.out.println(arr1.size());//1List arr2 = Arrays.asList(new Integer[]{123, 456});//new Integer[]{123, 456} 这种写法会被认为是多个元素System.out.println(arr2 );//[123, 456]System.out.println(arr2.size());//2//9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试(就是下面那个)}
}

IteratorTest.java

使用iterator()遍历Collection

在test1中演示

Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。

iterator是迭代器,是用来遍历的,它不装东西,coll是容器,iterator迭代的是容器coll中的数据。

Iterator遍历集合的两种错误写法

在test2中演示

Iterator迭代器remove()的使用

在test3中演示

IteratorTest.java代码

package com.atguigu.java;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;//迭代器Iterator用于迭代Collection,不用于迭代map
/*** 集合元素的遍历操作,使用 迭代器Iterator接口* 1.内部的方法:hasNext() 和  next()* 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,* 默认游标都在集合的第一个元素之前。* 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()**/
public class IteratorTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);Iterator iterator = coll.iterator();//方式一:System.out.println(iterator.next());//第1个元素 123System.out.println(iterator.next());//第2个元素 456System.out.println(iterator.next());//第3个元素 Person{name='Jerry', age=20}System.out.println(iterator.next());//第4个元素 TomSystem.out.println(iterator.next());//第5个元素 false//报异常:NoSuchElementException//System.out.println(iterator.next());//没有第六个元素,报异常//方式二:不推荐(没啥理由,感觉这个不好)
//        for(int i = 0;i < coll.size();i++){//            System.out.println(iterator.next());
//        }//方式三:推荐hasNext():判断是否还有下一个元素while(iterator.hasNext()){//next()方法实现 ①指针下移 ②将下移以后集合位置上的元素返回System.out.println(iterator.next());}}@Testpublic void test2(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);//错误方式一:
//        Iterator iterator = coll.iterator();
//        while((iterator.next()) != null){//这里判断下一个是不是空,下一个是123,非空,但是下一行,
//            System.out.println(iterator.next());//这此时指针指在123上,判断下一个是否为空,下一个是456不为空,指针指向456,输出456//判断了下一个 ,却输出了下下个
//        }//错误方式二://集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。while (coll.iterator().hasNext()){System.out.println(coll.iterator().next());//123//123//123一直输出123不停止}}//测试Iterator中的remove()//如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,// 再调用remove都会报IllegalStateException。@Testpublic void test3(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);//删除集合中"Tom"Iterator iterator = coll.iterator();while (iterator.hasNext()){//iterator.remove();在没调用next()之前调用remove()会报错Object obj = iterator.next();if("Tom".equals(obj)){iterator.remove();//iterator.remove();连着调用两次remove(),则在第二次报错}}//遍历集合iterator = coll.iterator();//重新指向开头的前面while (iterator.hasNext()){System.out.println(iterator.next());//123//456//Person{name='Jerry', age=20}//false 已移除Tom}}
}
//迭代器Iterator用于迭代Collection,不用于迭代map

新特性foreach 循环遍历集合或项目

package com.atguigu.java;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;/*** jdk 5.0 新增了foreach循环,用于遍历集合、数组**/
public class ForTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new Person("Jerry",20));coll.add(new String("Tom"));coll.add(false);//for(集合元素的类型 局部变量 : 集合对象)//内部仍然调用了迭代器。for(Object obj : coll){//原理,取coll中的元素赋给obj,打印obj,取第二个元素给obj,打印obj,,,一直取到最后一个元素。System.out.println(obj);//123//456//Person{name='Jerry', age=20}//Tom//false}}@Testpublic void test2(){int[] arr = new int[]{1,2,3,4,5,6};//for(数组元素的类型 局部变量 : 数组对象)for(int i : arr){System.out.println(i);}//1//2//3//4//5//6}//练习题@Testpublic void test3(){String[] arr = new String[]{"MM","MM","MM"};//        //方式一:普通for赋值
//        for(int i = 0;i < arr.length;i++){//            arr[i] = "GG";
//        }//方式二:增强for循环for(String s : arr){//把arr中的取出来给s,然后给s赋值GG,s是新变量,arr不变s = "GG";}for(int i = 0;i < arr.length;i++){System.out.println(arr[i]);//MM//MM//MM}}
}

JAVA集合1(Collection接口,iterator()方法,增强型for循环)相关推荐

  1. 49天精通Java,第23天,Java集合,Collection接口,Iterator接口

    目录 一.Collection接口 二.Iterator接口 1.Iterator接口方法 2.遍历 3.forEach 4.遍历元素的顺序取决于集合类型 三.Java集合 四.分布式中间件核心原理与 ...

  2. Java集合:Collection接口

    Collection是一个接口,继承自Iterable.我们先看一下Iterable接口的源码 一.Iterable package java.lang;import java.util.Iterat ...

  3. java学习(尚硅谷)集合之Collection接口中的方法

    又是好几天没有学Java了,今天下午睡了个觉状态很好,来到图书馆写了点C++的项目代码,写累了看了几集康师傅.笔记如下: Collection接口中的方法: package com.atguigu.j ...

  4. java 集合 接口_Java集合之Collection接口

    1 - Java集合介绍 /* 1. 一方面, 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象 的操作,就要对对象进行存储. 2. 另一方面,使用Array存储对象方面具有一些弊 端,而 ...

  5. java list接口为何要重新声明collection接口的方法_JAVA Collection接口中List Map 和Set的区别(转)...

    Java中的集合包括三大类,它们是Set(集).List(列表)和Map(映射),它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类.Set的实现类主要有HashS ...

  6. java集合框架的接口_Java集合框架之Collection接口详解

    Java是一门面向对象的语言,那么我们写程序的时候最经常操作的便是对象了,为此,Java提供了一些专门用来处理对象的类库,这些类库的集合我们称之为集合框架.Java集合工具包位于Java.util包下 ...

  7. 【Java笔记】集合(Collection接口)的使用

    面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储.使用 Array 存储对象方面具有一些弊端,而 Java 集合就像一种容器,可以动态地把多个对象的引用放入容器中 ...

  8. Educoder第1关:学习-Java集合类之Collection接口之往集合中添加元素

    ##educoer 第1关:学习-Java集合类之Collection接口之往集合中添加元素 任务:接收给定的一行字符串,实现如下需求: 1.通过空格(一个)切割字符串: 2.创建任意一种类型的集合: ...

  9. Java集合(3)--Iterator迭代器

    Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素.Collection接口继承了java.lang.Iterable接口,该接口有一个iterator ...

  10. java集合,Collection,list,set,map汇总

    1 Java集合简介 Java是一门面向对象的语言,就免不了处理对象,为了方便操作多个对象,那么我们就得把这多个对象存储起来,想要存储多个对象(变量),很容易就能想到一个容器(集合)来装载 简单来说集 ...

最新文章

  1. The key of C# 学习笔记I-II
  2. 如何读论文才不至于发疯?
  3. invoke 数组_如何对一个亿的数组进行快速排序
  4. HP-UX磁带备份错误收集
  5. js表单验证控制代码大全
  6. NLP之WE之CBOWSkip-Gram:CBOWSkip-Gram算法概念相关论文、原理配图、关键步骤之详细攻略
  7. 通过源码将git升级到最新版
  8. 图数据库_ONgDB图数据库与Spark的集成
  9. Java加密与解密的艺术~数字证书~证书管理openssl
  10. 1号店案例html源码_手把手教一起写jQuery版mini源码,分析jQuery的优势
  11. 南昌工程学院计算机考试题库和答案,南昌工程学院 语试题答案.doc
  12. 2.5亿!华为成立新公司!
  13. java 关闭对话框_java等待关闭对话框
  14. PDF编辑器是怎么使用的
  15. html页面乱码解决
  16. 最全整理:中国人工智能百强企业(100)榜单
  17. 共享没有计算机网络连接不上去,苹果usb共享给电脑连接不上怎么办
  18. linux下使用命令行获取公网ip地址
  19. HDU 4269 Defend Jian Ge 解题报告
  20. 「技术选型」Solr与ES难以抉择?且看第一回

热门文章

  1. Swift 版本相册,防微信相册
  2. 得物java怎么样_[上海] 得物(毒 app) 国际技术 招聘 高级 Java 开发工程师
  3. Qt开发 — 图片缩放简述
  4. import 快捷键 自动调整顺序_idea 快捷键修改去除 自动导入import 相关整理
  5. 27岁还能从事软件测试吗?软件测试这个行业能干到多少岁?
  6. 判断IE10以下提示更换浏览器
  7. Vue项目打包后不能正常显示页面
  8. create-react-app打包环境配置
  9. 使用 HTML 和 CSS 的玻璃态登录表单(含免费完整源码)
  10. java计算机毕业设计河南省农村多元化养老服务管理系统设计与实现MyBatis+系统+LW文档+源码+调试部署