java集合類整理

來源:互聯網
上載者:User

標籤:static   class   str   eset   不重複   線程   stat   hash   java   

LinkedList

優點:插入刪除迅速

缺點:不適合隨機訪問

 

List<String> staff = new LinkedList<String>();        staff.add("Amy");        staff.add("Amy");        staff.add("bob");        staff.add("car");        staff.remove("Amy");// 即使有重複的只只會刪掉第一個找到的

衝突

List<String> list = new LinkedList<String>();        list.add("a");        list.add("b");        list.add("d");        list.add("d");        ListIterator<String> iter1 = list.listIterator();        ListIterator<String> iter2 = list.listIterator();        System.out.println(iter1.next());        iter1.remove();        iter2.next();

listIterator的實現在LinkedList->AbstractSequentialList->AbstractList中,看到繼承的類AbstractList中實現了List借口,LinkedList也實現了LinkedList介面.這樣算衝突了,有點意思

iter1.remove()刪除節點了,iter2又要訪問,肯定異常了.
ArrayList

優點:隨機訪問方便,在單線程中使用快速

缺點:插入和刪除需要浪費,線程不同步,在多線程上不適合

Vector

優點:多線程同步

HashSet

優點:可以將有重複元素的ArrayList放到HashSet消除重複?

Set<String> words = new HashSet<>();
words.add("a");
words.add("c");
words.add("d");
words.add("e");
words.add("but");
words.add("e");
Iterator<String> iterator = words.iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());

}
// for (String string : words)
// {
// System.out.println(string);
// }

TreeSet:

優點:有序且過濾相等,保證不重複

package test;import java.util.Comparator;import java.util.SortedSet;import java.util.TreeSet;public class main{    public static class Item    {        public Item(int w, int h)        {            height = h;            weight = w;            // TODO Auto-generated constructor stub        }        public int weight;        int height;    }    public static void main(String[] args)    {        SortedSet<Item> items = new TreeSet<Item>(new Comparator<Item>()        {            public int compare(Item a, Item b)            {                return a.height * a.weight - b.height * b.weight;            }        });        items.add(new Item(1, 2));        items.add(new Item(2, 2));        items.add(new Item(3, 1));        items.add(new Item(4, 1));        for (Item item : items)        {            System.out.println(item.height + " and " + item.weight);        }    }    // 結果:    // 2 and 1 1    // and 3    // 2 and 2    // 還有1格4 1算作重複給扔掉了}

如果想僅僅用一個有序但是不重複的應該用哪個?

NavigationSet

NavigableSet 擴充 SortedSet,具有了為給定搜尋目標報告最接近匹配項的導航方法。方法 lower、floor、ceiling 和 higher 分別返回小於、小於等於、大於等於、大於給定元素的元素,如果不存在這樣的元素,則返回 null。可以按升序或降序訪問和遍曆 NavigableSet。descendingSet 方法返回 set 的一個視圖,該視圖表示的所有關係方法和方向方法都是逆向的。升序操作和視圖的效能很可能比降序操作和視圖的效能要好。此外,此介面還定義了 pollFirst 和 pollLast 方法,它們返回並移除最小和最大的元素(如果存在),否則返回 null。subSet、headSet 和 tailSet 方法與名稱相似的 SortedSet 方法的不同之處在於:可以接受用於描述是否包括(或不包括)下邊界和上邊界的附加參數。任何 NavigableSet 的 Submap 必須實現 NavigableSet 介面。

 

java集合類整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.