Summary of high frequency collections usage in Java Development and Java Platform Implementation source code viewing method

Source: Internet
Author: User

Life two, two three, sansheng all things, the foundation is always a computer man's body of this, I believe that the people who see this article generally know the data structure of this course, or will not find me this article. The analysis of this course of data structure lays the foundation of the engineers ' understanding of the container class and collection class in various platforms, as many people say, if you don't understand the collection class of a certain platform, it is possible that you are not using the code on that platform, but the data structure is not understood thoroughly.

On the Windows NT platform, MFC, ATL provides a small collection class, and the functionality is weak, which leads to the fact that the standard is STL, compared to the. Net and Java platform STL in use slightly less, but the efficiency should be better. However, regardless of which implementation, is based on the theoretical basis of data structure, this article will discuss the Java platform of high-frequency collection class usage.

The first thing to discuss is linkedlist, when we need to insert or delete elements frequently, our choice is linkedlist, because there is no cost of moving the elements, but linkedlist in Java has a quirk, you may need to note that Whenever you need to delete an element, you need to call the next () of the iterator, and then remove (), which is slightly unnatural to use.

 Public classlinkedlisttest{ Public Static voidMain (string[] args) {List<String> A =NewLinkedlist<>(); A.add ("Amy"); A.add ("Carl"); A.add ("Erica"); List<String> B =NewLinkedlist<>(); B.add ("Bob"); B.add ("Doug."); B.add ("Frances"); B.add ("Gloria"); //merge the words from B to aListiterator<string> Aiter =A.listiterator (); //iterator<string> aiter = A.iterator ();Iterator<string> biter =B.iterator ();  while(Biter.hasnext ()) {if(Aiter.hasnext ()) Aiter.next ();      Aiter.add (Biter.next ());      } System.out.println (a); //remove every second word from Bbiter=B.iterator ();  while(Biter.hasnext ()) {biter.next ();//Skip One element         if(Biter.hasnext ()) {biter.next ();//Skip Next elementBiter.remove ();//Remove that element}} System.out.println (b); //Bulk Operation:remove All words in B from aA.removeall (b);   System.out.println (a); }}

LinkedList when looking for data, the time complexity is O (n), when the demand for more high-level search needs to use more efficient containers, such as HashSet, its search for elements can achieve linear time complexity.

 Public classsettest{ Public Static voidMain (string[] args) {Set<String> words =NewHashset<string> ();//HashSet implements Set      LongTotalTime = 0; Scanner in=NewScanner (system.in);  while(In.hasnext ()) {String Word=In.next (); LongCalltime =System.currenttimemillis ();         Words.add (word); Calltime= System.currenttimemillis ()-Calltime; TotalTime+=Calltime; } Iterator<String> iter =Words.iterator ();  for(inti = 1; I <= && iter.hasnext (); i++) System.out.println (Iter.next ()); System.out.println (". . ."); System.out.println (Words.size ()+ "distinct words." + TotalTime + "milliseconds."); }}

HashSet iterative access to elements is random, so if you are sensitive to order, you might want to consider treeset, the inserted element is automatically sorted, and the output is an ordered set.

 Public classtreesettest{ Public Static voidMain (string[] args) {SortedSet<Item> parts =NewTreeset<>(); Parts.add (NewItem ("Toaster", 1234)); Parts.add (NewItem ("Widgets", 4562)); Parts.add (NewItem ("Modem", 9912));      SYSTEM.OUT.PRINTLN (parts); SortedSet<Item> sortbydescription =NewTreeset<> (NewComparator<Item>()         {               Public intCompare (item A, item b) {String Descra=a.getdescription (); String DESCRB=b.getdescription (); returnDescra.compareto (DESCRB);      }         });      Sortbydescription.addall (parts);   System.out.println (sortbydescription); }}

Remember the little Gan in the data structure? Java also has a corresponding implementation, his name is Priorityqueue, the small Gan itself iteration is unordered, but the small Gan guarantee that each deletion of the element is the smallest of the collection, that is, the root.

 Public classpriorityqueuetest{ Public Static voidMain (string[] args) {Priorityqueue<GregorianCalendar> PQ =NewPriorityqueue<>(); Pq.add (NewGregorianCalendar (1906, Calendar.december, 9));//G. HopperPq.add (NewGregorianCalendar (1815, Calendar.december, 10));//A. LovelacePq.add (NewGregorianCalendar (1903, Calendar.december, 3));//J. von NeumannPq.add (NewGregorianCalendar (1910, Calendar.june, 22));//K. ZuseSystem.out.println ("Iterating over elements ...");  for(GregorianCalendar date:pq) System.out.println (Date.get (calendar.year)); System.out.println ("Removing elements ...");  while(!Pq.isempty ())   System.out.println (Pq.remove (). Get (Calendar.year)); }}

Everyone in the use of Set class collection when there is an inconvenient place is, if I need to find an element, I have to know the details of this element, all the information, but not through the keyword search, map appears to solve the problem, map itself is a key value pair, you can use the key to take the value, This makes it much easier to find, but the price of paying is to store a key more.

 Public classmaptest{ Public Static voidMain (string[] args) {Map<string, employee> staff =NewHashmap<>(); Staff.put ("144-25-5464",NewEmployee ("Amy Lee")); Staff.put ("567-24-2546",NewEmployee ("Harry Hacker")); Staff.put ("157-62-7935",NewEmployee ("Gary Cooper")); Staff.put ("456-62-5527",NewEmployee ("Francesca Cruz")); //Print all EntriesSystem.out.println (staff); //Remove an entryStaff.remove ("567-24-2546"); //Replace an entryStaff.put ("456-62-5527",NewEmployee ("Francesca Miller")); //Look up a valueSystem.out.println (Staff.get ("157-62-7935")); //iterate through all entries       for(Map.entry<string, employee>Entry:staff.entrySet ()) {String key=Entry.getkey (); Employee value=Entry.getvalue (); System.out.println ("key=" + key + ", value=" +value); }   }}

In addition, the Java language itself is open source, all these collection class implementation is open source, if we can learn and debug the code written by these masters, will undoubtedly make our learning more effective, then how to set up to debug these platform source?

1. The JDK must be installed, the JRE is not

2. Select the JRE under the JDK when configuring the project

3. In the class or function you want to study the key F3, more shortcut keys please refer to the following, the specific settings please see.

Http://www.cnblogs.com/pugang/p/4471558.html

Summary of high frequency collections usage in Java Development and Java Platform Implementation source code viewing method

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.