Java List 介绍和使用

发布于 2024-07-31 17:34:46 字数 4036 浏览 13 评论 0

List 基础

常用方法

  • set : 替换 List 中对应 index 的元素, list.set(index, element)
  • add : 增加元素 list.add(E e)
  • remove : 删除元素 list.remove(Object o)
  • for : 遍历列表 for (Object o: list)
  • removeAll : 対两个列表做减法 list1.removeAll(list2) ,将 list2 中的全部元素在 list1 中移除

功能实现

初始化 list

// 初始化空 list
List myList = new ArrayList();
// 初始化一个非空固定长度的 list
List<Integer> list = Arrays.asList(1, 2);
// 初始化一个非空非固定长度 list
List<Integer> list = new ArrayList<>(Arrays.asList(3, 4));

ArrayList

实现了 List 接口,底层使用的是 数组 ,存储空间上是相邻的,所以 查询 起来会很方便,查询效率也会比 LinkedList 要高,除了 List 抽象类通用方法外还有 isEmpty 方法

LinkedList

实现了 List 接口,底层使用的是使用 双向链表 ,存储的数据在空间上很可能不相邻,但是他们都有一个引用连在一起,所以 增删 起来会很方便,除了 List 通用方法外的常用的方法有 insert ,这些方法可以使得其变成堆栈( stack ),队列( queue ) 或双向队列( deque )

Vector

与 ArrayList 十分相似,区别就是就是 vector 是一种 线程安全类 ,它的方法都带有 Synchronized 关键字,实际中很少用到.如果遇到多线程的问题,java 提供了一个 Collections 工具类,可以把 ArrayList 转换成线程安全的类

FAQ

  • 获取最后的 element: Object element = list.get(list.size() - 1);

判断 list 是否相等

参考 : 先做特殊性判断, 然后再排序 equals

public  boolean equalLists(List<String> one, List<String> two){
    if (one == null && two == null){
        return true;
    }

    if((one == null && two != null)
      || one != null && two == null
      || one.size() != two.size()){
        return false;
    }

    // to avoid messing the order of the lists we will use a copy
    // as noted in comments by A. R. S.
    one = new ArrayList<String>(one);
    two = new ArrayList<String>(two);

    Collections.sort(one);
    Collections.sort(two);
    return one.equals(two);
}

使用 Iterator 遍历列表

使用 迭代器 Iterator 类来完成遍历.主要有两个方法,基于这两个方法就能进行遍历操作: next() 方法来获取序列的下一个元素, hasNext() 检查序列中是否还有元素

public static void remove(long id) {
    Iterator<Post> iterator = posts.iterator();
    while (iterator.hasNext()) {
        Post post = iterator.next();
        if (post.getId() == id) {
            posts.remove(post);
            return;
        }
    }
}

两个 list 找到仅存在于一个 list 中的元素

找到只在 list1 中出现不在 list2 中出现的唯一对象

Set<Date> setA = new HashSet<Date>(a);
Set<Date> setB = new HashSet<Date>(b);
setA.removeAll(setB);

// 其实简化版可以不创建 bd 对象,直接
Set<Date> setA = new HashSet<Date>(a);
setA.removeAll(bd);

对非基础对象的列表进行排序

一般使用 Collections 中的 sort 功能, 并且自定义一个 Comparator 进行指定数据的排序

List<Map<String, String>> list = new ArrayList<>();
//Add entries
Collections.sort(list, new Comparator<Map<String, String>>() {
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        Collection<String> values1 = o1.values();
        Collection<String> values2 = o2.values();
        if(!values1.isEmpty() && !values2.isEmpty()){
            return values1.iterator().next().compareTo(values2.iterator().next());
        }else{
            return 0;
        }
    }
});

BTW: compare 中的源码是返回 -1 0 1 的值,源码如下

// this.values < other.value 返回 -1 默认升序
public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

0 文章
0 评论
21 人气
更多

推荐作者

风暴

文章 0 评论 0

乐玩

文章 0 评论 0

英雄似剑

文章 0 评论 0

秋风の叶未落

文章 0 评论 0

luoshaoja

文章 0 评论 0

吴彦文

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文