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

Source: Internet
Author: User

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 ...

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:

The common methods in the collection interface are as follows:

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.

12345678910111213 publicstaticvoidmain(String[] args) {    List list = newArrayList();    list.add("111");    list.add("aaa");    list.add("222");    list.add("bbb");    list.add("555");    list.add("abc");        for(inti = 0; i < list.size(); i++) {        System.out.println(list.get(i));    }}

Usage of Set:

Set the most common subclass of--hashset.

12345678910111213 publicstaticvoidmain(String[] args) {    Set list = newHashSet();    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.

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:

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.

1234567891011 publicstaticvoidmain(String[] args) {    Map map = newHashMap();    map.put("wang""王九蛋");    map.put("yang""杨羊洋");    map.put("li"null);    map.put(null"钱列先");        for(Object key : map.keySet()) {        System.out.println(key+":"+map.get(key));    }}

Operation Result:

Use the Hashtable effect:

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!!!

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 in small partners can go to see the relevant other blog posts.

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

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.