栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java集合框架笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合框架笔记

1 集合的概念

       什么是集合?

                概念:集合是对象的容器,定义了对多个对象进行操作的常用方法。可以实现数组的功能。

       集合与数组的区别:

               1、数组的长度固定,集合长度不固定;

               2、 数组可以存储基本数据类型和引用数据类型,集合只能存储引用数据类型。

2、Collection接口

        Collection体系集合

         Collection接口

                1、特点:代表一组任意类型的对象。无序、无下标、元素不可重复。

                2、常用方法:

                添加一个对象:boolean add();

                将一个集合中的所有对象添加到此集合中:boolean addAll();

                清空此集合中的所有对象:void clear();

                检查此集合中是否包含某个对象:boolean contains();

                检查此集合中是否包含指定集合中的所有元素:boolean containsAll();

                比较此集合中是否有与指定对象相等的对象:boolean equals(); 

                判断此集合是否为空:boolean isEmpty();

                移除此集合中的指定对象:boolean remove();

                移除此集合中包含指定集合中的所有元素:boolean removeAll();//移除两个集合中的相同元素

                保留此集合中也包含在指定集合中的所有元素:boolean retainAll();//两个集合求交集

                返回集合中的元素个数:int size();

                将集合转换为数组:Object[] toArray();

                3、遍历Collection元素:

                        首先创建Collection集合:

        Collection collection = new ArrayList();//创建Collection集合

                        遍历方法一:因为Collection是无序的,遍历时使用增强型for循环; 

        for (Object object : collection) {
        
        }

                        遍历方法二:使用迭代器,迭代器常用方法如下:

                                1、hasNext():是否有下一元素

                                2、next():获取下一元素

                                3、remove():删除当前元素

                               需要注意的一点是,在遍历迭代器的过程中,不能使用collection的删除方法。不然会抛并发修改异常。

        Iterator iterator = collection.iterator();
		while (iterator.hasNext()){
		    String s = (String) iterator.next();
		    iterator.remove();
		}

                4、 实例化对象时存储的值相同,也不会影响Collection 的操作,因为操作的是不同的对象。collection.clear()方法删除的是指向对象的地址,实际上对象还是存在的。

Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
Student s4 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s4);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}, Student{name='小明', age=18}]
collection.remove(s3);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
collection.remove(new Student("小明",18));
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection.contains(s1));//true
System.out.println(collection.contains(new Student("小明",18)));//false

3、List接口和实现类

        3.1 List接口
                List接口的特点:有序、有下标、元素可以重复。

                List常用方法:

                1、添加元素:list.add();

  List list = new ArrayList();
  //list在进行add时,如果指定下标,则按最新下标依次插入。插入的下标不可越界,否则会抛出异常。
  list.add("苹果");
  list.add("榴莲");
  list.add(0,"荔枝1");
  list.add(0,"荔枝2");
  list.add("荔枝3");
  System.out.println(list.size());//5
  System.out.println(list.toString());//[荔枝2, 荔枝1, 苹果, 榴莲, 荔枝3]

                索引排列:

                2、删除list中的元素,下标要已存在:list.remove();

    list.remove(2);//按索引值移除
    list.remove("苹果");//删除指定对象

                3、重置元素,下标要已存在:list.set();

   List list = new ArrayList();
   list.add("苹果");
   list.add("榴莲");
   list.add(0,"荔枝1");
   list.set(0, "apple");
   System.out.println(list.toString());//[apple, 苹果, 榴莲]

                4、遍历有4种方式

                        4.1普通for循环和增强for循环

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

                        4.2迭代器遍历

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

                        4.3列表迭代器遍历        

List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
System.out.println("使用列表迭代器 从前向后 遍历");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
    System.out.println(listIterator.next());
}
System.out.println("使用列表迭代器 从后向前 遍历");
while (listIterator.hasPrevious()){
    System.out.println(listIterator.previous());
}

                        打印结果:

                 5、判断

List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
//判断元素是否在集合中存在
System.out.println(list.contains("苹果"));//true
//判断集合是否为空
System.out.println(list.isEmpty());//false

                6、获取位置

List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
System.out.println(list.indexOf("榴莲"));//1
System.out.println(list.indexOf("荔枝2"));//-1:该对象不存在于list集合中

                7、List添加和删除数字

是否可以用indexOf,明天测试

List list = new ArrayList();
list.add(20);//list.add()在添加数字是自动装箱操作,int是基本数据类型,list中实际的对象是Integer包装类
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println(list);
//删除数据20
//list.remove(20);//此行代码执行报数组下标越界的异常。当删除数字时,默认是按int类型的索引值进行删除
//可以将数字转换为其他类型
list.remove((Object)20);
list.remove((Integer)30);
list.remove(new Integer(40));

                8、获取list的子集合,包头不包尾:list.subList();

List list = new ArrayList();
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println(list);//[20, 30, 40, 50, 60]
List list2 = list.subList(1, 3);
System.out.println(list2);//[30, 40]

        3.2 List实现类

                3.2.1 ArrayList

                特点:数组结构实现,查询快、增删慢;运行效率快,线程不安全。

                3.2.2 Vector

                特点:数组结构实现,查询快、增删慢;运行效率慢,线程安全。

                3.2.1 linkedList

                特点:链表结构实现,查询慢,增删快。

4、泛型和工具类

5、Set接口与实现类

6、Map接口与实现类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号