Java下ArrayList 与 LinkedList

来源:互联网 发布:log4j2 json 编辑:程序博客网 时间:2024/06/18 15:13
  • ArrayList
    通过 List<> list = new ArrayList<>(); 生成的list为可调整大小的数组,实现所有可选列表操作,并允许组数值NULL存在。在常数运行时间下,可以进行Size,isEmpty,get,set,iterator,listlterator等操作,添加(add)操作内,添加n个元素所需时间为O(n),其他操作的运行时间(粗略来讲)为线性时间。
  • LinkedList
    通过List<> list = new LinkedList<>(); 生成的list为双向链表,可以实现所有可选列表操作,并允许所有元素加入(包括NULL)。所有操作都可以将作为双链表执行。

    -ArrayList 与 LinkedList对比
    两者主要区别为一个为数组,一个为链表,在实际使用过程中,两者存在效率区别,具体差距不在此详述,有众多文章进行了描述。

  • Set下的HashSet 与 TreeSet
    在List转化为Set时,通过以下语句

List<String> list = new ArrayList<String>();Set<String> set = new TreeSet<String>();//Set<String> set = new HashSet<String>();set.addAll(list);Iterator<String> iterator = set.iterator();

HashSet 生成后,不能保证元素的排列顺序,顺序可能发生变化。
TreeSet 生成以后,默认状态下将以升序排列,如要求规定排序方式,将通过接口Comparable进行排序设定,序列排序。

0 0
原创粉丝点击