Java Collection: Introduction to Collection and map

Source: Internet
Author: User
Tags set set

Collection Collection://java.util.collection

List Interface: The list collection includes all the implementation classes of the list interface and the list interface. Elements in the list collection allow repetition, and the order of the elements is the order in which they are inserted

Common implementation classes for list interfaces: ArrayList and LinkedList

? ArrayList class: Implements a mutable array that allows all elements to be saved, including NULL, and provides quick random access to the collection based on the index location; The disadvantage is that inserting or deleting objects at the specified index location is slow.

? The LinkedList class uses a linked list structure to hold objects. This structure is a bit easier to insert and delete objects into the collection, you need to insert objects into the collection, and when you delete objects, it is more efficient to use the LinkedList class to implement the list collection, but for random access to objects in the collection, using the LinkedList class to implement the list collection is less efficient.

Set interface: The objects in the set set are not sorted in a particular way, but simply add the object to the collection, but the Set collection cannot contain duplicate objects, and the set set has a set interface and a set interface implementation class composition. The set interface inherits the collection interface, so there are many ways to include the collection interface.

The classes commonly implemented by the set interface are: Hashset and TreeSet classes

? The HashSet class implements the set interface, which is supported by a hash table (actually a hashmap instance). It does not guarantee the set iteration order, especially because he does not guarantee that the order is immutable, and that this class allows empty elements

? The TreeSet class not only implements the set interface, but also implements the Java.util.SortedSet interface, and the set set of the TreeSet class is pressed when the set is traversed. Ascending sort by natural order or ascending by a specified comparer

Map collection:

The map interface provides an object that maps key to value, a key can only map one value, and key cannot be duplicated

The common implementation classes of the map interface are HashMap and TreeMap, and it is generally recommended to implement the Map collection using the HashMap class, because the HashMap class implements the Map collection to add and delete mappings efficiently.

? The HashMap class is an implementation of a hash table-based map interface that provides all the optional mapping operations and allows nulls and null keys to be used, but the key uniqueness must be guaranteed, and HashMap is quickly found by a hash table for its internal mapping relationships. This class does not guarantee the order of the mappings and does not guarantee that the order is constant.

? The TreeMap class not only implements the map interface, but also implements the Java.util.SortedMap interface, the mapping relation of the set has a certain order, but the treemap is worse than HashMap class when adding, deleting and locating relations. Because the mappings in the map collection implemented by the TreeMap class are arranged according to key objects in a certain order, the key object is not allowed to be null


methods used by ArrayList :

import java.util.*;
public class Arraylistdemo {
public static void Main (string[] args) {
list<string> al = new Arraylist<string> ();
al.add ("AAA");
al.add ("BBB");
al.add ("CCC");
System.out.println ("The size of the collection is:" +al.size ());//Outputs 3
//traversal with Iterator
Iterator it = Al.iterator ();
while (It.hasnext ()) {
String str = (String) it.next ();//Because It.next returns an object, it must be strong to
System.out.println (str);
}
}
}

Methods used by HashMap:

public class Hashmapdemo {
public static void Main (string[] args) {
Map<string,string> HM = new hashmap<string,string> ();
Hm.put ("A", "123");
Hm.put ("B", "456");
Hm.put ("C", "789");
SYSTEM.OUT.PRINTLN (HM);//print: {b=456, c=789, a=123}

Get key--value
set<entry<string,string>> set = Hm.entryset ();
Iterator<entry<string, string>> it = Set.iterator ();
while (It.hasnext ()) {
entry<string, string> str = It.next ();
System.out.println ("Key:" +str.getkey () + "value:" +str.getvalue ());
}
}
}



Java Collection: Introduction to Collection and map

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.