In Java, the operation of data collection should be used very. Recently watched sporadic collection of little knowledge, here, a little sort of.
It is basically a collection of four commonly used class operations points.
First of all. A set is roughly divided into two directions. One is a common collection type, collection down through the interface, and a collection type of key-value pairs under the map interface. The four classes ArrayList and LinkedList, which are mainly mentioned today, are the two implementation classes of the list interface under the collection interface. The other two hashmap and TreeMap are the implementation classes of the map interface.
First, for collection, it has two main branches: list and set. A list is an ordered collection interface that can be repeated, and set is an unordered, non-recurring collection interface.
ArrayList:
- Thread Non-synchronous
- Description: Variable array, agreeing that all elements contain null
- Features: Ability to perform high-speed access and traversal of collections based on index location, with the disadvantage that inserting and deleting objects at a specified location is very slow
- Application: Suitable for random search and traversal, not suitable for insertion and deletion.
linklist:
- Thread Non-synchronous
- Introduction: Using linked list structure to save objects
- Features: Very easy to insert and delete objects to a specific location, the disadvantage is that random access to objects in the collection is very inefficient
- Application: Dynamic insertion and deletion of data, not suitable for random access and traversal
since all two classes implement the list interface, the methods are almost identical:
@Testpublic void Test1 () {list<string> list=new arraylist<string> (); List.add ("AK-47"); List.add ("ACER"); List.add ("ASUS"); List.set (2, "HP");//Replace List.add (2, "Dell");//Insert List.add ("LENOVO"); List.indexof ("HP");// Find iterator<string> iterator =list.iterator (); while (Iterator.hasnext ()) {System.out.println (Iterator.next ()) ;}}
HashMap:
- Description: Hash table based map interface implementation, agree null
- Features: The map set to join the deletion mapping relationship is more efficient, can not guarantee the mapping order
- Application: For adding a delete mapping relationship
TreeMap:
- Description: Implement map and SortedMap interface, do not agree null
- Features: There is a certain mapping order, but join delete mapping relationship efficiency than HashMap difference
- Application: For adding a delete mapping relationship
Demo Code:
@Testpublic void Test2 () {HashMap m=new HashMap (); M.put ("001", "Acer"), M.put ("002", "HP"), M.remove ("002"); M.put ("003" , "LENOVO"); Set Set=m.entryset (); Iterator Iterator=set.iterator (); while (Iterator.hasnext ()) {map.entry mapentity= (map.entry) iterator.next (); System.out.println (Mapentity.getkey () + ":" +mapentity.getvalue ());}}
Ok. Set on the first one to write here. Please correct me if there is anything wrong with you.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
The Java Foundation was founded