Java Learning Notes-collection of map collections

Source: Internet
Author: User

Map Collection

Map Interface Overview:

Java.util.map<k,v> interface: is a double-column collection

Features of the Map collection:

    • is a double-column collection with two generic keys and value, using the same data type as key and value . can also be different
    • Key is not allowed to repeat,value can be repeated;
    • A key can only correspond to one value

Map Common Implementation class overview:

Java.util.hashmap<k,v> Collection implements Map<k,v> interface

features of the HashMap collection:

    • The bottom layer is a hash table (array + one-way linked list): Fast query, delete quickly
    • is an unordered collection

Common methods in the map interface:

    • 1.V Get (key) returns the corresponding value value based on the key value , or null if the key value does not exist
    • 2.V put (key, value); add elements to the collection (key and value)
      • Note: When added, if key does not exist, the return value is null
      • If the key already exists, the new value is replaced with the old value, and the old value is returned
    • 3. V Remove (key); deletes the key value pair corresponding to the key value, or fails if key does not exist . The return value is nullif key exists and the delete succeeds, and the value returned is deleted

Map Traversal mode

    • The first way: the way to find value by key:

There is a method in map:

Set <k> keySet (); returns the set set of keys contained by this map

Operation Steps :

1. Call the method KeySet in the map collection to take all the keys from the map collection and store them in the set set.

2. Iterate through the set collection to get every health in the map collection

3. get the value by means of the method get (key) in the map collection

You can use iterators with enhanced for loop traversal

Second way:map set Traversal key value mode

A method in the Map collection:

Set<map.entry<k,v>> EntrySet (); Returns a set view of the mappings contained in this map

Use steps

* 1. Using the method EntrySet in the Map collection , the key-value pairs (the key-value mappings ) are taken out to be stored in the set set

* 2. iterate through the set collection to get each entry object

* 3. use the methods in the Entry object Getkey and GetValue to get the health and value

You can use iterators with enhanced for loop traversal

HashMap Store Custom types:

Use HashMap to store custom class forms, because you want to guarantee the uniqueness of the key. Custom classes are required to override the hashcode () with the equals () method;

Linkedhashmap

Java.util.linkedhashmap<k,v> Collection extends hashmap<k,v> implements Map<k,v>

Characteristics:

1. The underlying is a hash table (array + unidirectional linked list)+ linked list

2. is an ordered set

Hashtable

the implementation class of Map Hashtable

The underlying data structure is a hash table , characterized by the same hashMap

Hashtable is a thread-safe collection that is single-threaded and slow to run

HashMap is a thread unsafe collection , multi-threaded , fast running speed

Hashtable Fate and Vector are the same, starting from JDK1.2 , replaced by the more advanced HashMap

HashMap allows null values to be stored , null

Hashtable does not allow null values to be stored, null

Hashtable His children, sub-category Properties are still active in the development stage

Properties

Java.util.Properties Collection extends Hashtable<k,v> collection

Properties Collection Features:

    • The Properties collection is also a double-column set, with key and value already built into the string type
    • the Properties collection is a combination of unique and IO streams
    • Temporary data stored in the collection can be persisted to a file in the hard disk storage
    • You can store the pair of key pairs in the file, read into the collection to use

Basic operations for the Properties collection: Adding data, traversing the collection

    Both key and value have been built into string types. It contains some methods related to the String class.

    1. Object SetProperty (string key, String value) adds a key-value pair to the collection, calling the Hashtable method Put
    2. String GetProperty (String key), which is the value that is obtained by key, which is equivalent to the Get (key) method in the Map collection
    3. set<string > stringpropertynames () returns the keyset for this property list. Equivalent to the keyset () method in the Map collection ;

the Load method of the Properties class :

    Key-value pairs stored in the file can be read into the collection using the

    1. void load (Reader reader)
    2. void load (InputStream instream)
  • * Parameters:
  • Reader Reader: character input stream, you can use FileReader
  • InputStream instream: byte input stream, you can use FileInputStream
  • * Operation procedure:
  • 1. Create a Properties collection Object
  • 2. Create character input stream FileReader object , construct method to bind data source to read
  • 3. use the method load in the Properties collection to read the key-value pairs stored in the file into the collection
  • 4. Releasing Resources
  • 5. traversing the Properties collection
  • * Note:
  • 1. Stream uses the reader character stream to read Chinese data
  • 2. stream using InputStream byte stream, can not operate Chinese, there will be garbled
  • in the configuration file for the 3.Properties collection, You can use the comment line data, using the #
  • in the 4.Properties collection configuration file, key and value are all strings by default, without adding "" ( Snake fill )
  • in the 5.Properties collection configuration file, the connection symbols for key and value can be used =, or spaces can be used

the store method of the Properties class uses:

/*

* Can put the temporary data stored in the collection , persisted in the hard disk file storage

* Void Store (writer writer, String comments)

* Void Store (OutputStream out, String comments)

* Parameters:

* writer Writer: character output stream, can use FileWriter

* outputstream out: byte output stream, can be used FileOutputStream

* String Comments: comment, explain the stored file, cannot use Chinese ( garbled), the default encoding format is Unicode encoding

* You can use the empty string

* Operation procedure:

* 1. Create A Properties collection to add data to the collection

* 2. Create a character output stream FileWriter object , construct a method to bind the destination to write to

* 3. Call the method store in the Properties collection , storing the temporary data stored in the collection , persisting the hard drive 's text

* 4. Freeing Resources

* Note:

* 1. Stream using the writer character Stream , you can write the Chinese data

* 2. stream using OutputStream byte stream, can not operate Chinese, there will be garbled

* 3.Propertie collection of files stored, usually end with. Properties ( programmer default)

Variable parameters of the method

New features that appear after Jdk1.5

Role:

When defining a method, the type of the method parameter is determined, but the number is indeterminate, and variable parameters can be used

Format:

Modifier Returns a value type method name (data type ... Variable name){

}

Use Note:

      • The underlying principle of a mutable parameter is an array
      • Passing different number of parameters, will create a different length of the array, receive these parameters variable parameters of the method, when called, can not pass parameters, pass arbitrary parameters

  Precautions:

    1. Parameter of a method, only one variable parameter can be defined
    2. If there are multiple arguments to the method, then the mutable argument must be written in the last digit of the list

Collections Collection Tool class

Java.util.Collections Collection Tool class

Collections Private parameterless construction method, the inside method is all static, through the class name can be directly used

Collections Common methods:

1. static void Shuffle (list<?> list) replaces the specified list with a default random source (disrupts the order of elements in the collection)

2.static void sort (list<?> list) sorts the specified list in ascending order according to the natural ordering of the elements (ascending sort of collection)

Java Learning Notes-collection of map 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.