Java基础之集合

本文深入讲解Java集合框架中的List、Set、Map等核心接口及其实现类的基本操作、遍历方式与排序技巧,并通过示例代码帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java基础之集合



Collection接口思维导图







1、List序列

List是有序、可重复的集合;


1.1 常用方法


1.  add(Object obj):添加元素;

2.  add(int i,Object o):在指定位置处添加元素;

3.  addAll(Collection c):把一个集合内的元素依次添加到当前集合;

4.  addAll(int i,Collection c):把一个集合内的元素添加到当前集合的指定位置;

5.  size():返回序列的长度;

6.  get(int i):返回指定位置上的元素;

7  set(int i,Object o):修改指定位置的元素,返回被修改的原对象;

8  remove(int i):删除指定位置的元素,返回指定位置的元素;

9.  remove(Object o):删除指定的对象,删除成功返回true;

10.  removeAll(Collection c):从列表中移除指定集合中包含的其所有元素,删除成功返回true;

11. clear():清除集合中的所有元素;

12. contains(Object o):判断集合中是否包含指定的元素,包含返回true;

13.indexOf(Object o):返回指定元素在集合中第一次出现的下标;

14.astIndexOf(Object o):返回指定元素在集合中最后一次出现的下标;

15.isEmpty():当且仅当集合长度为0时,返回true;

16. subList(int start,int end);截取集合,下标从start开始(包含),到end结束(不包含),返回一个新的集合;


实例:


1.2 添加

//获得List对象

      List list = new ArrayList();

      //依次添加

      list.add("tom");

      list.add("jack");

      list.add("Lily");

      list.add(3,"宋小宝");

      List list2 = new ArrayList();

      list2.add("张三");

      list2.add("李四");

      list2.add("王五");

//    list.add(list2);

// list.addAll(list2);//list集合中添加另一个集合的元素

      list.addAll(2, list2);



1.3 for遍历

//使用for循环遍历

      for (inti = 0;i <list.size();i++) {

        Object obj = list.get(i);

      }

     

      //使用foreach循环遍历

      for (Objectobj :list) {

        System.out.println(obj);

      }

迭代器遍历:

//使用Iterator迭代器遍历

      Iterator it = list.iterator();

      while(it.hasNext()){

        Object obj = it.next();

        System.out.println(obj);

      }

 

1.4 修改

//修改

      Object o = list.set(0, "汤姆");//返回被修改的对象

      System.out.println(o+"被修改了");

 

1.5 删除

      //根据下标删除

      Object o = list.remove(0);

      System.out.println(o+"被删除了");

      //根据对象删除

      booleanb =list.remove("tom");

      System.out.println(b);

      //删除指定集合中的所有元素

      List list3 = new ArrayList();

      list3.add("tom");

      list3.add("宋小宝");

      list3.add("王五");

      booleanb =list.removeAll(list3);

      System.out.println(b);

 

1.6 泛型实例

Student s1 = new Student();

      s1.name ="tom";

      Student s2 = new Student();

      s2.name ="jack";

      Student s3 = new Student();

      s3.name ="Lily";

      Person p = new Person();

      p.name ="张三";

      //泛型,规定当前的集合只能存某种类型

      List<Student> list = new ArrayList<Student>();

      list.add(s1);

      list.add(s2);

      list.add(s3);

      //遍历

      for (Studentstudent :list) {

        System.out.println(student.name);

      }

//使用迭代器遍历

      Iterator<Student> it = list.iterator();

      while(it.hasNext()){

        Student s = it.next();

        System.out.println(s.name);

      }

 

1.7 集合与数组的转换实例

      String[] str = {"tom","jack","Lzly","rose","Ljly","1"};

      //把数组转为集合

      List<String> list = Arrays.asList(str);

      //把集合转为数组

      Object[] obj = list.toArray();

1.8 集合排序实例

String[] str = {"tom","jack","Lzly","rose","Ljly","1"};

      //把数组转为集合

      List<String> list = Arrays.asList(str);

      Collections.sort(list);//自然升序

      Collections.reverse(list);//将集合元素倒置

      Collections.shuffle(list);//随机排序

     

      for (Strings :list) {

        System.out.println(s);

      }

1.9 使用排序算法对集合排序

      List<Integer> list = new ArrayList<Integer>();

      list.add(3);

      list.add(1);

      list.add(5);

      list.add(2);

      list.add(9);

      list.add(7);

     

      System.out.println("===========排序前==========");

      for (Objectobject :list) {

        System.out.print(object+" ");

      }

      System.out.println();

      System.out.println("===========排序后==========");

      for(inti = 1 ;i <list.size() ;i++){

        for(intj = 0 ;j <list.size() -i;j++){

           inta = (int)list.get(j);

           intb = (int)list.get(j+1);

           if(a >b){

              list.set(j,b);

              list.set(j+1,a);

           }

        }

      }

      //遍历

      for (Objectobject :list) {

        System.out.print(object+" ");

      }

      System.out.println();

 

2、Set集合

Set是不可重复,无序的集合

2.1 常用方法

1.  add(Object o):添加元素,添加成功返回true,重复添加,返回false;

2.  addAll(Collection c):把一个集合内的元素添加到当前集合;

