日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Map集合练习题

發布時間:2024/8/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Map集合练习题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、已知字符串“adhflkalkfdhasdkhflsa”
(1)統計去掉重復后的字符
(2)統計每個字符出現的次數,使用map存儲,字符為鍵,次數為值。
(3)遍歷map,打印統計信息

package com.Map集合1112;import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set;public class MapStringTest {public static void main(String[] args) {// TODO Auto-generated method stubString s = "adhflkalkfdhasdkhflsa";Set<Character> str_set = new HashSet<Character>();// 將字符串轉化為字符數組,存放在cs數組中char[] cs = s.toCharArray();for (Character c : cs) {str_set.add(c);}System.out.println("去掉重復,總共有:" + str_set.size() + "個字符");Map<Character, Integer> map = new HashMap<Character, Integer>();for (Character c : cs) {map.put(c, map.containsKey(c) ? (map.get(c) + 1) : 1);}//打印map集合map.forEach((key, value) -> {System.out.println("鍵:" + key + " 值:" + value);});}}

運行結果為:

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++;}}}

運行結果為:

總結

以上是生活随笔為你收集整理的Map集合练习题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。