Java Collection Class collation

Source: Internet
Author: User
Tags repetition

LinkedList

Pros: Quick Insert Removal

Cons: not suitable for random access

New Linkedlist<string>();        Staff.add ("Amy");        Staff.add ("Amy");        Staff.add ("Bob");        Staff.add ("Car");        Staff.remove ("Amy"); // even if there are duplicates, only the first one found will be deleted .

Conflict

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 implementation in Linkedlist->abstractsequentiallist->abstractlist, see the inherited class Abstractlist implemented a list of excuses, The LinkedList also implements the LinkedList interface. It's a conflict.

Iter1.remove () Delete node, Iter2 again access, definitely abnormal.
ArrayList

Advantage: Easy to access at random, use fast in single thread

Cons: Insertions and deletions need to be wasted, threads are out of sync, not suitable for multithreading

Vector

Pros: Multi-threaded synchronization

HashSet

Pros: Can you put ArrayList with repeating elements to HashSet de-duplication?

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:

Advantages: Orderly and filter equal, guaranteed not to repeat

 Packagetest;ImportJava.util.Comparator;ImportJava.util.SortedSet;ImportJava.util.TreeSet; Public classmain{ Public Static classItem { PublicItem (intWinth) {height=h; Weight=W; //TODO auto-generated Constructor stub        }         Public intweight; intheight; }     Public Static voidMain (string[] args) {SortedSet<Item> items =NewTreeset<item> (NewComparator<item>()        {             Public intCompare (item A, item B) {returnA.height * A.weight-b.height *B.weight;        }        }); Items.Add (NewItem (1, 2)); Items.Add (NewItem (2, 2)); Items.Add (NewItem (3, 1)); Items.Add (NewItem (4, 1));  for(Item item:items) {System.out.println (item.height+ "+" +item.weight); }    }    //Results://2 and 1 1//and 3//2 and 2//and 1 cells, 4, 1 is a repetition.}

If you want to use only one orderly but not the repetition of which should be used?

Navigationset

Navigableset extends SortedSet with a navigation method that reports the closest match to a given search target. Methods lower, floor, ceiling, and higher return elements that are less than, less than, equal to, greater than, equal to, or greater than the given element, and return NULL if no such element exists. You can access and traverse navigableset in ascending or descending order. The Descendingset method returns a view of the set, which represents all the relationship methods and direction methods that are reversed. The performance of ascending operations and views is likely to be better than the performance of descending operations and views. In addition, this interface defines the Pollfirst and Polllast methods, which return and remove the smallest and largest elements (if they exist), otherwise return null. The subset, HeadSet, and Tailset methods differ from SortedSet methods with similar names in that they can accept additional parameters that describe whether to include (or not include) the lower and upper bounds. Any navigableset Submap must implement the Navigableset interface.

Java Collection Class collation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.