3.  contians(Object o):判断集合中是否包含指定元素,包含返回true;

4.  isEmpty():当且仅当集合长度为0时,返回true;

5.  iterator():返回迭代器对象,用于遍历集合;

6.  remove(Object o):删除集合中的指定元素,删除成功返回true;

7.  removeAll(Collection c):从列表中移除指定集合中包含的其所有元素,删除成功返回true;

8.  clear():清除集合中的所有元素;

9.  size():返回集合的长度;

10.  toArray():把集合转为数组;

 


2.2 添加

//添加

      Student s1 = new Student("tom");

      Student s2 = new Student("jack");

      Student s3 = new Student("Lily");

      Student s4 = new Student("rose");

     

      Set<Student> set = new HashSet<Student>();

      set.add(s1);

      set.add(s2);

     

      //添加set集合

      Set<Student> set2 = new HashSet<Student>();

      set2.add(s3);

      set2.add(s4);

      set.addAll(set2);

      //添加list集合

      List<Student> list = new ArrayList<Student>();

      list.add(s3);

      list.add(s4);

      set.addAll(list);

      //添加数组

      Student[] stuArray = {s3,s4};

      set.addAll(Arrays.asList(stuArray));

 

2.3 遍历

      //使用foreach遍历

      for (Studentstu :set) {

        System.out.println(stu.name);

      }

     

      //使用迭代器遍历

      Iterator<Student> it = set.iterator();

      while(it.hasNext()){

        Student stu = it.next();

        System.out.println(stu.name);

      }

 

2.4 修改

//可以使用间接的方式修改

      booleanb =false;

      for (Studentstu :set) {

        if(stu.name.equals("tom")){//找到要修改的对象

           //把该对象删除

           b = set.remove(stu);

           if(b){

              break;

           }

        }

      }

      //添加要替换的新对象

      set.add(new Student("汤姆"));

 

2.5 删除

      //根据对象的属性删除,使用迭代器删除

      Iterator<Student> i = set.iterator();

      while(i.hasNext()){

        Student stu = i.next();

        if(stu.name.equals("rose")){

           i.remove();

        }

      }

      //删除指定对象

      booleanb =set.remove(s1);

      System.out.println(b);

      //删除多个对象

      Student[] stuArry = {s1,s3};

      booleanb =set.removeAll(Arrays.asList(stuArry));

      System.out.println(b);

      //清除所有元素

      set.clear();

      System.out.println(set.size());

 

 

 

3、Map集合

Map集合是一种键值对映射关系,Map中的键是唯一性的,即:键不能重复,值可以重复,一个键只能对应一个值,每个值可以有多个键

常用方法:

1.  put(K key,V value):向map集合中添加键值对,返回上一次该键对应的值,初次添加,返回null;

2.  putAll(Map<k,v>):将一个Map集合添加到该Map集合中;

3.  size():返回map集合中的键值对个数;

4.  get(Key k):通过Key返回Value;

5.  keySet():把所有的键以set集合的形式返回;

6.  isEmpty():当且仅当Map集合中的个数为0时,返回true;

7.  containsKey(K key):判断集合中是否包含指定的Key;

8.  containsValue(V value):判断集合中是否包含指定的Value;

9.  clear():清除集合中的所有键值对;

10.  remove(K key):根据键删除集合中的对应键值对,返回被删除键值对的值;

11.values():返回以Collection集合的形式所有的值;

12.entrySet():把所有的键-值对,以set集合的形式返回;

 

添加:

Map<Integer,String> map = new HashMap<Integer,String>();

     

      String s = map.put(1, "tom");

      String s2 = map.put(2, "jack");

      String s3 = map.put(3, "Lily");

      String s4 = map.put(4, "Lily");

 

遍历:


1.使用KeySet方法遍历


//使用KeySet获得map中的所有键值对

      Set<Integer> set = map.keySet();

      for (Integeri :set) {

        String v = map.get(i);

        System.out.println("键:"+i+",值:"+v);

      }


2.使用entrySet方法遍历


//使用entrySet方法遍历

      Set<Entry<Integer,String>> set =map.entrySet();

      for (Entry<Integer, String>e :set) {

        System.out.println("键:"+e.getKey());

        System.out.println("值:"+e.getValue());

      }


3.使用values遍历值

//使用values方法遍历值

      Collection<String> c = map.values();

      Iterator<String> it = c.iterator();

      while(it.hasNext()){

        String v = it.next();

        System.out.println("值:"+v);

      }

 


5、集合中的对象排序

实例:


Student类:

public class Student implements Comparable<Student>{

   public Stringid;

   public Stringname;

   public Integerage;

   @Override

   public int compareTo(Student s) {

      // TODO Auto-generated method stub

      return id.compareTo(s.id);

   }

}



Test类:

public static void main(String[] args) {

      List<Student> list = new ArrayList<Student>();

      list.add(new Student("1","tom",20));

      list.add(new Student("3","jack",22));

      list.add(new Student("2","Lily",21));

      list.add(new Student("6","hanmeimei",25));

      list.add(new Student("5","Lilei",24));

      //按照自然升序排序

      Collections.sort(list);

      //反转排序

      Collections.reverse(list);

 

   }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值