Understanding of Java Generic Collections

Source: Internet
Author: User
Tags map class

Transferred from: http://shuyangyang.blog.51cto.com/1685768/103076

————————————————————————————————————

What is a generic type?

Generics (Generic type or Generics) are an extension of the Java language type system to support the creation of classes that can be parameterized by type . You can think of a type parameter as a placeholder for the type that you specify when you use a parameterized type, as if the form parameter of the method is a placeholder for the value passed at run Time.

The map class allows you to add objects of any class to a map, even if the most common scenario is to save an object of a particular type (such as a String) in a given map (map).

Because Map.get () is defined to return an Object, it is generally necessary to cast the result of Map.get () to the desired type, as shown in the following code:

    1. Map m = new HashMap ();
    2. M.put ("key", "value");
    3. string result = (string) M.get ("key");

In order for the program to compile, the M.get ("key") must be cast to the string type because the string type value is placed here, and the result must be of type String. If you put a value in the map that is not of type string, the classcastexception error will be Reported.

ideally, you might come to the view that M is a map that maps a string key to a string Value. This allows you to eliminate coercion of type conversions in your code and to obtain an additional type-checking layer that prevents someone from saving the key or value of the wrong type in the collection . This is the work that generics do.

Benefits of Generics

Type safety

The type of the variable definition is Restricted.

Eliminate forced type conversions

Or take the example that you just said

    1. map<string, string> m = new hashmap<string, string> ();
    2. M.put ("key", "value");
    3. String result = M.get ("key");

The code above is not cast less and the code looks more concise, which is one of the benefits of Generics. Usually when you write, the editor will have a yellow exclamation point to remind you that you need to use Generics.

wildcard character of generic type

? Wildcard characters

Can I use wildcards to refer to various other parameterized types? a wildcard-defined variable is primarily used as a reference, and you can call methods that are not parameterized, and cannot invoke methods related to Parameterization.

Let's look at an example:

  1. public class People {
  2. Private String name;
  3. Private String job;
  4. Public people () {
  5. }
  6. Public people (string _name, string _job, int _sex) {
  7. THIS.name = _name;
  8. This.job = _job;
  9. This.sex = _sex;
  10. }
  11. public void SayHello () {
  12. SYSTEM.OUT.PRINTLN ("i am a" + job + "; My name is" + name ");
  13. }
  14. Public String getName () {
  15. Return name;
  16. }
  17. public void SetName (String Name) {
  18. THIS.name = name;
  19. }
  20. Public String getjob () {
  21. Return job;
  22. }
  23. public void Setjob (String Job) {
  24. This.job = job;
  25. }
  26. public int Getsex () {
  27. Return sex;
  28. }
  29. public void Setsex (int Sex) {
  30. This.sex = sex;
  31. }
  32. private int sex;
  33. }

    1. public class Softwareengineer extends people{
    2. public void Coding () {
    3. System.out.println ("coding ...");
    4. }
    5. }

    1. Public class Cook extends people {
    2. public void Cook () {
    3. System.out.println ("cooking ...");
    4. }
    5. }

Main Method:

    1. list<? Super people> flist = new Arraylist<people> ();
    2. Flist.add (new People ());
    3. Softwareengineer soft = (softwareengineer) Flist.get (0);
    4. Soft.coding ();

Output result: Coding ...

Note that this is super, so add new people () to Flist.add (), or add new Softwareengineer () or new Cook (), either!

well, Let's take a look at the Following:

    1. list<? Extends people> flist = new Arraylist<people> ();

You can not add the ...?

Why can't I join the subclasses of the people class and the people class in the list because of this:

list<? Extends people> indicates that the upper limit is people, and the following assignments are legal

    1. list<? Extends people> list1 = new Arraylist<people> ();
    2. list<? Extends people> list2 = new Arraylist<softwareengineer> ();
    3. list<? Extends people> list3 = new Arraylist<cook> ();

If list<? Extends people> the method of supporting 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's subclasses

In that case, the problem will Arise.

list<? Extends people> should hold the object is the subclass of people, and specifically which subclass is still an unknown, so adding any people subclasses will have a problem, because if add people, maybe list<? Extends people> holds the object is new arraylist<cook> () Softwareengineer The addition is certainly not possible, if add Cook, may list< Extends people> holds a subclass of new Arraylist<cook > ()

Softwareengineer's accession is not legal, so list< Extends people> list cannot be add, and list<? The Super People> list indicates that the list holds an object that is the parent of people, and the lower bound is people, so there is no problem with the subclass of add people or people in the List.

Understanding of Java Generic Collections

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.