java基础之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 Demo1 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str ="If you want to change your fate I think you must come to the ujiuye to learn java";
		Map<String,Integer> map = new HashMap<String,Integer>();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			String key = c+"";
			boolean flag =map.containsKey(key);
			//判断map中是否有该元素,有了value叠加
			if(flag){
				int count = map.get(key);
				map.put(key,++count);
				//没有该元素设value为1
			}else{
				map.put(key, 1);
			}
		}
		//Entry方法遍历打印集合
		Set<Entry<String,Integer>> set =map.entrySet();
		for (Map.Entry<String, Integer> entry : set) {
			System.out.println(entry.getKey()+"="+entry.getValue());
		}
	}
}

```java
案例2:已知 List<String> list = new ArrayList<String>();
list .add("张三丰,北京");
list .add("李四丰,上海");
list .add("王二小,北京");
list .add("小明,河北");
list .add("小毛,北京");
list .add("王五,北京");   
要求:求出每个地区有多少人,都是谁?

例如: 北京 4人   张三丰 王二小 小毛 王五

```java
public class Demo2 {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("张三丰,北京");
		list.add("李四丰,上海");
		list.add("王二小,北京");
		list.add("小明,河北");
		list.add("小毛,北京");
		list.add("王五,北京");
		//令value为list集合
		Map<String, List<String>> map = new HashMap<>();
		for (String s : list) {
			//通过,分割数组strings[0]为人名,strings[1]为地名
			String[] strings = s.split(",");
			//System.out.print(Arrays.toString(strings));
			boolean flag = map.containsKey(strings[1]);
			//判断该城市中是否有人或有对相应的人
			//没有的话创建对应的人名集合,然后把此人添加到新的人名集合中
			if (!flag) {
				map.put(strings[1], new ArrayList<>());
				map.get(strings[1]).add(strings[0]);
			//
			} else {
				//有直接添加到该人名集合中
				map.get(strings[1]).add(strings[0]);
			}
		}
		//遍历集合
		Set<Entry<String, List<String>>> set = map.entrySet();
		for (Map.Entry<String, List<String>> entry : set) {
			System.out.println(entry.getKey() + " " + entry.getValue().size() + entry.getValue());
		}
	}

}

  1. 做一个方法 方法有两个参数 参数1 Map<String,String> 参数2:String value

方法完成的功能是 根据value获取key

public static 返回值类型 getKeyByValue(方法完成的功能是 根据value获取key){

}
返回值类型自身思考用什么类型???
getKeyByValue(map,“111”)

例如:传入的map中的数据为:
map.put(“aaa”,“111”);
map.put(“bbb”,“111”);
map.put(“ccc”,“111”);
map.put(“ddd”,“222”);

方法就应该获得 “aaa” “bbb” “ccc”

public class Demo6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<>();
		map.put("aaa","111");  
		map.put("bbb","111"); 
		map.put("ccc","111"); 
		map.put("ddd","222"); 
		System.out.println(getKeyByValue(map,"111"));
		}

	public static List<String> getKeyByValue(Map<String,String> map, String value){
		List<String> list = new ArrayList<>(); 
		Set<Map.Entry<String, String>> set = map.entrySet();
		for (Map.Entry<String, String> entry : set) {
			if(entry.getValue()==value){
				list.add(entry.getKey());
			}else{
				continue;
			}
		}
		return list;	
	}
}

4.模拟斗地主洗牌发牌,并将发好的牌排好顺序

public class Demo5 {
	public static void main(String[] args) {
		// 牌数
		String[] nums = {  "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" ,"A", "2"};
		// 花色
		String[] colors = { "♥", "♣", "♠", "♦" };
		// 扑克
		List<Integer> poker = new ArrayList<Integer>();// 只存数字,发牌玩数字即可
		Map<Integer, String> table = new HashMap<Integer, String>();//存序号和牌面对应
		//把牌装入list集合为了发牌,洗牌。把牌装入map集合中为了排序
		int count = 1;
		for (String num : nums) {
			for (String color : colors) {
				table.put(count, color + num);
				poker.add(count);
				count++;
			}
		}
		//加入大王小王
		poker.add(53);
		poker.add(54);
		table.put(53, "小王");
		table.put(54, "大王");
		//洗牌
		Collections.shuffle(poker);
		//创建四个集合对象存牌
		List<Integer> dusheng = new ArrayList<>();
		List<Integer> dushen = new ArrayList<>();
		List<Integer> me = new ArrayList<>();
		List<Integer> dipai = new ArrayList<>();
		
		//先存底牌
		for (int i = 1; i <=3; i++) {
			dipai.add(poker.remove(0));
		}
		while(true){
			if(!poker.isEmpty()){
				dusheng.add(poker.remove(0));
			}
			if(!poker.isEmpty()){
				dushen.add(poker.remove(0));
			}
			if(!poker.isEmpty()){
				me.add(poker.remove(0));
			}
			if(poker.isEmpty()){
				break;
			}
		}
		//排序
		Collections.sort(dusheng);
		Collections.sort(dushen);
		Collections.sort(me);
		Collections.sort(dipai);
		//看牌
		System.out.println("底牌"+kanpai(dipai,table));
		System.out.println("小真的牌"+kanpai(dusheng,table));
		System.out.println("小明的牌"+kanpai(me,table));
		System.out.println("小宝的牌"+kanpai(dushen,table));
		

	}
	public static List<String> kanpai(List<Integer> list2,Map<Integer,String> map){
		List<String> list = new ArrayList();
		for (Integer num : list2) {
			list.add(map.get(num));
		}
		
		return list;
		
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值