A preliminary understanding of the collection class container in Java

Source: Internet
Author: User

  Container (Collection)

An array is a container, and a collection is a container.

In Java programming, a thing that is loaded with a variety of other objects (reference types), called containers

(All the books in the library, in order to manage all the books in the library, you need to put these books in a thing, the knowledge currently mastered, can only be an array, the length of the array is fixed,

    This is   A question of how long the length of the array should be defined. The length is not fixed, because you do not know how many books, this time need such a mechanism:

Define a kind of thing, the length is not fixed, can be added and deleted at any time, this kind of thing is collection, as long as do not exceed memory, add in any way.

  Collection---Interface:

    Set interface

*hashset---Implementation class (Implementation mechanism with hash table as the underlying)

TreeSet---Implementation class (Implementation mechanism with two-fork tree (a data structure)

No order, no repetition (can be called repeat with equals)

    ! List interface
*arraylist---Implementation class (an array-based implementation mechanism)

LinkedList---Implementation class (the implementation mechanism with the underlying list)

There are sequences that can be repeated

ArrayList (the API says the initial capacity is 10, pay attention to this problem), LinkedList
Sequential, can be added repeatedly
Get
Set (have a return value to note!)

Add ()

Remove (int)

Remove (Object)

IndexOf

LastIndexOf

    ! Map interface (key-value pairs)

      *hashmap---implementation

TREEMAP---implementation class

Every time I put it in, it's a pair.

HASHMAP (hash table indexed), TREEMAP (binary tree index)
Key-value pairs (keys cannot be duplicated, what is called duplicates)
Put (why is there a return value Ah Pro?)
Get
Remove (Why do you have a return value?)
ContainsKey
Containsvalue
Size
IsEmpty
Putall
Clear

  Method of Collection interface

Use of the collection interface

code example: ECLIPSE--COM.TEST3

collection<string> C = new arraylist<string> ();

Question: Why not write directly arraylist<string> a = new arraylist<string> ();

C.add (parameter type must be object)

C.remove method: By judging whether two objects are the equals of each other to determine if it is time to delete the object, the custom class needs to override the parent class's Equals method

Overriding the Equals method, you should also override the Hashcode method

Hashcode is usually used to index, an object through its Hashcode value can find its address in memory, so two objects if equals,

And again as an index, the value of the hashcode must be equal

Exercise: using string[] args to detect the number of occurrences of an input string array on the command line

 Packagecom.maya.test;Importjava.awt.List;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Map; Public classLianxi { Public Static voidMain (string[] args) {Map C=NewHashMap (); ArrayList L=NewArrayList ();  for(inti=0; i<args.length;i++){            if(C.get (args[i]) = =NULL) {c.put (args[i],1); }Else{                intValu = (int) C.get (Args[i]) +1;            C.put (Args[i], valu);    }} System.out.println (c); }}

A preliminary understanding of the collection class container in Java

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.