Collection 和 Collections 比较

来源:互联网 发布:怎样增加淘宝信誉度 编辑:程序博客网 时间:2024/06/06 06:37

区别

Collection是一个接口,Set接口和List接口的父类(注意:Map接口不是)。

Collections是一个包装类,操作Collection类的工具类,类中方法都是静态的。

Iterator

1. Iterator接口

所有实现Collection接口容器类都有一个iterator方法用以返回一个实现iterator接口对象。
iterator对象称迭代器,方便对实现对容器内元素的遍历操作。

2. Iterator接口方法

boolean hasNext()   //判断右边是否有元素Object next()       //返回游标右边的元素,且将游标移动到下一个位置。void remove()       //删除游标左边的元素,只可以执行一次。

3. 典型用法

Iterator it = collection.iterator(); // 获得一个迭代子while(it.hasNext()) {    Object obj = it.next(); // 得到下一个元素}

Collection

Collection提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式。

1. Set

Set是最简单的集合,元素不重复,集合中的对象不按照特定的方式排序。主要有如下两个实现:HashSet和TreeSet。

1.1 HashSet

HashSet类按照哈希算法来存取集合中的对象,具有很好的存取性能。当HashSet向集合中加入一个对象时,会调用对象的hashCode()方法获取哈希码,然后根据这个哈希码进一步计算出对象在集合中的存放位置。

Set hs = new HashSet<E>();  //定义一个对象hs.add(o);                  //添加一个元素ohs.addAll(new ArrayList()); //添加多个元素hs.size();                  //对象中元素的个数hs.isEmpty();               //对象是否为空hs.contains(o);             //对象中是否包含元素ohs.remove(o);               //在对象中删除元素ohs.clear();                 //清空对象中的元素

1.2 TreeSet

TreeSet 是一个有序的集合,它的作用是提供有序的Set集合。TreeSet的性能比HashSet差但是我们在需要排序的时候可以用TreeSet。TreeSet底层是使用TreeMap实现的。

//TreeSet排序规则Comparator案例public class HelloWorld {      public static void main(String[] args) {          TreeSet set = new TreeSet(new PersonComparator());          set.add(new Person("lwc", 80));          set.add(new Person("nxj", 70));          set.add(new Person("lp", 60));          set.add(new Person("fy", 75));          Iterator ite = set.iterator();          while (ite.hasNext()) {              Person p = (Person)ite.next();              System.out.println(p.name);          }      }  }  class Person {      String name;      int score;      public Person(String name, int score) {          this.name = name;          this.score = score;      }  }  class PersonComparator implements Comparator {      public int compare(Object o1, Object o2) {          Person p1 = (Person) o1;          Person p2 = (Person) o2;          return p1.score - p2.score;      }  }  

2. List

List继承自Collection接口。List是一种有序集合,List中的元素可以根据索引(顺序号:元素在集合中处于的位置信息)进行取得/删除/插入操作。实现类包括LinkedList,Vector,ArrayList。其中ArrayList底层采用数组实现,LinkedList底层采用双链表实现。

2.1 ArrayList

