I genius official free Tutorial 34: Generic collection of Java collection framework

Source: Internet
Author: User
Tags map class

Generic Collection

A 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 List

For 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;/** *   Demonstrating 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 ();/*******************  does not use a generic collection  ********************/// The reference to the parent list refers to the subclass ArrayList object, the polymorphic application list list = new arraylist ();//Add the two objects created above to the collection// Any object can be joined List.add (f); List.add (s);//To use a member specific to the object, the type conversion father f1 =  (Father)  list.get (0) must be enforced; F1.work (); son s1 =  (Son)  list.get (1); S1.play ();/*******************  used a generic collection  ************* /system.out.println ("-----------------------------");/* *  create generic collection objects, you can use ArrayList, You can also use linkedlist *  Note:  * 1, generics can only use reference types, cannot use basic data types  *  such as:list<int> is the wrong  *  2, List<father> and aRraylist<father> There are inheritance relationships  *  but arraylist<father> and arraylist<son> are two different types  *  There is also no inheritance relationship  *  such as:list<father> fatherlist = new arraylist<son> ();  *  ArrayList<Father> fatherList = new ArrayList<Son> (); *  are all wrong.  */List<Father> fatherList = new ArrayList<Father> (); List<son> sonlist = new arraylist<son> ();//After using generics, the Fatherlist collection can only store father types// 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 need to force type conversions when fetching elements father  f2 = fatherlist.get (0); F2.work (); Son s2 = sonlist.get (0); S2.play ();}} Class father{public void work () {System.out.println ("work");}} 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 */PUBL IC class Genericsetdemo {public static void main (string[] args) {//Create HashSet object set<string> set = new Hashset<strin G> ();//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 middle of the placeholder is separated by a comma in English format * For example: Map<k,v> * Use is also in the order of the location of the incoming * For example:map<string,integer> * All K in its Map class will be replaced with a String * All V will be replaced with an Integer * When you want to store the value in the collection is also to correspond to the data type * * @author Genius Federation-Yukun */public class Genericmapdemo {public static void main (string[] args) {//Create generics MAPM ap<string,integer> map = new hashmap<string,integer> ();/* Because generics are used when creating the object map, only the corresponding data type value is added when the value is added */map.put ( "Zhang San", Map.put ("John Doe"); Map.put ("Harry", 23);/* * Gets 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 Summary

Syntax 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

This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius official free Tutorial 34: Generic collection of Java collection framework

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.