Set exercise-Map part, set map part

Source: Internet
Author: User

Set exercise-Map part, set map part

Exercise:

Enter the name of the poem to query the content of the poem. When you enter exit, exit the program, such as Chunxiao, sleep, and goose ".

 

package CollectionPart;public class Poetry {        private String title;    private String poet;    private String content;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getPoet() {        return poet;    }    public void setPoet(String poet) {        this.poet = poet;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    @Override    public String toString() {        return "Poetry [title=" + title + ", poet=" + poet + ", content=" + content + "]";    }    public Poetry(String title, String poet, String content) {        super();        this.title = title;        this.poet = poet;        this.content = content;    }    public Poetry() {        super();    }}
Package CollectionPart; import java. util. hashMap; import java. util. map; import java. util. summary; public class Map1 {public static void main (String [] args) {Poetry p1 = new Poetry ("Chunxiao", "Meng Haoran", "chunmian doesn't know, it's everywhere. The amount of rain and rain in the night is unknown. "); Poetry p2 = new Poetry (" quiet night thinking "," Li Bai "," Moonlight in front of the bed, suspected to be frost on the ground. Look up at the moon and look down at your hometown. "); Poetry p3 = new Poetry (" wing goose "," Luo binwang "," goose, Qu Xiang Tian ge. White hair float green water, red palm dial clear wave. "); Map <String, Poetry> myMap = new HashMap <String, Poetry> (); myMap. put (p1.getTitle (), p1); myMap. put (p2.getTitle (), p2); myMap. put (p3.getTitle (), p3); sums in = new loads (System. in); String string = null; do {System. out. println ("enter the name of the poem to be searched:"); String name = in. nextLine (); findThePoem (myMap, name); System. out. println ("Continue search? "); String = in. nextLine ();} while (string. equals ("yes");} private static void findThePoem (Map <String, Poetry> myMap, String name) {if (myMap. containsKey (name) {System. out. println ("Poetry content:"); System. out. println (myMap. get (name ). getContent ();} else {System. out. println ("the poem does not exist, or the collection is incomplete. ");}}}

 

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.