• Java 集合的常用操作(ArrayList, LinkedList, HashSet, HashMap)


    Java 集合的常用操作(ArrayList, LinkedList, HashSet, HashMap)


    在算法题中,经常要用到集合,在此作以记录总结。

    ArrayList

    • ArrayList 底层 双向链表
    • 初始化
    ArrayList<E> arraylist =new ArrayList<>();
    
    • 1
    方法描述补充
    添加元素arraylist.add(“Taobao”)
    访问元素arraylist.get​(int index)
    修改元素arraylist.set(2, “Google”)( 索引位置,要修改的值)
    删除元素arraylist.remove(3)删除第四个元素
    计算大小arraylist.size()
    判断包含arraylist.contains​(Object o)判断元素是否在 arraylist
    判断为空arraylist.isEmpty()判断 arraylist 是否为空
    转为数组arraylist.toArray()将 arraylist 转换为数组
    • 迭代
    for (int i = 0; i < arraylist.size(); i++) {
    	System.out.println(arraylist.get(i));
     }
     // for-each
     for (String str : arraylist) {
        System.out.println(str);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 排序
      Collections 类也是一个非常有用的类,位于 java.util 包中,提供的 sort() 方法可以对字符或数字列表进行排序
    Collections.sort(arraylist);  // 字母,数字排序
    
    • 1

    LinkedList

    • 可以作为 栈、队列
    • 初始化
    // 普通创建方法
    LinkedList<E> linkedlist = new LinkedList<E>();   
    // 使用集合创建链表
    LinkedList<E> linkedlist = new LinkedList(Collection<? extends E> c); 
    
    • 1
    • 2
    • 3
    • 4
    方法描述补充
    添加元素linkedlist.add(“Taobao”);添加在尾部
    开头添加linkedlist.addFirst(“Wiki”);添加在头部
    结尾添加linkedlist.addLast(“Wiki”);添加在尾部
    开头移除linkedlist.removeFirst();移除头部元素
    结尾移除linkedlist.removeLast();移除尾部元素
    获取开头linkedlist.getFirst()获取头部元素
    获取结尾linkedlist.getLast()获取尾部元素
    访问元素linkedlist.get(3);返回指定位置的元素
    设置元素linkedlist.set(6, “Google”);设置指定位置的元素
    计算大小linkedlist.size();
    判断包含linkedlist.contains​(Object o)判断元素是否在 list
    判断为空linkedlist.isEmpty()判断 list 是否为空
    转为数组linkedlist.toArray()将 list 转换为数组

    LinkedList 本身没有 isEmpty()方法,但他父类中有,所以可以直接调用。

    • 迭代
    for (int i = 0; i < linkedlist.size(); i++) {
        System.out.println(linkedlist.get(i));
    }
     // for-each
     for (String str : linkedlist) {
        System.out.println(str);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 排序
      Collections 类也是一个非常有用的类,位于 java.util 包中,提供的 sort() 方法可以对字符或数字列表进行排序
    //方式1:继承Comparable接口创建一个内部比较器
    @Override
    	public int compareTo(Object o) {
    		return this.height - ((User) o).height;
    	}
    	
    //方式2:创建一个外部比较器,然后传入sort方法中或传入LinkedList构造器中
    Collections.sort(linkedlist,(o1, o2) -> {
    					return ((User) o2).height - ((User) o1).height;
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    HashSet

    • 无序不允许有重复元素的集合,不是线程安全的
    • 初始化
    HashSet<String> set = new HashSet<String>();
    
    • 1
    方法描述补充
    添加元素set.add(“Taobao”);重复的元素不会被添加
    删除元素set.remove(“Taobao”);删除成功返回 true
    删除所有set.clear();删除集合中所有元素
    计算大小set.size();
    判断包含set.contains(“Taobao”)判断元素是否在 set
    判断为空set.isEmpty()判断 arraylist 是否为空
    • 迭代
     // for-each
     for (String i : set) {
        System.out.println(i);
    }
    
    • 1
    • 2
    • 3
    • 4
    • set 转换为 数组
    // 简单方法: set --> int[]
    int[] arr = new int[set.size()];
    int k = 0;
    for(Integer i : set){
        arr[k] = i;
        k++;
    }
    return arr;
    // .toArray()方法,可以转换,但为 Object[] 数组类型
    Object[] arr = set.toArray(new int[set.size()]);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    HashMap

    • 键值对(key-value)映射,无序
    • 初始化
    HashMap<Integer, String> map = new HashMap<Integer, String>();
    
    • 1
    方法描述补充
    添加元素map.put(“one”, “Google”);添加键值对(key-value)
    访问元素map.get(Object key)
    删除元素map.remove(key);
    删除所有map.clear();删除所有键值对
    计算大小map.size();
    判断keymap.containsKey​(Object key)是否存在指定的 key 对应的映射关系
    判断valuemap.containsValue​(Object value)是否存在指定的 value 对应的映射关系
    判断为空map.isEmpty()判断 map是否为空
    遍历keySet()hashMap 中所有 key 组成的集合视图
    遍历值values()hashMap 中存在的所有 value 值
    • Map中已知Key的值,如何修改Value的值? 直接重新 put 一个相同 key 的就行,value会自动覆盖。 map.put(1, "One"); ==> map.put(1,"First");

    • 迭代

    // ForEach KeySet,输出 key 和 value
    for (Integer key : map.keySet()) {
      	System.out.println(key);
      	System.out.println(map.get(key));
    }
    
    // 返回所有 value 值, 输出每一个value
    for(String value: map.values()) {
      	System.out.print(value + ", ");
    }
    
    // entrySet 遍历,效率最高!	
    for (Map.Entry<Integer, String> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
    
    
    // 迭代器 KeySet
    Iterator<Integer> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        Integer key = iterator.next();
        System.out.println(key);
        System.out.println(map.get(key));
    }
    
    // 迭代器 EntrySet!
    Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Integer, String> entry = iterator.next();
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 我们不能在遍历中使用集合 map.remove() 来删除数据,这是非安全的操作方式,但我们可以使用迭代器的 iterator.remove() 的方法来删除数据,这是安全的删除集合的方式。

    参考:Java菜鸟教程HashMap 的 7 种遍历方式与性能分析!

  • 相关阅读:
    OV2640图像出现细小条纹问题
    文本关键信息抽取-面向复杂文本结构的实体关系联合抽取研究(论文研读)(一)
    剑指 Offer 27. 二叉树的镜像
    Linux随记(四)
    编程资源分享2022
    常用激活函数汇总
    会声会影2022智能、快速、简单的视频剪辑软件
    使用 Azure OpenAI 打造自己的 ChatGPT
    Android Shimmer 布局微光(闪光,闪烁)效果
    Android相机调用-CameraX【外接摄像头】【USB摄像头】
  • 原文地址:https://blog.csdn.net/weixin_43806049/article/details/127938362