java中关于Set与List之间不同的总结

来源:互联网 发布:淘宝被降权是什么意思 编辑:程序博客网 时间:2024/06/10 19:22

Set和List的不同


  • List中有set方法,set(int index, E element),而Set中没有Set方法。
  • List中的contains实现原理,利用对象的equals()方法比较是否包含这个对象,Set中的contains方法是先调用hashcode()方法,比较哈希值,若相等再利用contains方法比较。

List中常用的方法


1. add(E o),add(int index ,E element)2. addAll(Collection< extends E> c) :用数组时需:Arrays.asList()转化为List3. iterator() :返回List的迭代器4. contains(E o)5. containsAll(Collection< extends E> c):用数组时需:Arrays.asList()转化为List6. get(int Index) 返回List中的对象7. size()8. remove(int Index) remove(E o)9. removeAll()10. set(int Index ,E new element)11. indexof() 返回索引位置,依次调用equals方法,lastindexof与之相反

Set中常用的方法

1. add(E o)2. addAll(Collection< extends E> c) :用数组时需:Arrays.asList()转化为List3. iterator() :返回List的迭代器4. contains(E o)5. containsAll(Collection< extends E> c):用数组时需:Arrays.asList()转化为List6. size()7. remove(int Index) remove(E o)8. removeAll()9. hashcode()
1 0