Several types of collections commonly used in Java

Source: Internet
Author: User
The collection is like an array for storing things.
Commonly used collections are divided into List (Ordered emissions), MAP (with the name and value of one by one corresponding storage), Set (neither unordered nor name)
Of these three, the list and set are sub-interfaces of the collection interface, and the map is not a sub-interface of the collection interface
The first is to introduce the collection
First, List:
To learn the list, here are the implementation classes for list: ArrayList and LinkedList
①arraylist: Index starting from 0, linear storage, indexed, with sequential
Main method: Add () is used to add elements to the collection, get () to obtain the element at the specified index, size () to get the length of the collection, equivalent to
②linkedlist: Also linear storage, indexed, with sequential
It's about the same as ArrayList, and it has methods that are not available in the ArrayList collection, such as: AddFirst (Object) This is the way to add elements to the front of the list collection, and the corresponding addlast
Differences and connections between ③arraylist and LinkedList:
The advantage of the LinkedList collection: You can specify a location when adding elements, much faster than adding elements to the ArrayList collection.
But LinkedList is much slower than the ArrayList collection when getting gets, and the slower the retrieval of the element gets.
Each has its own advantages, and for better use you can use the two together, store and add elements using the LinkedList collection, and use the ArrayList collection to get the element.


MAP: Name and value one by one corresponding, is unordered, famous and name can not be repeated
To learn map, first learn from its implementation class HashMap.
Adding elements to a map is added using the Put method. Map.put ("name", value)
Obtain is get (name)
All the values are available in the map as long as you get all the names.
This requires a set to get all the names:
Set Keyname=map.keyset ();
Using the iterator in set:
Iterator It=keyname.iterator ();
while (It.hasnext ()) {
Object Key=it.next (); Name
Object Value=map.get (key); Value
}


Third, Set: Neither the need to have a number also do not need to be famous, to go to the inside, storage speed relatively fast, but take a bad
Here is the common hashset (Set implementation Class)
add element as Add ()
Variable get element: using its iterator () method is the tool that executes the iterator
Iterator It=set.iterator ();
It.hashnext (); Determine if the next one has
It. Next ();
Use while (It.hasnext ()) {
System.out.println (It.next ());
The output is also unordered.

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.