Why use the collection framework?
Parsing: You can use the Java Collection Framework If you do not know how many objects are required when the program is running, or if you need to store the objects in a more complex way .
If you enable the Delete method for a collection, the index of all elements in the collection is maintained automatically.
The collection completely compensates for the defect of the array.
the contents of the collection framework
The collection framework contains three chunks of content: external interfaces, interface implementations, and algorithms for set operations
01. Interface: An abstract data type that represents a collection
02. Implementation: The implementation of the interface in the set framework
03. Algorithm: A useful calculation method is done on an object that implements the interface of a set frame
Java Collection Framework diagram:
01.Collection interface exists to store a set of not unique (allow duplicates), unordered objects
The 02.Set interface inherits the collection interface, storing a unique set of objects that are not allowed to be duplicated, unordered
The 03.List interface inherits the collection interface, storing a set of objects that are not unique (allow duplicates), ordered (placed elements in the order in which elements are inserted, and are not rearranged)
The 04.Map interface stores a pair of key-value objects that provide a mapping of the key (key) to value. The key in the map does not require ordering and does not allow duplication. Value also does not require ordering, but allows repetition.
The 05.Iterator interface is the interface responsible for defining the access and traversal elements.
Here's how to start:
1.List interface
List interface Inheritance Collectio n Interface, which stores a set of objects that are not unique (allow duplicates), ordered (placed elements in the order in which elements are inserted, and are not rearranged)
Common classes that implement the list interface are ArrayList and LinkedList
ArrayList arrays are encapsulated to achieve a variable length array
LinkedList uses a linked list storage, the advantage is that the insertion, the removal of elements of high efficiency
01.ArrayList Collection Class
Packagecn.day001; Public classPenguin {PrivateString name= "Anonymous"; PrivateString sex= "Q Tsai"; PublicPenguin () {} PublicPenguin (string name, string sex) { This. Name =name; This. Sex =sex; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } }
Test class:
Packagecn.day001;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;//on machine 1 ArrayList collection class Public classTest { Public Static voidMain (string[] args) {//1. Create multiple Penguin ObjectsPenguin penguin=NewPenguin ("Nan nan", "Q Tsai"); Penguin penguin2=NewPenguin ("Flower", "Q sister")); Penguin penguin3=NewPenguin ("Zie Zhe", "Q sister"); //2. Create a ArrayList collection object and put 2 penguin objects in itList<penguin> penguins=NewArraylist<penguin>(); Penguins.add (Penguin); Penguins.add (penguin2); Penguins.add (penguin3); //3. Number of penguins in the output setSYSTEM.OUT.PRINTLN ("Total has" +penguins.size () + "Penguin only"); //4. By iterating through the collection display for(inti = 0; I < penguins.size (); i++) {Penguin pg=(Penguin) penguins.get (i); System.out.println (Pg.getname ()+ "\ T" +pg.getsex ()); } //5. Delete the Zie Zhe penguin in the collectionPenguins.remove (penguin3); //6. Determine if there are Zie Zhe penguins in the collection if(Penguins.contains (penguin3)) {System.out.println ("Presence of Zie Zhe penguins"); }Else{System.out.println ("There is no Zie Zhe penguin."); } System.out.println (); //7. Iterating with iteratorSystem.out.println ("Traverse with iterator"); Iterator<Penguin> its=Penguins.iterator (); while(Its.hasnext ()) {Penguin pg=Its.next (); System.out.println ("First Name" +pg.getname () + "\ t sex" +pg.getsex ()); } System.out.println (); //8. Using the foreach traversalSystem.out.println ("Use foreach traversal"); for(Penguin pg:penguins) {System.out.println ("First Name" +pg.getname () + "\ t sex" +pg.getsex ()); } }}
02.LinkedList Collection Class
Packagecn.day001;Importjava.util.LinkedList;//several special methods for testing LinkedList Public classTest2 { Public Static voidMain (string[] args) {//1. Create multiple Penguin ObjectsPenguin penguin=NewPenguin ("Nan nan", "Q Tsai"); Penguin penguin2=NewPenguin ("Flower", "Q sister")); Penguin penguin3=NewPenguin ("Zie Zhe", "Q sister"); //2. Create a ArrayList collection object and put 2 penguin objects in it//list<penguin> penguins=new arraylist<penguin> ();Linkedlist<penguin> penguins=NewLinkedlist<penguin>(); Penguins.add (Penguin); Penguins.add (penguin2); Penguins.add (penguin3); //3. Number of penguins in the output setSYSTEM.OUT.PRINTLN ("Total has" +penguins.size () + "Penguin only"); //4. View the nickname of the first Penguin in the collectionPenguin firstpenguins=Penguins.getfirst (); System.out.println ("The first Penguin's nickname is:" +firstpenguins.getname ()); //5. View the nickname of the last Penguin in the collectionPenguin lastpenguins=Penguins.getlast (); System.out.println ("The last penguin's nickname is:" +lastpenguins.getname ()); //6. Delete the first penguin and the last PenguinPenguins.removefirst (); Penguins.removelast (); System.out.println ("Number of Penguins after deletion" +penguins.size ()); }}
2.Map interface
01.HashMap Collection Class
Packagecn.day002;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Scanner;ImportJava.util.Set;//on machine 2 Find pets based on pet nickname 3 using iterator iterative traversal Public classTest { Public Static voidMain (string[] args) {Map<string, string> pet=NewHashmap<string, string>(); Pet.put ("Hehe", "dog"); Pet.put ("Haha", "Penguin"); Pet.put ("Hehe", "Cat Cat"); Scanner input=NewScanner (system.in); System.out.println ("Please enter your pet nickname:"); String name=Input.next (); if(Pet.containskey (name)) {String PetName=pet.get (name); SYSTEM.OUT.PRINTLN (Name+ "corresponding Pet" +petname); }Else{System.out.println ("Sorry, no pets for this nickname"); } System.out.println (); //Iterating with iterator iterationsSystem.out.println ("Traverse with iterator"); Set<String> Keys=pet.keyset ();//Remove the value of all keysIterator<string> It=keys.iterator ();//Get Iteratoer Object while(It.hasnext ()) {String key= (String) it.next ();//Remove key valueString Pets=pet.get (key);//remove the corresponding value according to keySYSTEM.OUT.PRINTLN ("Key value:" +key+ "\tvalue value:" +pets); } }}
The collection framework for Java