Java Generics issues

Source: Internet
Author: User

  1. Public St-atic void Main (string[] aRGS)
  2. {   
  3. List StrList = new ArrayList();   
  4. StrLis T.add ("one");
  5. StrLis T.aDD ("a");
  6. }   
  7.   
  8. I believe a lot of people have experienced this kind of writing, yes, it is not wrong. We've even done it before. But is it not about keeping pace with the times? This way of writing to today, but there is a problem. The problem arises from our needs, and what we need is an L-is T, which is dedicated to storing strings, now add a sentence to this code to make it look like this:

  9. Public St-atic void Main (string[] aRGS)
  10. {   
  11. Lis t StrLis t = new    ArrayList();
  12. StrLis T.add ("one");
  13. StrLis T.aDD ("a");
  14. StrLisT. a  DD (new IntegeR (1));          
  15. }   
  16.   
  17. then run a check and you can run! It's good news, don't be happy, the problem is thatthis is T works (remember: Programmers don't require programs to run). Looking back, our demand is that this ist dedicated to storing strings, but for now, this L-is T can even store an integer data. Perhaps you would say that the Jav class library is defined not to be able to store any one of the obj-ect? Yes, yes, but now our range of requirements has become smaller, and we just need to store strings. Don't say I'm a dead end, we see a more general problem: when we operate the database, we often return all the data of a table, after we have mapped through the entity relationship, we get a series of the same type of data, usually we take the action is to use an L is T to save this series of data, but since the data all have the same type, the L is Talso becomes an L-is t that holds a single object.    Seeing here, I think we should be clear about the purpose of my example.

  18. Now let's talk about the solution to the problem, one sentence to summarize is that we need a single type of L is T. The method is here, Java 5 gives us a solution-----Generic, for our example, Java   5 of the type safety to play a role.

  19. now we go back to the program clear single, open your development tools, compile (eclipse and other tools without your own mutation can see), we found that the program although there is no error, but there is a warning:

  20. Type safety:the method add (OBJect) be longs to the raw type Lis T.
  21. References to generic type Lis T should be parameterized

  22. See, JavaTigeR gives you a warning that the source of the warning is the type safety described above. If you query Javadoc, you will find that Lis t defined as:

  23. public interface Lis t < E > exte NDS Collection,iterable
  24.   
  25. Watch this . < E >          , it is the type safety mark of JavaTigeR, and I think that here we can give a definition of how L is T in ourexample when it comes to ensuring type safety:

  26. Lis t < String > StrLis t = new ArrayList<    String>();

  27. before you understand this statement, remember that this is the official notation ofL is t (^_^). Now look, after L ist we have <String> such a flag in JaV a TigeR Inside, this is the type-safe definition method, combined with this statement, it is easy to see that we have defined an L is T to storea string type. Take a look at the redefined effect:

  28. Public St-atic void Main (string[] aRGS)
  29. {   
  30. Lis t < String > StrLis t = new ArrayList<    String>();
  31. StrLis T.add ("one");
  32. StrLis T.aDD ("a");
  33. }   
  34.   
  35. compile it again or see it directly in eclipse , the previous warning is gone.
  36. Here , it should be said that we have mastered the basic use of type safety, and now I have a small change, the definitionof L-T is changed to this:

  37. Lis t < String >    StrLis t = new ArrayList();
  38.   
  39. Oh, the warning came back again. Although we are here to definethe string type for L-T, we do not specify it when we instantiate it, so this warning appears, so remember the official wording.
  40.     
  41. now come back and let's verify that this ensures that the type-safel ' t is not safe. Let's verify by adding an integer datato this L-is t:

  42. Public St-atic void Main (string[] aRGS)
  43. {   
  44. Lis t < String > StrLis t = new ArrayList < String >   ();
  45. StrLis T.add ("one");
  46. StrLis T.aDD ("a");
  47. StrLis T.aDD (new IntegeR (1));
  48. }          
  49.   
  50. it turned out to be a mistake (this is not a warning!) Isn't it amazing? Take a look at the error message:

  51. the method add (String) in The type Lis t not applicable for the arguments (IntE GeR)
  52.   
  53. The effect is obvious that the add method of the L is T can only accept parameters of type string, Javadoc in a DD (OBJE ct obj method has not been used, and now everyone should be very satisfied with it, our demand has reached, the L ist too safe, hehe. Think about the benefits of it, and then when we're going todeal with an element in the middle of an L is T, it's no longer necessary to write:

  54. String Str          = (String) lis t.get (i);
  55.         
  56. with type safety, we can write directly:

  57. String Str = lis T          . Get (i);
  58.   
  59. Maybe it's not enough to satisfy you, but let's look at One way:

  60. Public Lis t < String > geTstrlis t (Lam T<String> lis T)
  61. {   
  62.      .....   
  63. REturn XXX;
  64. }   

  65. This is the power of parameterized types, don't tell me you think this method is nothing special.
  66.   
  67. Well, I think my purpose should have been achieved, we should understand the type of safety Jav 5 what is going on and how to use it, OK, in order to let everyone in the heart more bottom, then write two lines of code let us look:

  68. Map < IntegeR , String > MyMap = new HashMap    <IntegeR , String>();
  69. Set < MYCLaSS > set = new HashSet   <Myclass>(0);

  70. No more examples, these examples are straightforward enough.
  71.   
  72. at the end of this article, let's go back to the procedure to clear a single, have not previously warned? If you are such a person who does not want to use type safety to eliminate the warning, and do not want to see that nasty Waining, you can add such a sentence before the Main method (if you use IDE can be completed automatically):

  73. @SuppressWarnings ("UncheckeD").

  74. To make a program like this:

  75. @SuppressWarnings ("Unchecked")
  76. Public St-atic void Main (string[] aRGS)
  77. {   
  78. Lis t StrLis t = new    ArrayList();
  79. StrLis T.add ("one");
  80. StrLis T.aDD ("a");
  81. StrLis T.aDD (new IntegeR (1));
  82. }   
  83.   
  84. I think even if you are so picky you should be satisfied, there is no type safety, there is no warning. Why? The key is the line statement we added, which is also one of the new features of JavaTigeR annotation: as to what it is, Please pay attention to the following topics.

Java Generics issues

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.