The Java1.5 version introduced generics, although this layer of syntactic sugar gives developers a boost in code reusability, but this is just a layer of syntactic sugar that the compiler makes, which is erased in the real bytecode. I found that many years of experience in the development of programmers, still not good at using Java generics, this article will start from the basic use of Java generics, in the future in a number of blog posts, the use of generics to make a summary. This article does not delve into the implementation of Java generics, but only describes the use of Java generics.
Experiment Preparation
First, you need to create a class inheritance system. In the case of commodities, each commodity has a basic name attribute. In big data applications, data tables and services can be used as commodities, the table has a row number attribute, and the service has method properties, as shown in code Listing 1.
Code Listing 1
classAuction {PrivateString name; PublicAuction (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString toString () {returnname; }}classTableextendsAuction {PrivateInteger RowNum; PublicTable (String name, Integer rowNum) {Super(name); This. RowNum =RowNum; } PublicInteger Getrownum () {returnRowNum; } Public voidsetrownum (Integer rowNum) { This. RowNum =RowNum; } PublicString toString () {return Super. toString () + ", RowNum:" +RowNum; } }classServiceextendsAuction {PrivateString method; PublicService (string name, String method) {Super(name); This. method =method; } PublicString GetMethod () {returnmethod; } Public voidSetmethod (String method) { This. method =method; } PublicString toString () {return Super. toString () + ", Method:" +method; } }Experiment: The most basic use of generics
Now to demonstrate the use of generics, take the example of a commodity. The data table itself is just a table in HBase, and the service is a data service running on top of Hadoop yarn, such as Strom, Spark, or Oozie. Whether it is a data sheet or a service, it is not a commodity itself, to become a commodity, there must be an adorner to wrap it as a commodity, now we use generics to implement a simple adorner, see Listing 2 shows.
Code Listing 2
classDecorator<t> { /*** Description: EXP1 use <br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramitemList*/ Public voidDodecorate (list<t>itemList) { for(inti = 0; I < itemlist.size (); i++) {System.out.println (Itemlist.get (i)); } } /*** Description: EXP1 use <br/> * Author:jiaan.gja<br/> * History: (version) Author time comment <br/> *@paramItemList *@paramT*/ Public voidAdddecorate (list<t>itemList, T t) {Itemlist.add (t); } }
Now we can use decorator to add or print any type to the list, as shown in Listing 3.
Code Listing 3
/*** class Name: exp1<br/> * Description: Generic basic Use <br/> * Author: jiaan.gja<br/> * created: August 13, 2015 <br/> * * History: (version) Author time comment*/classEXP1 { Public Static voidMain (string[] args) {Decorator<Auction> Decoratora =NewDecorator<auction>(); List<Auction> ListA =NewArraylist<auction>(); Auction Auctionone=NewAuction ("Auctionone"); Auction Auctiontwo=NewAuction ("Auctiontwo"); Decoratora.adddecorate (ListA, Auctionone); Decoratora.adddecorate (ListA, auctiontwo); Decoratora.dodecorate (ListA); Decorator<Table> Decoratorb =NewDecorator<table>(); List<Table> Listb =NewArraylist<table>(); Table Tableone=NewTable ("Tableone", 10); Table Tabletwo=NewTable ("Tabletwo", 20); Decoratorb.adddecorate (Listb, Tableone); Decoratorb.adddecorate (Listb, tabletwo); Decoratorb.dodecorate (LISTB); Decorator<Service> DECORATORC =NewDecorator<service>(); List<Service> LISTC =NewArraylist<service>(); Service ServiceOne=NewService ("ServiceOne", "MethodOne"); Service Servicetwo=NewService ("Servicetwo", "Methodtwo"); Decoratorc.adddecorate (LISTC, ServiceOne); Decoratorc.adddecorate (LISTC, servicetwo); Decoratorc.dodecorate (LISTC); }}Legacy issues
If you want Auction for decorator, you must declare decorator<auction>;service for decorator, and you must declare decorator<service>. Since table is a subtype of Auction, we naturally want the table to be used for DECORATOR<AUCTION> as well, as in the following:
Decoratora.dodecorate (LISTB);
Listb's generic table is indeed a subtype of the Decoratora generic auction, but the compiler will give an error stating that this syntax is not allowed.
How to solve this problem? Take a look at the next "Java Generics covariant"
Java generics-Basic use