Java Data structure

Source: Internet
Author: User

One, the list interface, the Orderly collection interface, can precisely control each element to insert the position, agrees to have the same element

1. Linked list, LinkedList implements the list interface, agrees with the null element, provides a get (), remove (), insert () method, no synchronization method

Public <textarea readonly="" name="code" class="java">Void Add () {LinkedList List = new LinkedList (); List.add ("Link1"); List.add ("Link2"); List.add ("Link3"); Iterator it = List.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} It.remove (); Iterator it1 = List.iterator (); for (int i = 0; i < list.size (); i++) {System.out.println (It1.next ());}}</textarea>

2. Array list, ArrayList, ability to dynamically change the capacity of the array, non-synchronous

The array list contains the object type, so the type of objects that are stored in the array list are replaced with the parent of their prototype, which is the type conversion when the elements are extracted.

Public <textarea readonly="" name="code" class="java">static void Main (string[] args) {ArrayList al=new ArrayList (); Al.add ("name"); Al.add ("value"); Al.add ("number"); for (int i=0;i<al.size (); i++) {System.out.println (Al.get (i)); }}</textarea>
second, set interface, does not include the collection interface of repeated elements
1. Hash set, HashSet, implements the set interface, non-linear synchronization
Almost similar to linked lists and arrays, but when data processing takes less time to process data than using linked lists, hash sets are typically used when working with big data

Public <textarea readonly="" name="code" class="java">static void Main (string[] args) {long time=0; HashSet hs=new HashSet (); ArrayList al=new ArrayList (); Long Starttime=system.currenttimemillis (); for (int i=0;i<10000;i++) {Hs.add (new Integer (i)); } System.out.println (System.currenttimemillis ()-starttime); for (int i=0;i<10000;i++) {Al.add (new Integer (i)); } System.out.println (System.currenttimemillis ()-starttime); }</textarea>
2. Tree set, TreeSet, implements the set interface, implements the sorting function, the elements in the collection are arranged in ascending order by default.
The object that uses the tree set data structure, need implement comparable interface, the data of tree set output is sorted


third, the map interface, does not inherit the collection interface, which provides a key-to-value mapping, the map cannot include the same key, each key can only map a value.
1. Hash list class, HashTable, inherit the map interface, non-empty (Non-null) object can be as key or value, is synchronous
Add data: Put (key,value);
Fetch data: Get (key);
Features: unordered, therefore able to find specific elements at high speed
Public <textarea readonly="" name="code" class="java">static void Tabletest () {Hashtable ht = new Hashtable () ht.put ("Key1", "value1"), Ht.put ("Key2", "value2"); String value1= (String) ht.get ("Key2"); System.out.println (value1);}</textarea>
Constructor: Hashtable (), initial capacity 11, load factor 0.75, that is, the hash list already has 75% of the position is filled, then hashed.
The higher the load factor (closer to 1), the higher the memory usage, the longer the element is looking for, and the more wasted the memory, the shorter the search time
The object of key will determine the corresponding value position by calculating the hash function, so the object of key must implement the Hashcode () and Equals () methods.
Two objects similarly, their hashcode must be the same; two objects are different, and their hashcode are not necessarily different.
Conflict: The hashcode of different objects is also called a conflict, which causes the time overhead of manipulating the hash table to increase. So to replicate the Equals () and Hashcode () methods at the same time
2. Hash map class, HASHMAP, similar to Hashtable, not HashMap non-synchronous, and agree null
Public <textarea readonly="" name="code" class="java">static void Maptest () {map<string string= "" > Map=new hashmap<string string= "" > (); Map.put ("Key1" , "value1"), Map.put ("Key2", "value2"), Map.put ("Key3", "Value3"); for (map.entry<string string= "" > Entry: Map.entryset ()) {System.out.println (Entry.getkey ()); System.out.println (Entry.getvalue ());} String value1= (String) map.get ("Key1"); System.out.println (value1);} </string></string></string></textarea>
3. Tree image, TreeMap, output data sorted according to keyword

Java Data structure

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.