欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Java - 集合

Java - 集合

2024/10/25 17:15:50 来源:https://blog.csdn.net/qq_57620101/article/details/141195783  浏览:    关键词:Java - 集合

Collection集合:

集合是一种容器,用来装数据的,类似于数组,但集合的大小可变,开发中也常常用。

1Collection 接口

Collection 是最基本的集合接口,【单列集合,每个元素(数据)只包含一个值】一个 Collection 代表一组 Object,即 Collection 的元素, Java不提供直接继承自Collection的类,只提供继承于的子接口(如List和set)。

Collection 接口存储一组不唯一,无序的对象。

2List 接口

List接口是一个有序的 Collection,使用此接口能够精确的控制每个元素插入的位置,能够通过索引(元素在List中位置,类似于数组的下标)来访问List中的元素,第一个元素的索引为 0,而且允许有相同的元素。

List 接口存储一组不唯一,有序(插入顺序)的对象。

3Set

Set 具有与 Collection 完全一样的接口,只是行为上不同,Set 不保存重复的元素。

Set 接口存储一组唯一,无序的对象。

4SortedSet
继承于Set保存有序的集合。
5Map      双列集合,每个元素包含两个值(键值对)

Map 接口存储一组键值对象,提供key(键)到value(值)的映射。

6Map.Entry
描述在一个Map中的一个元素(键/值对)。是一个 Map 的内部接口。
7SortedMap
继承于 Map,使 Key 保持在升序排列。
8Enumeration
这是一个传统的接口和定义的方法,通过它可以枚举(一次获得一个)对象集合中的元素。这个传统接口已被迭代器取代。
        ArrayList<String> list = new ArrayList<>();//有序 可重复,有索引list.add("java1");list.add("java2");list.add("java1");list.add("java2");System.out.println(list);System.out.println(list.get(0));//输出// [java1, java2, java1, java2]//java1HashSet<String> set = new HashSet<>();//无序 不可重复 不支持索引set.add("java1");set.add("java2");set.add("java1");set.add("java2");set.add("java3");System.out.println(set);//输出// [java3, java2, java1]

方法:

Collection - Java17中文文档 - API参考文档 - 全栈行动派 (qzxdp.cn)icon-default.png?t=N7T8https://doc.qzxdp.cn/jdk/17/zh/api/java.base/java/util/Collection.html

遍历集合:

 迭代器

迭代器是用来遍历集合的专用方式(数组没有迭代器),在java中迭代器的代表为Iterator

Collection集合获取迭代器的方法:

Iterator<E> iterator()   返回集合中的迭代器对象,该迭代器对象默认指向当前集合的第一个元素。

Iterator迭代器中的常用方法:

增强for循环

Lambda表达式

