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