Map集合练习题
1、已知字符串“adhflkalkfdhasdkhflsa”
(1)統計去掉重復后的字符
(2)統計每個字符出現的次數,使用map存儲,字符為鍵,次數為值。
(3)遍歷map,打印統計信息
運行結果為:
2、(Map)利用Map,完成下面的功能:
從命令行讀入一個字符串,表示一個年份,輸出該年的世界杯冠軍是哪支球隊。如果該年沒有舉辦世界杯,則輸出:沒有舉辦世界杯。
歷屆世界杯冠軍
(Map)在原有世界杯Map 的基礎上,增加如下功能: 讀入一支球隊的名字,輸出該球隊奪冠的年份列表。 例如,讀入“巴西”,應當輸出 1958 1962 1970 1994 2002 讀入“荷蘭”,應當輸出 沒有獲得過世界杯 。
package com.Map集合1112;import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set;public class TheWorldCup {public static void main(String[] args) {// TODO Auto-generated method stubMap<String, String> map = new HashMap<String,String>();map.put("1930", "烏拉圭");map.put("1934", "意大利");map.put("1938", "意大利");map.put("1950", "烏拉圭");map.put("1954", "西德");map.put("1958", "巴西");map.put("1962", "巴西");map.put("1966", "英格蘭");map.put("1970", "巴西");map.put("1974", "西德");map.put("1978", "阿根廷");map.put("1982", "意大利");map.put("1986", "阿根廷");map.put("1990", "西德");map.put("1994", "巴西");map.put("1998", "法國");map.put("2002", "巴西");map.put("2006", "意大利");map.put("2010", "西班牙");map.put("2014", "德國");System.out.println("請輸入年份:");Scanner input = new Scanner(System.in);String year = input.next();Set<String> keys = map.keySet();boolean isFindYear = false;boolean isFindCountry = false;for(String key :keys) {if(year.equals(key)) {System.out.println(map.get(key));isFindYear = true;}}if(isFindYear == false){System.out.println("沒有舉辦世界杯");};System.out.println("請輸入國家:");String country = input.next();Set<String> totleYearSet = new HashSet<String>();for(String key: keys) {if(country.equals(map.get(key))) {totleYearSet.add(key);isFindCountry = true;}}for(String reyear:totleYearSet) {System.out.print(reyear + " ");}if(isFindCountry == false){System.out.println("沒有獲得過世界杯");};}}運行結果:
3、(Map)設計Account 對象如下:
private long id;
private double balance;
private String password;
要求完善設計,使得該Account 對象能夠自動分配id。 給定一個List 如下:
List list = new ArrayList();
list.add(new Account(10.00, “1234”));
list.add(new Account(15.00, “5678”));
list.add(new Account(0, “1010”));
要求把List 中的內容放到一個Map 中,該Map 的鍵為id,值為相應的Account 對象。 最后遍歷這個Map,打印所有Account 對象的id 和余額。
package com.Map集合1112;import java.awt.RenderingHints.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;public class MapCount {public static void main(String[] args) {// TODO Auto-generated method stubList<Account> list = new ArrayList<Account>();list.add(new Account(10.00, "1234"));list.add(new Account(15.00, "5678"));list.add(new Account(0, "1010"));Map<Long, Account> map = new HashMap<Long, Account>();for(Account a:list) {Long id = (long)(Math.random()*1000000000);map.put(id, a);}map.forEach((key , value)->{System.out.println("id:" + key + " 余額:" + value.getBalance());});} } class Account{private long id;private double balance;private String password;public Account(double balance, String password) {super();this.balance = balance;this.password = password;}public Account(long id, double balance, String password) {super();this.id = id;this.balance = balance;this.password = password;}public long getId() {return id;}public void setId(long id) {this.id = id;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }運行結果:
4、已知有十六支男子足球隊參加2008 北京奧運會。
寫一個程序,把這16 支球隊隨機分為4 個組。采用List集合和隨機數
2008 北京奧運會男足參賽國家:
科特迪瓦,阿根廷,澳大利亞,塞爾維亞,荷蘭,尼日利亞、日本,美國,中國,新西 蘭,巴西,比利時,韓國,喀麥隆,洪都拉斯,意大利。
package com.Map集合1112;import java.util.List; import java.util.ArrayList; import java.util.Collection;public class Grouping {public static void main(String[] args) {// TODO Auto-generated method stubList<String> list = new ArrayList<String>();List<String> delList = new ArrayList<String>();String[] str = { "科特迪瓦", "阿根廷", "澳大利亞", "塞爾維亞", "荷蘭", "尼日利亞", "日本", "美國", "中國", "新西蘭", "巴西", "比利時", "韓國", "喀麥隆","洪都拉斯", "意大利" };for(int i=0;i<str.length;i++) {list.add("+++");}delList.add("+++");Collection<String> col = delList;int index = 0;for (int i=0;i<str.length; i++) {int random = (int) (Math.random() * 16);if (list.contains(str[index])) {random = (int) (Math.random() * 16);list.add(random, str[index++]);} else {list.add(random, str[index++]);}}list.removeAll(col);int a = 0;for (String string : list) {if(a==0) {System.out.println("第1組");}if(a==4) {System.out.println();System.out.println("第2組");}if(a==8) {System.out.println();System.out.println("第3組");}if(a==12) {System.out.println();System.out.println("第4組");}System.out.print(string + " ");a++;}}}運行結果為:
總結
- 上一篇: InfoPath 教程
- 下一篇: Product Key Algorith