27. Using Generic Collections

Source: Internet
Author: User

/*** Pet, Dog and Penguin parent class*/ Public Abstract classPet {protectedString name = "Anonymous";//Nickname    protected intHealth = 100;//Health Value    protected intLove = 0;//Degree of intimacy         Public Abstract voidEat ();//abstract method Eat (), responsible for the pet meal function.         /*** Non-parametric construction method. */     PublicPet () {}/*** A method for constructing a parameter. * @paramName Nickname*/     PublicPet (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public intGethealth () {returnHealth ; }     Public intGetlove () {returnLove ; }    /*** Output pet information. */     Public voidprint () {System.out.println ("The Confession of the pet: \ n My name is" + This. Name + ", health value is" + This. Health + ", and the intimacy of the host is" + This. Love + ". "); }}
/*** Doggy class, pet subclass. */ Public classDogextendsPet {PrivateString strain;//Variety    /*** A method for constructing a parameter. * @paramName Nickname *@paramStrain Varieties*/     PublicDog (string name, string strain) {Super(name);  This. Strain =strain; }     PublicString Getstrain () {returnstrain; }    /*** Override the Print method of the parent class. */     Public voidprint () {Super. print ();//Call the Print method of the parent classSystem.out.println ("I am a" + This. Strain + ". "); }    /*** To achieve a meal method. */     Public voideat () {Super. Health =Super. Health + 3; System.out.println ("Dog" +Super. Name + "enough to eat!" Health value increased by 3. "); }}
Importjava.util.ArrayList;Importjava.util.List;ImportCn.jbit.epet.poly.Dog;/*** Test for list application generics*/ Public classtestlist { Public Static voidMain (string[] args) {/*1. Create Multiple Dog objects*/Dog Ououdog=NewDog ("Europe", "Shirley"); Dog Yayadog=NewDog ("Asia-Asia", "Labrador"); Dog Meimeidog=NewDog ("Meimei", "Shelly Rui")); Dog Feifeidog=NewDog ("Feifei", "Labrador"); /*2. Create ArrayList collection objects and put multiple dog objects into them*/List<Dog> dogs =NewArraylist<dog> ();//tag Element typeDogs.add (Ououdog);        Dogs.add (Yayadog);        Dogs.add (Meimeidog); Dogs.add (2, Feifeidog);//add Feifeidog to a specified location//dogs.add ("Hello");//A compilation error occurred and the element type is not dog.         /*3. Display information for the third element*/Dog Dog3= Dogs.get (2);//no type casts requiredSystem.out.println ("The third dog's message is as follows:")); System.out.println (Dog3.getname ()+ "\ T" +Dog3.getstrain ()); /*4. Using the foreach statement to traverse the Dogs object*/System.out.println ("\ nthe information for all dogs is as follows:");  for(Dog dog:dogs) {//no type casts requiredSystem.out.println (Dog.getname () + "\ T" +Dog.getstrain ()); }       }}
ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set;ImportCn.jbit.epet.poly.Dog;/*** Test apply generics to map * */ Public classTestMap { Public Static voidMain (string[] args) {/*1. Create Multiple Dog objects*/Dog Ououdog=NewDog ("Europe", "Shirley"); Dog Yayadog=NewDog ("Asia-Asia", "Labrador"); Dog Meimeidog=NewDog ("Meimei", "Shelly Rui")); Dog Feifeidog=NewDog ("Feifei", "Labrador"); /*2. Create a Map collection object and put multiple dog objects into it*/Map<String,Dog> dogmap=NewHashmap<string,dog>();        Dogmap.put (Ououdog.getname (), Ououdog);        Dogmap.put (Yayadog.getname (), Yayadog);        Dogmap.put (Meimeidog.getname (), Meimeidog);        Dogmap.put (Feifeidog.getname (), Feifeidog); /*3. Output the information of all the dogs in the collection by Iterators*/System.out.println ("Using iterator traversal, all dogs ' nicknames and breeds were:"); Set<String> Keys=dogmap.keyset ();//Take out the collection of all keysIterator<string> It=keys.iterator ();//get Iterator Object         while(It.hasnext ()) {String key=it.next ();//Remove KeyDog Dog=dogmap.get (key);//remove the corresponding value according to keySystem.out.println (key+ "\ T" +Dog.getstrain ()); }        /*//Use the foreach statement to output information for all dogs in the collection for (String Key:keys) {dog dog=dogmap.get (key);            Remove the corresponding value according to key System.out.println (key+ "\ T" +dog.getstrain ()); }*/            }}

27. Using 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.