Java generic set

Source: Internet
Author: User
Tags map class

What is generics? Generic (Generic type or generics) is an extension of the Java type system to support the creation of classes that can be parameterized by type. You can regard the type parameter as a placeholder of the type specified when parameterized type is used, just as the method form parameter is a placeholder of the value passed during runtime. The Map class allows you to add objects of any class to a Map, even if the most common case is to save an object of a specific type (such as String) in a given map. Because Map. get () is defined as the returned Object, so Map is usually required. the result of get () is forcibly converted to the expected type, as shown in the following code: Map m = new HashMap (); m. put ("key", "value"); String result = (String) m. get ("key"); to compile the program, you must. get ("key") is forcibly converted to the String type, because the String type value is put here, and the result must be of the String type. If you add a value not of the String type to the map, the ClassCastException error is returned. Ideally, you may come up with the idea that m is a Map that maps the String key to the String value. This allows you to eliminate the forced type conversion in the Code and obtain an additional type check layer, which prevents someone from saving the wrong type keys or values in the set. This is what Generics do. The benefits of generics: type security limits the types defined by variables. Eliminate forced type conversion or use the example above to describe Map <String, String> m = new HashMap <String, String> (); m. put ("key", "value"); String result = m. get ("key"); is the above Code less forced conversion and the code looks much more concise, which is one of the benefits of generics. When you write it, the editor will have a yellow exclamation point to remind you to use generics. Wildcard? Wildcard usage? Wildcards can reference other parameterized types ,? Variables defined by Wildcards are mainly used as references. You can call Methods unrelated to parameterization, rather than methods related to parameterization. Example: public class People {private String name; private String job; public People () {} public People (String _ name, String _ job, int _ sex) {this. name = _ name; this. job = _ job; this. sex = _ sex;} public void SayHello () {System. out. println ("I am a" + job + "; my name is" + name);} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getJob () {ret Urn job;} public void setJob (String job) {this. job = job;} public int getSex () {return sex;} public void setSex (int sex) {this. sex = sex;} private int sex;} public class SoftwareEngineer extends People {public void coding () {System. out. println ("coding ...... ") ;}} Public class Cook extends People {public void cook () {System. out. println (" cooking ...... ") ;}} Main method: List <? Super People> flist = new ArrayList <People> (); flist. add (new People (); SoftwareEngineer soft = (SoftwareEngineer) flist. get (0); soft. coding (); output result: coding ...... Note that this is super. You can add new People () to flist. add () or new SoftwareEngineer () or new Cook! So let's switch to the following: List <? Extends People> flist = new ArrayList <People> (); you cannot add it here ...? Why can't the people class and people class subclasses be added to the list? The reason is: List <? Extends People> indicates that the upper limit is People. The following values are valid lists. <? Extends People> list1 = new ArrayList <People> (); List <? Extends People> list2 = new ArrayList <SoftWareEngineer> (); List <? Extends People> list3 = new ArrayList <Cook> (); If List <? Extends People> If the add method is valid: list1 can add People and all People subclasses list2 can add SoftWareEngineer and all SoftWareEngineer subclasses list3 can add Cook and all Cook subclasses. In this case, the problem arises. <? Extends People> the object to be held is a subclass of People, and the specific subclass is still unknown. Therefore, it is problematic to add any subclass of People, because if you add People, possible List <? Extends People> the object held by new ArrayList <Cook> () SoftWareEngineer cannot be added. If you add Cook, the List may be <? Extends People> the object held by extends is the addition of new ArrayList <Cook subclass> () SoftWareEngineer is invalid, so List <? Extends People> list cannot be added, while List <? Super People> list indicates that the object held by list is the parent class of People, and the lower limit is People. Therefore, the subclass of add People or People in list is normal.

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.