import java.util.ArrayList;import java.util.List;public class HelloWorld {    public static void main(String[] args) {        List<String> list = new ArrayList<String>();        // 向列表的尾部追加指定的元素        list.add("lwc");        // 在列表的指定位置插入指定元素        list.add(1, "nxj");        // 追加指定 collection 中的所有元素到此列表的结尾        list.addAll(new ArrayList());        // 从列表中移除所有元素        list.clear();        // 如果列表包含指定的元素,则返回true        list.contains("nxj");        // 如果列表包含指定 collection 的所有元素,则返回 true        list.containsAll(new ArrayList());        // 比较指定的对象与列表是否相等        list.equals(new ArrayList());        // 返回列表中指定位置的元素        list.get(0);        // 返回列表的哈希码值        list.hashCode();        // 返回列表中首次出现指定元素的索引,如果列表不包含此元素,则返回 -1        list.indexOf("lwc");        // 返回列表中最后出现指定元素的索引,如果列表不包含此元素,则返回 -1        list.lastIndexOf("lwc");        // 如果列表不包含元素,则返回 true        list.isEmpty();        // 移除列表中指定位置的元素        list.remove(0);        // 移除列表中出现的首个指定元素        list.remove("lwc");        // 从列表中移除指定 collection 中包含的所有元素        list.removeAll(new ArrayList());        // 用指定元素替换列表中指定位置的元素        list.set(0, "lp");        // 返回列表中的元素数        list.size();        // 返回列表中指定的fromIndex(包括)和toIndex(不包括)之间的部分视图        list.subList(1, 2);        // 返回以正确顺序包含列表中的所有元素的数组        list.toArray();        // 返回以正确顺序包含列表中所有元素的数组        list.toArray(new String[] { "a", "b" });        // 遍历方法一        Iterator<String> ite1 = list.iterator();        while (ite1.hasNext()) {            String str = ite1.next();            System.out.println(str);        }        System.out.println("---------------------");        // 遍历方法二(方法一的变形)        for (Iterator<String> ite2 = list.iterator(); ite2.hasNext();) {            String str = ite2.next();            System.out.println(str);        }        System.out.println("---------------------");        // 遍历方法三        for(String s : list){            System.out.println(s);        }    }}

2.2 LinkedList

import java.util.LinkedList;import java.util.List;import java.util.ListIterator;public class HelloWorld {    public static void main(String[] args) {        List link = new LinkedList();        link.add(123);        link.add("lwc");        link.add(8.8);        link.add("nxj");        link.add(520);        printList(link);        printReversedList(link);    }    public static void printList(List link) {        System.out.println("正序链表中的元素");        // 的到链表的迭代器,位置指向链头        Iterator li = link.iterator();        // 判断迭代器中是否有下一个元素        while (li.hasNext()) {            // 返回下个元素            System.out.print(li.next() + " ");        }        System.out.println();    }    public static void printReversedList(List link) {        System.out.println("逆向链表中的元素");        // 的到链表的迭代器,位置指向link.size()结尾        ListIterator li = link.listIterator(link.size());        // 判断迭代器中是否有前一个元素        while (li.hasPrevious()) {            // 返回前一个元素            System.out.print(li.previous() + " ");        }        System.out.println();    }}/*打印结果:    正序链表中的元素    123 lwc 8.8 nxj 520    逆向链表中的元素    520 nxj 8.8 lwc 123*/

Collections

//Collections常用方法public class Test {    public static void main(String[] args) {        // 将所有元素从一个列表复制到另一个列表        Collections.copy(new ArrayList(), new ArrayList());        // 如果两个指定collection中没有相同的元素,则返回 true        Collections.disjoint(new ArrayList(), new ArrayList());        // 使用指定元素替换指定列表中的所有元素        Collections.fill(new ArrayList(), new Object());        // 返回指定 collection 中等于指定对象的元素数        Collections.frequency(new ArrayList(), new Object());        // 返回指定源列表中第一次出现指定目标列表的起始位置,如果没有出现这样的列表,则返回 -1        Collections.indexOfSubList(new ArrayList(), new ArrayList());        // 根据元素的自然顺序,返回给定 collection 的最大元素        Collections.max(new ArrayList());        // //根据元素的自然顺序,返回给定 collection 的最大元素        Collections.min(new ArrayList());        // 使用另一个值替换列表中出现的所有某一指定值        Collections.replaceAll(new ArrayList(), "oldVal", "newVal");        // 反转指定列表中元素的顺序        Collections.reverse(new ArrayList());        // 返回一个比较器,它强行反转        Collections.reverseOrder();        // 返回一个比较器,它强行反转指定比较器的顺序        Collections.reverseOrder(new Comparator() {            @Override            public int compare(Object o1, Object o2) {                return 0;            }        });        // 使用默认随机源随机更改指定列表的序列        Collections.shuffle(new ArrayList());        // 根据元素的自然顺序对指定列表按升序进行排序        Collections.sort(new ArrayList());        // 根据元素的自然顺序对指定列表按降序进行排序        Collections.sort(new ArrayList(), Collections.reverseOrder());        // 在指定列表的指定位置处交换元素        Collections.swap(new ArrayList(), 1, 2);    }}
0 0
原创粉丝点击