Map集合的两个小实例以及Map集合的基本操作

这两个实例现在还有一点细节没有搞明白,大晚上脑子一下子转不过来弯,等第二天在看一下,先存起来

Map统计字符串

package Map统计字符串出现的次数;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class a {
public static void main(String[] args) {
 String text="sdasdasdfvsdfg5ws41as23fas";
 //1)定义一个Map集合  来保存<字符,次数>
 Map<Character,Integer> map=new HashMap<>();
 //2)遍历字符串中的每一个字符
 for(int i=0;i<text.length();i++) {
  char cc=text.charAt(i);
  //2.1 如果该字符是第一次出现(map中的键不包含这个字符)把<字符,1>添加到map中
  if(!map.containsKey(cc)) {
   map.put(cc,1);
  }else {
   //2.2如果该字符不是第一次出现,把map中该字符取出来加一在保存到map中
   int count=map.get(cc);    //int一个count  将map的值赋给count
   map.replace(cc,count+1);
  }
 }
 Set<Entry<Character, Integer>> entrySet = map.entrySet();   //创建一个对象返回所有Entry的集合
 for (Entry<Character, Integer> entry : entrySet) {   //在使用foreach循环遍历
  System.out.println(entry.getKey()+":"+entry.getValue());  //打印
 }
}
}

Map集合统计邮箱

package map统计邮箱;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class a {
public static void main(String[] args) {
 String text ="yang@163.com,bing@yahu.com,hei@baidu.com,shu@shohu.com";
 //把字符串的邮箱分离出来
 String[] words=text.split("[,@]");
 
 //在分别把邮箱名和邮箱地址保存到map集合中
 //在把map集合保存到set集合中
 Set<Map<String,String>> set=new HashSet<Map<String,String>>();
 for(int i=0;i<words.length;i+=2) {
  Map<String,String> map=new HashMap<String, String>();
  map.put(words[i],words[i+1]);
  set.add(map);
 }
 System.out.println(set);
 //统计每个邮箱地址的邮箱个数
 Map<String,Integer> map2=new HashMap<String, Integer>();
 for(int i=1;i<words.length;i+=2) {
  if(!map2.containsKey(words[i])) { //如果没有这个键的话
   map2.put(words[i],1);
  }else {   //如果有这个键的话
   int count=map2.get(words[i]);   //将map2的值返回给count
   map2.replace(words[i], count+1);  //在进行加一操作
  }
 }
 //打印
 Set<Entry<String, Integer>> entrySet = map2.entrySet();
 for (Entry<String, Integer> entry : entrySet) {
  System.out.println(entry.getKey()+":"+entry.getValue());
 }
}
}

Map集合的基本操作

package Map集合;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class a {
public static void main(String[] args) {
 //1)创建Map集合
 Map<String,Integer> map =new HashMap<String, Integer>();
 //2)向Map集合中添加元素
 map.put("zahngsan",5000);
 map.put("lisi",6564);
 map.put("wangwu",6524);
 map.put("zhaoliu",8521);
 map.put("qiqi",6530);
 map.put("baba",665630);
 //3)打印Map集合,存储顺序与添加顺序可能不一样
 System.out.println(map);
 System.out.println();
 //4)put添加数据时,如键已存在,则会将原来的值替换为现在的值
 map.put("zahngsan", 5001);
 System.out.println(map);
 System.out.println();
 //5)判断
 System.out.println(map.size());   //判断键值对的数量
 System.out.println(map.containsKey("zhang"));   //通过键来查询
 System.out.println(map.containsValue("100000")); //通过值来查询
 System.out.println();
 //6)使用键来返回值
 System.out.println(map.get("qiqi"));
 System.out.println(map.get("zhang"));   //键不存在,打印null
 System.out.println();
 //7)修改替换
 map.replace("lisi",65);    //通过replace关键字来进行修改
 System.out.println(map);
 map.replace("sda", 545);  //如果当前map中不存在这个键,则无变化
 System.out.println(map);  //无变化
 System.out.println();
 //8)删除
 map.remove("lisi");   //只要键匹配
 System.out.println(map);
 map.remove("baba",665630);  //需要键和值都匹配
 System.out.println(map);
 System.out.println();
 //9)返回所有键的集合
 Set<String>  ks=map.keySet();
 System.out.println(ks);
 System.out.println();
 //10)返回所有值的集合
 System.out.println(map.values());
 System.out.println();
 //11)返回所有Entry的集合
 Set<Entry<String, Integer>> entrySet = map.entrySet();
 for (Entry<String, Integer> entry : entrySet) {
  System.out.println(entry.getKey()+":"+entry.getValue());
 }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值