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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JavaSE各阶段练习题----Map

發布時間:2024/2/28 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaSE各阶段练习题----Map 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 分析以下需求,并用代碼實現:

(1)統計每個單詞出現的次數

(2)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格間隔)

(3)打印格式:

to=3

?? think=1

?? you=2

public?class?Test01 {

?

public?static?void?main(String[] args) {

?

String str?= "If you want to change your fate I think you must come to the ujiuye to learn java";

String[] strS?= str.split(" ");

Map<String, Integer> hMap?= new?HashMap<String, Integer>();

for?(int?i?= 0; i?< strS.length; i++) {

String s?= strS[i];

if?(hMap.get(s) == null) {

hMap.put(s, 1);

}else?{

hMap.put(s, hMap.get(s)+1);

}

}

System.out.println(hMap);

//{think=1, fate=1, ujiuye=1, learn=1, want=1, change=1, I=1, come=1, your=1, the=1, java=1, must=1, to=3, If=1, you=2}

}

}

?

2 ?案例2:已知 List<String> list = new ArrayList<String>();

list .add("張三豐,北京");

list .add("李四豐,上海");

list .add("王二小,北京");

list .add("小明,河北");

list .add("小毛,北京");

list .add("王五,北京"); ??

要求:求出每個地區有多少人,都是誰?

?

例如: 北京 4人 ??張三豐 王二小 小毛 王五

public?class?Test02 {

?

public?static?void?main(String[] args) {

?

List<String> list?= new?ArrayList<String>();

list.add("張三豐,北京");

list.add("李四豐,上海");

list.add("王二小,北京");

list.add("小明,河北");

list.add("小毛,北京");

list.add("王五,北京"); ??

Map<String, Integer> hMapCount?= new?HashMap<>();

Map<String, String> hMapNames?= new?HashMap<>();

?

for?(String str?: list) {

String[] strS?= str.split(",");

String s?= strS[1];

if?(hMapCount.get(s) == null) {

hMapCount.put(s, 1);

hMapNames.put(s, strS[0]);

}else?{

hMapCount.put(s, hMapCount.get(s)+1);

hMapNames.put(s, hMapNames.get(s) +" "+ strS[0]);

}

}

Set<String> set?= hMapCount.keySet();

for?(String string?: set) {

System.out.println(string+" "+hMapCount.get(string)+"人"+ " "+hMapNames.get(string));

}

/*

?* 上海 1人 李四豐

河北 1人 小明

北京 4人 張三豐 王二小 小毛 王五

?*/

}

?

}

?

3.

3 做一個方法 ??方法有兩個參數 ??參數1 Map<String,String> ?參數2:String value

?

???方法完成的功能是 根據value獲取key

???

???

???public ?static 返回值類型 ?getKeyByValue(Map<String,String>,String value){

???

???}

???

???返回值類型自身思考用什么類型???

???

???getKeyByValue(map,"111")

?

???例如:傳入的map中的數據為:

???map.put("aaa","111"); ?

???map.put("bbb","111");

???map.put("ccc","111");

???map.put("ddd","222"); ???

???

???方法就應該獲得 ????"aaa" ?"bbb" ?"ccc"

package?com.henu;

?

import?java.util.ArrayList;

import?java.util.HashMap;

import?java.util.List;

import?java.util.Map;

import?java.util.Map.Entry;

import?java.util.Set;

?

public?class?Demo_mapgetkeybyvalue {

public?static?void?main(String[] args) {

?

Map<String, String> hMap?= new?HashMap<String, String>();

hMap.put("111", "aaa");

hMap.put("222", "bbb");

hMap.put("333", "aaa");

hMap.put("444", "ddd");

hMap.put("555", "aaa");

hMap.put("666", "eee");

String value?= "aaa";

System.out.println(mapGetKeyByValue(hMap, value));

}

?

public?static?List<String> mapGetKeyByValue(Map<String, String> hMap,String value){

?

Set<Entry<String, String>> hSet?= hMap.entrySet();

List<String> list?= new?ArrayList<String>();

?

for?(Entry<String, String> entry?: hSet) {

String k?= entry.getKey();

String v?= entry.getValue();

if?(v.equals(value)) {

list.add(k);

}

}

?

return?list;

?

}

?

}

?

?

4.?獨立完成課上斗地主發牌案例

package?com.henu;

?

import?java.util.ArrayList;

import?java.util.Collections;

import?java.util.HashMap;

import?java.util.List;

import?java.util.Map;

?

public?class?Demo04_斗地主_制牌_洗牌_發牌_有序 {

?

public?static?void?main(String[] args) {

?

String[] nums?= {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

String[] color?= {"?","?","?","?"};

//定義map存儲所有牌,采用它的key為后期的每個人的手牌排序做準備

Map<Integer, String> hMap?= new?HashMap<>();

//list存取序號,相當于map的key值。

List<Integer> cardId?= new?ArrayList<Integer>();

int?count?= 1;

//雙重循環將牌存入到map中,注意外內循環的不同數組,為了后續的排序

for?(String str1?: nums) {

for?(String str2?: color) {

cardId.add(count);

hMap.put(count++, str2+str1);

}

}

hMap.put(53, "小?");

hMap.put(54, "大?");

cardId.add(53);

cardId.add(54);

?

//將牌的序號進行打亂

Collections.shuffle(cardId);

?

//定義三個玩家

List<Integer> xiaoTeng?= new?ArrayList<>();

List<Integer> xiaoYun?= new?ArrayList<>();

List<Integer> xiaoHong?= new?ArrayList<>();

List<Integer> diPai?= new?ArrayList<>();

?

//發牌

for?(int?i?= 0; i?< cardId.size(); i?+= 3) {

?

if?(i?< cardId.size()-3) {

xiaoTeng.add(cardId.get(i));

xiaoYun.add(cardId.get(i+1));

xiaoHong.add(cardId.get(i+2));

}else?{

diPai.add(cardId.get(i-1));

diPai.add(cardId.get(i));

diPai.add(cardId.get(i+1));

}

}

?

//輸出結果

System.out.println("小騰的牌是:"+toCard(xiaoTeng, hMap));

System.out.println("小云的牌是:"+toCard(xiaoYun, hMap));

System.out.println("小宏的牌是:"+toCard(xiaoHong, hMap));

System.out.println("底牌的牌是:"+toCard(diPai, hMap));

?

?

}

//根據list的序號取map的value;即根據序號對map的 牌進行取,得到的即是排序好的紙牌。

public?static?List<String> toCard(List<Integer> user,Map<Integer, String> hMap){

//現將抽到的牌進行排序

Collections.sort(user);

//根據序號取到牌

List<String> userCard?= new?ArrayList<>();

for?(Integer in?: user) {

userCard.add(hMap.get(in));

}

?

return?userCard;

?

}

?

}

?

?

?

總結

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

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