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