Overview
A container is a collection of stored data
(Java provides an interface specifically to constrain the implementation class of our container)
Set:does not allow duplicate objects toenter the collection of elements
Sortedset:similar to set except this theelements in the set is stored in ascending order
List:is ordered, maintain an order asobjects is added and removed from the Collection,can also contain duplicateentries of objects
Map:stores objects that is identifiedby unique keys, and may not store duplicate keys
Sortedmap:similar to Map, except the objectsare stored on ascending order according to their keys
That
? Collection interface: Defines a method for accessing a set of objects, whose sub-interface set and list define the storage method separately.
The data objects in the –set are not sequential and cannot be duplicated.
The data objects in the –list are in order and can be duplicated.
The –map interface defines a method that stores the key (key)-value mapping pair.
Collection Method
? Collection represents a set of objects, which are concentrated, and the meaning of collection is to collect some data.
? The collection function library is a number of interfaces and classes under the Java.util package that are used to generate objects for storing data, while interfaces are the way to access data.
? The collection function library differs from the two points of an array:
1. The capacity of the array is limited, and the collection library has no such limit, its capacity can be automatically adjusted.
The 2.Collection library can only be used to hold objects, and arrays have no such limitations.
? The collection interface is the root interface in the collection hierarchy, which defines some of the most basic access methods, allowing us to access the data in a uniform way through it or its sub-interfaces.
? Difference: Collection represents a set of objects, the collection function library is the collection framework in Java, collection interface, is the root interface in this collection framework.
? The data stored in the collection library is called the element.
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >public class Person {private IntID; Private Stringname; public person (int ID, String name) {this. id= ID; This. name = name; } public Intgetid () {return id; } public Stringgetname () {return name; } public voidsetid (int id) {this. id= ID; } public Voidsetname (String name) {this.name = name; } public stringtostring () {return "ID:" + ID + "|name:" + name; }}========================================================================import java.util.*;p Ublic class CollectionTest1 {public staticvoid main (string[] args) {collectionc = new HashSet (); C. Add (new person (1, "C + +")); C. Add (New person (2, "Java")); System.out.println (c. Size () + ":" + C); System.out.println ("contains:" + C. Contains (new person (2, "Java")); System.out.println (c. Remove (new person (2, "JAVA "))); System.out.println (c. Size () + ":" + C); }}</span></span>
Output Result:
Iterator Interface
? All container classes that implement the collection interface have a iterator method that returns an object that implements the iterator interface.
? The iterator object is called an iterator to facilitate the traversal of elements within the container.
? The iterator interface defines the following methods:
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class IteratorTest1 {public staticvoid main (string[] args) { collectionc = new ARR Aylist (); C.add ("good"); C.add ("Morning"); C.add ("key"); C.add ("Happy"); for (Iterator it = C.iterator (); It.hasnext ();) { Stringtem = (String) it.next (); if (Tem.trim (). Length () <= 3) { it.remove (); } } System.out.println (c); }} </span></span>
Set Method
? The set interface is a sub-interface of the collection interface, and the set interface does not provide an additional method, and the set interface is characterized by the fact that the elements in the container class are not sequential and cannot be duplicated.
? The set container can correspond to the concept of "set" in mathematics.
? The Set container classes provided in the J2SDK API are Hashset,treeset and so on.
Demo
<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class Settest {public staticvoid main (string[] args) { Set s =new HashSet (); S.add ("Hello"); S.add ("World"); S.add (New Integer (4)); S.add (new Double (1.2)); S.add ("Hello"); The same element will not be added to System.out.println (s); }} </span>
List interface
? The list interface is a sub-interface of the collection, and the elements in the container class that implement the list interface are ordered and can be duplicated.
? The elements in the list container correspond to the ordinal of an integer that records its position in the container, and the elements in the container can be accessed according to the sequence number.
? The List container class provided by J2SDK has arraylist,linkedlist and so on.
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class Listtest {public staticvoid main (string[] argc) { List l1= new ArrayList (); C2/>for (inti = 0; I <= 5; i++) L1.add ("a" + i); System.out.println (L1); List.add (3, "A100"); System.out.println (L1); List.set (6, "a200"); SYSTEM.OUT.PRINTLN (list); System.out.print (String) List.get (2) + ""); System.out.println (List.indexof ("A3")); List.remove (1); SYSTEM.OUT.PRINTLN (list);} } </span></span>
Common Algorithms
Class Java.util.Collections provides some static methods to implement some common algorithms based on the list container .
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class Collectionstest { publicstatic void Main (string[] argc) { List alist =new ARR Aylist (); for (int i = 0;i < 5; i++) Alist.add ("a" + i); System.out.println (alist); Collections.shuffle (alist);//random arrangement System.out.println (alist); Collections.reverse (alist);//Inverse continuation System.out.println (alist); Collections.sort (alist);//Sort System.out.println (alist); System.out.println (Collections.binarysearch (alist, "A2")); Collections.fill (alist, "Hello"); System.out.println (alist); }} </span></span>
Comparable interface
Question: What determines the "size" order of the objects in the collection based on what the above algorithm does?
? All classes that can be "sorted" implement the Java.lang.Comparable interface, with only one method in the comparable interface
public int compareTo (objectobj);
This method:
? A class that implements the comparable interface determines how the class objects are sorted by implementing the Comparato method.
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >public class Student implements comparable {private stringname; Private Integerscore; Publicstudent (String name, int score) {this.name =name; This.score =new Integer (score); } public Intcompareto (Object o) {Student n = (Student) o; int a =score.compareto (n.score); Return (a!=0 a:name.compareto (n.name)); } public stringtostring () {return "name:" + name + "Score:" + Score.tostring (); }}import java.util.*;p ublic class Studenttest {public static voidmain (string[] args) { List L1 = newlinkedlist (); L1.add (Newstudent ("TTT", 66)); L1.add (Newstudent ("BBB", 77)); L1.add (Newstudent ("CCC", 99)); L1.add (Newstudent ("FFF", 88)); L1.add (Newstudent ("AAA", 66)); System.out.println (L1); Collections.sort (L1); System.out.println (L1); }}</span></span>
Map Interface
? The class that implements the map interface is used to store key (key)-value pairs.
? The implementation classes of the Map interface are HashMap, TreeMap, and so on.
? Key-value pairs stored in the map class are identified by key, so the key value cannot be duplicated.
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class Maptest {public staticvoid main (String args[]) { Map M1 =new HashMap (); Map m2 =new TreeMap (); M1.put ("One", New Integer (1)); M1.put ("n", New Integer (2)); M1.put ("Three", New Integer (3)); M2.put ("A", New Integer (1)); M2.put ("B", New Integer (2)); System.out.println (M1.size ()); System.out.println (M1.containskey ("one")); System.out.println (M2.containsvalue (Newinteger (1))); if (M1.containskey (")") { inti = ((Integer) M1.get ("a"). Intvalue (); System.out.println (i); } Map M3 =new HASHMAP (M1); M3.putall (m2); System.out.println (M3.size ());} } </span></span>
generic type
The purpose of generics is to constrain the type of objects you pass in;
In layman's terms: pass in what, take out what.
? Cause:
–jdk1.4 previous types are ambiguous:
? The type of the mounted collection is treated as an object, thus losing its actual type.
? When removed from the collection, it is often necessary to transform, inefficient and prone to errors.
? Workaround:
– Define the type of the objects in the collection at the same time as the collection
? You can specify the collection when defining the
? You can also specify it with iterator in the loop
? Benefits:
– Enhanced readability and stability of the program
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class MapTest1 {public staticvoid main (String args[]) { Map<string,integer > m = new hashmap<string, integer> (); for (int i = 0; i < 5; i++) { m.put (string.valueof (i), 1); } for (int i = 0; i < 5; i++) { m.put (string.valueof (i), 1); } System.out.println (m.size () + "distinct words detected:"); System.out.println (m); Set<string>set = M.keyset (); Iteratorit = Set.iterator (); while (It.hasnext ()) { System.out.println (M.get (It.next ()));}}} </span></span>
Enhanced for Loop
? The enhanced for loop is fairly simple for traversing an array or collection
? Defects:
Array
? Unable to easily access subscript values
Collection
? It is not easy to delete the contents of the collection compared to using iterator
? Summarize:
– In addition to simply traversing and reading out the contents, it is not recommended to use the enhanced for
Business Ideas
This interface, provided by Java, specifically constrains the implementation classes of our containers, greatly reducing the burden on our designers and improving efficiency.
The summary of the container class is relatively scattered knowledge points, it is difficult to complete the narrative out, in the continuous use, you can get a better understanding.
[Java Primary]:19. Container class