Common set of Java learning List,set,map

Source: Internet
Author: User

Commonly used collections have LIST,SET,MAP, these three are interfaces,
Where list, and set inherit from collection
And map is an interface that can hold key-value pairs.

Collection interface
Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements).
Some collection allow the same elements while others do not. Some can sort and others can't.
The Java SDK does not provide classes that inherit directly from collection, and the Java SDK provides classes that inherit from collection, such as list and set.

All classes that implement the collection interface must provide two standard constructors: the parameterless constructor is used to create an empty collection,
A constructor that has a collection parameter is used to create a new collection that has the same elements as the incoming collection collection.
The latter constructor allows the user to copy a collection.

How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that uses the iteration to access each element of the collection one at a time. Typical usage is as follows:
Iterator it = Collection.iterator (); Get an iteration child
while (It.hasnext ()) {
Object obj = It.next (); Get the next element
}
The two interfaces that are derived from the collection interface are list and set.

List interface, the two commonly used implementation classes of the list interface are the LinkedList class and the ArrayList class
The list is an ordered collection, using this interface to precisely control where each element is inserted. The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript), similar to an array of java.
Unlike the set mentioned below, the list allows the same elements.
In addition to the iterator () method with collection interface Prerequisites, List also provides a Listiterator () method,
Returns a Listiterator interface, compared to the standard iterator interface,
Listiterator has a number of add () methods that allow you to add, delete, set elements, and traverse forward or backward.

LinkedList class
The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList.
These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).
The LinkedList class uses a linked list structure to hold objects, the advantage of which is the ease of inserting and deleting objects into the collection, and often the need to insert/delete objects into the collection, using this class
The efficiency will be better, but it will be slower for random access to objects in the collection.

ArrayList class
ArrayList implements a variable-size array. It allows all elements, including null. The collection can be quickly and randomly accessed based on the index location, and the disadvantage is that the specified index location
When inserting an object or deleting an object, it is slow and the Size,isempty,get,set method runs at constant time.
The instantiation method is as follows:
List<string> list=new arraylist<string> ();

Summary: array-based list is suitable for querying, while LinkedList (linked list) is suitable for adding, deleting operations.

Set interface
Set is a collection that contains no duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false,set have a maximum of one null element.
Obviously, the constructor of a set has a constraint that the passed-in collection parameter cannot contain duplicate elements.
The common implementation classes of the set interface are HashSet and TreeSet.
The instantiation method is as follows:
Set<string> set1=new hashset<string> ();
Set<string> set2=new treeset<string> ();
The elements in the set collection cannot be duplicated, and are unordered, and may not be the same as the order in which the set elements are inserted when traversing set elements
When traversing the method of a set collection, you can use a for loop,
or use the iterator () method of the collection interface to get an iterator to traverse

Map interface
Note that map does not inherit the collection interface, and map provides a key-to-value mapping.
A map cannot contain the same key, and each key can only map one value.
The map interface provides views of 3 collections, and the contents of the map can be treated as a set of key sets, a set of value collections, or a set of key-value mappings.
The three methods commonly used when the map interface is traversed:
EntrySet ()
Returns a Set view that set<map.entry<k,v>> the mappings contained in this map.

KeySet ()
Returns a Set view of the keys contained in this set<k> map.

VALUES ()
Returns a Collection view of the values contained in this collection<v> map.

The common implementation classes of the map interface are HashMap and treemap. It is generally recommended to use the HashMap because the HashMap class implements the behavior for add and delete
Higher efficiency. HashMap is the implementation of a hash table-based map interface.
The mappings in treemap exist in a certain order, and if you want the objects in the Map collection to be in a certain order, use the TreeMap class

HashMap allows the use of NULL and NULL elements, but requires the uniqueness of the key, which does not guarantee the order of the collection, and in particular does not guarantee that the order is immutable
The TreeMap class not only implements the map interface, but also implements the SortedMap interface, so there is a certain order in the collection of the class. But when adding. Delete and locate,
Performance will be worse than the HashMap class. Because the mappings in the map collection implemented by the TreeMap class are sorted according to the key objects in a certain order. Because the object that does not allow the key is null
Implementation of the application, you can create a map collection through the HashMap class, when you need sequential output, then create a key value to the same mapping relationship of the TreeMap class, and then output

Practice Code Examples:

Import java.util.*;classtest{ Public Static voidMain (string[] args) {List<String> d=NewLinkedlist<string>();d. Add ("AAA");d. Add ("BBB");d. Add ("CCC");d. Add ("DDD");d. Add ("Eee"); Set<String>Set=NewHashset<string>();Set. Add ("1");Set. Add ("1");Set. Add ("1");Set. Add ("1");Set. AddAll (d);Set. Add ("2"); Set<String> intset=NewTreeset<string>(); Intset.add ("1"); Intset.add ("2"); Intset.add ("3"); Intset.add ("4"); Intset.add ("5"); Intset.add ("6"); Iterator<String> i=intset.iterator (); while(I.hasnext ()) {System. out. println (I.next ());} Map<String,String> map=NewHashmap<string,string>(); Map.put ("1"," One"); Map.put ("2"," A"); Map.put ("3"," -"); Map.put ("4"," -"); for(intL=1; L<map.size (); l++) {System. out. println (Map.Get(""-lm"")+"");} Collection<String> vv=map.values (); Iterator<String> vvi=vv.iterator (); while(Vvi.hasnext ()) {System. out. println (Vvi.next ());} Set<String> keyset=Map.keyset (); Vvi=keyset.iterator (); while(Vvi.hasnext ()) {System. out. println (Map.Get(Vvi.next ())); }}}

Usually set use more, familiar with the collection class, sometimes unexpected effect oh.

Common set of Java learning List,set,map

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.