Generic Collectiona generic collection does not refer to a collection, but rather to the addition of generics on the basis of the collection.
In a generic collection, once the generic parameter "Class A" is passed in, only objects of Class A or class A subclasses can be added to the collection, and no other object can be added.
When you get an object from a generic collection, the object's type is Class A, not an object of type objects.
Generic ListFor example: The two list sets mentioned above can be added to a generic
Package Collection.list.arraylist;import Java.util.arraylist;import java.util.list;/** * Demonstrates the use of generic collections * @author Genius Federation-Yukun * * public class Genericlistdemo {public static void main (string[] args) {//create object Father f = new Father (); Son s = new Son ();/******************* the collection ********************///the parent class list references to subclasses ArrayList objects, polymorphic applications List List = new ArrayList ();//Add the two objects created above to the collection//any object can join List.add (f); List.add (s);//You must force type conversion Father f1 = (Father) When you want to use members that are specific to the object List.get (0); F1.work (); son S1 = (son) list.get (1); S1.play ();/******************* uses the generic collection ********************/system.out.println ("-------- ---------------------"); */* Create generic collection objects, you can use ArrayList, or you can use LinkedList * Note: * 1, generics can only use reference types and cannot use basic data types * For example: List<int > Is wrong * 2, list<father> and arraylist<father> exist inheritance relationships * but arraylist<father> and arraylist<son> is completely different between the two types * and there is no inheritance relationship * For example:list<father> fatherlist = new arraylist<son> (); * arraylist<father> fatherlist = new arraylist<son> (); * It's all wrong */list<father> fatheRList = new arraylist<father> (); list<son> sonlist = new arraylist<son> ();//After using generics, the Fatherlist collection can only store father type// No type other than father can be added to the collection fatherlist.add (f);//After using generics, the Sonlist collection can only store the son type Sonlist.add (s);//You do not have to force type conversions when you get an element father F2 = Fatherlist.get (0); F2.work (); Son s2 = sonlist.get (0); S2.play ();}} Class father{public void Work () {System.out.println ("working");}} Class Son extends father{public void Play () {System.out.println ("Play");}} Running results: Work and play-----------------------------work and play
Generic Set
Package Collection.set.hashset;import java.util.hashset;import java.util.set;/** * Demo Generic Set Collection * * @author Genius Federation-Yukun * * public class Genericsetdemo {public static void main (string[] args) {//Create HashSet object set<string> set = new Hashset<s Tring> ();//generic specifies that only string types can be added, so it is not possible to add other types of element Set.add ("Zhang San"), Set.add ("John Doe"), Set.add ("Zhang San"), or//using a generic string type, So the generic collection set here is the string type for (string str:set) {System.out.println (str);}}} Running Result: Li Shizhang
Generic Map
Package Map.hashmap;import Java.util.hashmap;import java.util.map;/** * Demo add generic after map collection * * When using multiple generics, The placeholder is separated by a comma in English format * For example:map<k,v> * is used in order to correspond to the location of the incoming * For example:map<string,integer> * All K in its Map class will be replaced by String * All v will be replaced with an integer * when you want to store the value in the collection is also the corresponding data type * * @author Genius Federation-Yukun */public class Genericmapdemo {public static void main (STR Ing[] (args) {//create generic mapmap<string,integer> map = new hashmap<string,integer> ();/* * Because generics are used when creating object map * So add value can only add the corresponding data type value */map.put ("Zhang San"); Map.put ("John Doe", "23"), Map.put ("Harry"); */* * Get the value corresponding to the value of the key * use generics without forcing type conversion */ int age = Map.get ("Zhang San"); System.out.println ("Zhang San's age is:" + Ages);}} Operation Result: Age of Zhang San is: 18
Generic Collection SummarySyntax Format: Collection class name a< class name B>
Function: Only data of type B can be stored in collection a
Declaring and creating an object:list<string> List = new arraylist<string> ()
Note: 1, Collection < parent class > and Collection < subclasses > are incompatible two types
2, Collection < parent class > object, can store child class object;
3. The element type in a generic collection can only be a generic specified type or its subclasses
4. The type of the element to be removed from the generic collection is the type of the generic type
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
I genius official free Tutorial 34: Generic collection of Java collection framework