package CollectionTest;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class CollectionDemo01 {public static void main(String[] args) {Collection<String> c = new ArrayList<>();c.add("hello");c.add("hello1");c.add("hello2");c.add("hello3");System.out.println(c);//输出 [hello, hello1, hello2, hello3]//使用迭代器遍历集合//1.从集合对象中获取迭代器对象Iterator<String> it =c.iterator();System.out.println(it.next());System.out.println(it.next());System.out.println(it.next());System.out.println(it.next());
//        System.out.println(it.next());//异常//hello//hello1//hello2//hello3//2.使用循环结合迭代器遍历集合while (it.hasNext()){String s = it.next();System.out.println(s);}//3.lambda表达式c.forEach(new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s);}});//相当于c.forEach(s -> System.out.println(s));//相当于(方法引用)c.forEach(System.out::println);}
}

List类集合

1.ArrayList

遍历:

  1.  for循环(fori+tab快速生成一个for循环
  2. 迭代器
  3. 增强for循环
  4. Lambda表达式
         //for循环for (int i = 0; i < list.size(); i++) {String s1 = list.get(i);System.out.println(s1);}//迭代器Iterator<String> ii = list.iterator();while (ii.hasNext()){String s2 = ii.next();System.out.println(s2);}//增强for循环for (String si : list) {System.out.println(si);}//Lambda表达式list.forEach(s3 -> System.out.println(s3));

 2.LinkedList

 应用场景:

  1. 设计队列(先进先出,后进后出)
  2. 设计栈(后进先出,先进后出)
        //创建一个队列LinkedList<String> queque = new LinkedList<>();queque.addLast("张三");queque.addLast("李四");queque.addLast("王五");queque.addLast("赵六");queque.addLast("田七");System.out.println(queque);//出队System.out.println(queque.removeFirst());System.out.println(queque.removeFirst());System.out.println(queque);//2.创建一个栈对象LinkedList<String> stack = new LinkedList<>();//入栈(push)stack.addLast("张三");stack.push("张三1");stack.addLast("李四");stack.addLast("王五");System.out.println(stack);//出栈(pop)System.out.println(stack.removeFirst());System.out.println(stack.pop());System.out.println(stack.removeFirst());System.out.println(stack.removeFirst());System.out.println(stack);

push = addLast             pop = removeFirst

Set类集合

//创建一个set集合对象
//        Set<Integer> set =new HashSet<>();
//        Set<Integer> set = new LinkedHashSet<>();Set<Integer> set = new TreeSet<>();set.add(111);set.add(222);set.add(363);set.add(333);set.add(455);set.add(455);set.add(455);System.out.println(set);//HashSet 输出[455, 363, 333, 222, 111]--无序 不重复 无索引//LinkedHashSet 输出[111, 222, 363, 333, 455]--有序 不重复 无索引//TreeSet 输出[111, 222, 333, 363, 455]]--可排序(升序) 不重复 无索引

1.HashSet

 

   2.LinkedHashSet

 3.TreeSet

方法一:

Test.java:

Set<Student> students = new TreeSet<>();students.add(new Student("张三",100,20));students.add(new Student("李四",90,30));students.add(new Student("王五",80,40));students.add(new Student("王五5",80,40));students.add(new Student("赵六",70,50));System.out.println(students);//输出[Student{name='赵六, age=70, height=50.0}, Student{name='王五, age=80, height=40.0}, Student{name='张三, age=100, height=20.0}, Student{name='李四, age=750, height=30.0}]//王五5没存进去,因为比较年龄相等则不存

Student.java:

package set;public class Student implements  Comparable<Student>{private String name;private int age;private double height;public Student(String name,int age,double height){this.name=name;this.age=age;this.height=height;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//this o//this.age:表示当前对象(调用compareTo方法的对象)的年龄。//o.age:表示传入参数对象o的年龄。//this.age - o.age:计算当前对象的年龄与参数对象的年龄之差@Overridepublic int compareTo(Student o) {//如果认为左边对象大于右边对象,返回正整数//如果认为左边对象小于右边对象,返回负整数//如果认为左边对象等于右边对象,返回0,此时TreeSet集合指挥保留一个元素,认为两者重复//需求:按照年龄升序排列return this.age - o.age;}@Overridepublic String toString() {return "Student{" +"name='" + this.name +", age=" + this.age +", height=" + this.height +'}';}
}

方法二:

        //设置comparator对象//两个比较方法都存在会就近选择自己自带的比较器对象进行排序Set<Student> students = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//按照身高升序排序return Double.compare(o1.getHeight(),o2.getHeight());}});students.add(new Student("张三",100,55.5));students.add(new Student("李四",750,53.3));students.add(new Student("王五",80,40.5));students.add(new Student("王五5",80,40.5));students.add(new Student("赵六",70,50.7));System.out.println(students);//输出[Student{name='王五, age=80, height=40.5}, Student{name='赵六, age=70, height=50.7}, Student{name='李四, age=750, height=53.3}, Student{name='张三, age=100, height=55.5}]

集合的并发修改异常问题

package collection_exception;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class CollectionTest {public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("王麻子");list.add("李小龙");list.add("小李子");list.add("张三");list.add("李爱华");list.add("王李虎");list.add("李于呷");System.out.println(list);//[王麻子, 李小龙, 小李子, 张三, 李爱华, 王李虎, 李于呷]//需求:找出集合中全部含“李”的名字,并从集合中删除
//        Iterator<String> it = list.iterator();
//        while (it.hasNext()){
//            String name = it.next();
//            if(name.contains("李")){
//                list.remove(name);
//            }
//        }
//        System.out.println(list);//会报错//使用for循环删除“李”的名字// list.fori+enter快速生成
//        for (int i = 0; i < list.size(); i++) {
//            String name = list.get(i);
//            if (name.contains("李")){
//                list.remove(name);
//            }
//        }
//        System.out.println(list);//输出[王麻子, 小李子, 张三, 王李虎]//因为当i走到第二个"李小龙"时,第二个删除,i继续走到第三个,已经到”张三“并验证张三是否含李,所以漏筛选//Iterator会报错原因一样System.out.println("出现并发修改错误,如何解决?????");//for循环
//        for (int i = 0; i < list.size(); i++) {
//            String name = list.get(i);
//            if (name.contains("李")){
//                list.remove(name);
//                i--; //解决并发修改错误
//            }
//        }
//        System.out.println(list);//[王麻子, 张三]//迭代器
//        Iterator<String> it = list.iterator();
//        while (it.hasNext()){
//            String name = it.next();
//            if(name.contains("李")){list.remove(name);//会发生并发修改错误
//                it.remove();//删除迭代器当前遍历到的数据,每删除一个数据后,相当于也在底层做了i--
//            }
//        }
//        System.out.println(list);//[王麻子, 张三]//增强for循环//list.for+enter快速生成
//        for (String name : list) {
//            if(name.contains("李")){
//                list.remove(name);
//            }
//        }
//        System.out.println(list);//并发修改报错,且无法解决//lambda表达式list.forEach(name -> {if (name.contains("李")){list.remove(name);}});System.out.println(list);//同增强for循环,报错且无法解决并发修改错误}
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com