Java Base array Set MAP collection synthesis application Generate random string "00000001" fruit with 0 item number problem

Source: Internet
Author: User

 Packagecom.swift.test01;/*There are four kinds of fruits (apples, bananas, watermelon, oranges) 1, to each kind of fruit set a product number, product number is 8 0-9 random number, the product number can not be duplicated, the minimum value "00000001", the maximum Value "99999999" 2, according to the product number to query the corresponding product. If the output is not found: "Check this Product" If you can find print: "According to the product number: 12345678, the corresponding product to find: Watermelon"*/ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Map;ImportJava.util.Random;ImportJava.util.Set; Public classShuiguomap { Public Static voidMain (string[] args) {string[] fruits=NewString[] {"Apple", "banana", "watermelon", "orange" }; Set<String> nums =NewHashset<string>();        Getnums (Nums); Map<string, string> map =NewHashmap<>(); inti = 0;  for(String str:nums) {map.put (str, fruits[i++]);        } System.out.println (map); String Strnum= "12568738";    Findfuit (map, Strnum); }    Private Static voidFindfuit (map<string, string>map, String strnum) {        if(!Map.containskey (Strnum)) {System.out.println ("No such product is found."); } Else{System.out.println ("According to the product number" + Strnum + "query to the corresponding product" +Map.get (strnum)); }    }    Private Static voidGetnums (set<string>nums) {Random ran=NewRandom ();  while(Nums.size () < 4) {String str= "";  for(inti = 0; I < 8; i++) {                intn = ran.nextint (10); STR= str +N;        } nums.add (str);    } System.out.println (Nums); }}

Java Base array Set MAP collection synthesis application Generate random string "00000001" fruit with 0 item number problem

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.