"Java from getting started to giving up" javase introductory article: Collections

Source: Internet
Author: User
Tags object object

Today we talk about the use of collections and common collection types in Java.

What is a collection?

Just recently the school inside military training, only heard the instructor shouted: "Set!!!" "You little moe new people on the fart of the run came up neatly arranged, this is the collection ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0020.gif "alt=" J_0020.gif "/>


The collection in Java is also the same meaning, Java a shout: "Collection!!!" ", then we put the data that needs to be put together in a set. Some people will say "arrays do not have this function". Yes, the array has this function, but the collection and the array, compared to the function will be more, and different sets of emphasis is not the same, specific advantages, we will explain next.

The largest difference between collections and arrays : The length of the collection is not fixed , you can add or remove elements arbitrarily, and the length of the array is fixed , more than the data is not put in, delete the data but the location is still there.


The collections in Java provide a series of interfaces and classes, all in the Java.util package:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/F9/wKioL1m14SDh_qy4AABzU80WRAM245.png "title=" 11. PNG "width=" 650 "height=" 219 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:650PX;HEIGHT:219PX; "alt=" Wkiol1m14sdh_ Qy4aabzu80wram245.png "/>

The common methods in the collection interface are as follows:

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/48/wKiom1m14iKz6G53AAD4v_W-Zmg432.png "title=" 12. PNG "width=" "height=" 355 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:355px; "alt=" Wkiom1m14ikz6g53aad4v_w-zmg432.png "/>

The collection interface is also divided into the list interface and the set interface, the differences are as follows:

List interface: stored in order, can save duplicate elements

Set interface: Store unordered, cannot save duplicate elements


Usage of list:

The most commonly used subclass of the list is--arraylist.

public static void Main (string[] args) {List List = new ArrayList (); List.add ("111"); List.add ("AAA"); List.add ("222"); List.add ("BBB"); List.add ("555"); List.add ("abc"); for (int i = 0; i < list.size (); i++) {System.out.println (List.get (i ));}}

Usage of Set:

Set the most common subclass of--hashset.

public static void Main (string[] args) {Set list = new HashSet (); List.add ("111"); List.add ("AAA"); List.add ("222"); List.add ("BBB"); List.add ("555"); List.add ("abc"); for (object object:list) {System.out.println (object);}}

list is ordered, so there is a corresponding get method to read the data according to subscript, and set is unordered, so we can only get the data all over the history . The left is the result of the list's traversal, and the right is the result of the set traversal.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/01/wKioL1m2NnmxY3olAAAQAjuh_IM927.png "style=" float : none; "title=" 11.png "alt=" wkiol1m2nnmxy3olaaaqajuh_im927.png "/> 650" this.width=650; "src=" https:// S4.51cto.com/wyfs02/m02/06/50/wkiom1m2nqdrnn1raaamocg_fro513.png "title=" 12.png "style=" Float:none; "alt=" Wkiom1m2nqdrnn1raaamocg_fro513.png "/>

As for deleting data and adding the same data, you crossing test it yourself, there's nothing to say.

The common methods in the map interface are as follows:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/01/wKioL1m2OUSQKD-KAADRqpU0JPM916.png "title=" 11. PNG "width=" "height=" 343 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:343px; "alt=" Wkiol1m2ousqkd-kaadrqpu0jpm916.png "/>

The most commonly used subclasses below the map are HashMap and Hashtable.

The map collection is stored in the form of a key-value pair , so it is clear that it is not in the order, which is similar to the dictionary used in our life (according to Pinyin or radicals to check the corresponding word).

The difference between HashMap and Hashtable: HashMap allows null keys or null values to appear. And Hashtable is the opposite.

public static void Main (string[] args) {map map = new HashMap (), Map.put ("Wang", "Wangqiwang egg"), Map.put ("Yang", "Yeung Yang Yang"), Map.put ("Li ", null); Map.put (null," Money first "), for (Object Key:map.keySet ()) {System.out.println (key+": "+map.get (Key)}}

Operation Result:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/01/wKioL1m2PJbA5tHfAAAhedJck1s439.png "title=" 11. PNG "alt=" Wkiol1m2pjba5thfaaahedjck1s439.png "/>


Use the Hashtable effect:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/06/51/wKiom1m2PPiBZ_A8AABA6TxjAss414.png "title=" 11. PNG "alt=" Wkiom1m2ppibz_a8aaba6txjass414.png "/>

In the implementation of the development, most of the occasions we use the generic collection , compared to the normal collection, its advantage is to save the data before the type of data must be specified, otherwise you will not be allowed to put!!!

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A5/02/wKioL1m2PnzRY4wmAABHzqvva3E788.png "title=" 11. PNG "alt=" Wkiol1m2pnzry4wmaabhzqvva3e788.png "/>


OK, today's collection Getting started here, the boys can use the previous set of tasks to implement the collection of this article to see (no longer use the array). How to use the specific collection, and so on after the comprehensive exercise to carefully analyze.



"Software Thinking" blog address: 51CTO , Blog Park  , interested small partners can go to see the relevant other blog posts.

This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1964337

"Java from getting started to giving up" javase introductory article: Collections

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.