day16集合map里面的(hashtable,hashmap,treemap)的取出方式(keyset和entryset),获取该字符串的字母出现次数
来源:互联网 发布:淘宝联盟可以自己买吗 编辑:程序博客网 时间:2024/04/27 15:24
{
public static void main(String[] args)
{
Map<String,String> map=new HashMap<String,String>();
//
map.put("01","zhangsan");
map.put("02","zhangsan");
map.put("03","zhangsan");
System.out.println("containsKey:"+map.containsKey("022"));
//System.out.println("remove:"+map.remove("02"));
System.out.println("get:"+map.get("02"));
map.put(null,"aha");
System.out.println("get:"+map.get(null));
//可以通过get方法的返回值来判断一个键是否存在。通过返回null判断
//获取map集合中所有的值
Collection<String> coll=map.values();
System.out.println(coll);
System.out.println(map);
}
}
-----------------------------------------------
import java.util.*;
class MapDemo1
{
public static void main(String[] args)
{
Map<String,String> map=new HashMap<String,String>();
//
map.put("01","zhangsan2");
map.put("04","zhangsan4");
map.put("02","zhangsan3");
map.put("03","zhangsan4");
//先获取map集合所有键的set集合,keySet()
Set<String> keySet=map.keySet();
//有了set集合,就可以获取迭代器
Iterator<String> it=keySet.iterator();
while (it.hasNext())
{
String key=it.next();
//有了键就可以通过map集合的get方法获取其对应的值。
String value=map.get(key);
System.out.println("key:"+key+",value:"+value);
}
}
}
-----------------------------------------
/*
每个学生都对应的归属地
学生student,地址String.
学生属性:姓名,年龄。
注意:,姓名和年龄相同视为同一个学生
保证学生的唯一性
思路:
1,描述学生
2,定义map集合,将学生作为键值,地址为值。存入
3,获取map集合中的元素。
*/
import java.util.*;
class Student implements Comparable<Student>//实现,两个覆盖方法。
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Student s)
{
int num=new Integer(this.name).compareTo(new Integer(s.name));
if (num==0)
{
return this.name.compareTo(s.name);
}
return num;
}
public int hashCode()
{
return name.hashCode()+age*34;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Student))
{
throw new ClassCastException("类型不匹配");
}
Student s=(Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+":"+age;
}
}
class MapDemo2
{
public static void main(String[] args)
{
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("lisi1",21),"beijing");
hm.put(new Student("lisi2",22),"shanghai");
hm.put(new Student("lisi3",23),"nanjing");
hm.put(new Student("lisi4",24),"wuhan");
//第一种取出方式keySet
//先获取map集合所有键的set集合,keySet()
Set<Student> keySet=hm.keySet();
//有了set集合,就可以获取迭代器
Iterator<Student> it=keySet.iterator();
while (it.hasNext())
{
Student stu=it.next();
//有了键就可以通过map集合的get方法获取其对应的值。
String addr=hm.get(stu);
System.out.println("stu:"+stu+",addr:"+addr);
}
//第二种取出方式entrySet
Set<Map.Entry<Student,String>> entrySet=hm.entrySet();
Iterator<Map.Entry<Student,String>> iter=entrySet.iterator();
while (iter.hasNext())
{
Map.Entry<Student,String> me=iter.next();
Student stu=me.getKey();
String addr=me.getValue();
System.out.println("stu:"+stu+"-----,addr:"+addr);
}
}
}
--------------------------------------
/*
sdfgzxcvasdfxcdf获取该字符串的字母出现次数
*/
import java.util.*;
class MapTest
{
public static void main(String[] args)
{
String s=CharCount("sdfgzxcvasdfxcdf");
System.out.println(s);
}
public static String CharCount(String str)
{
char[] chs=str.toCharArray();
TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
for (int x=0;x<chs.length ;x++ )
{
Integer value=tm.get(chs[x]);
if (value==null)
{
tm.put(chs[x],1);
}
else
{
value=value+1;
tm.put(chs[x],value);
}
}
StringBuilder sb= new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet=tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
while (it.hasNext())
{
Map.Entry<Character,Integer> me=it.next();
Character ch=me.getKey();
Integer value=me.getValue();
sb.append(ch+"("+value+")");
}
return sb.toString();
}
}
- day16集合map里面的(hashtable,hashmap,treemap)的取出方式(keyset和entryset),获取该字符串的字母出现次数
- 【java编程】Map集合之HashMap--KeySet和EntrySet的两种取出方式
- 集合框架——Map的两种取出方式(keySet和entrySet)
- Map集合的两种取出方式(keySet、entrySet)
- HashMap的两种取出方式keySet和entrySet
- java中map集合的两种取出方式keySet和entrySet
- 黑马程序员_集合HashMap练习:获取Map集合元素的两种方式:keySet()和entrySet()方法
- TreeMap获取该字符串中的每个字母出现的次数
- Map集合的两种获取元素的方式:keySet()和entrySet()方法
- 代码训练营——TreeMap(获取该字符串中的字母出现的次数)
- Map集合中的两种取出方式keySet和entrySet
- 利用map集合获取字符串中字母出现的次数
- Java Map集合获取字符串中的字母出现的次数
- Map集合 键值对 映射关系 Hashtable HashMap TreeMap keySet entrySet Map.Entry
- 黑马程序员_集合TreeMap练习:获取某一字符串的字母出现的次数
- 集合(TreeMap练习-字母出现的次数)
- map的keySet entrySet
- Map遍历的两种方式keySet()和entrySet()
- 项目完成过程中遇到的问题(更新结束)
- 三,MyBatis映射器及CRUD
- Android中的RecyclerView: 基础知识
- Android5.x:RecycleView(一):实现ListView + GridView + StaggeredGridLayou效果
- 关于谷歌webrtc源码国内镜像的使用问题,以及Kurento媒体服务器
- day16集合map里面的(hashtable,hashmap,treemap)的取出方式(keyset和entryset),获取该字符串的字母出现次数
- UNP-基本TCP编程-1
- 101. Symmetric Tree
- 【面试收货】linux部分
- CAS原理分析
- 六个最佳的软件工程实践(迭代化开发、需求管理)
- struts2文件过滤拦截器fileUpload以及各种文件类型
- #Deep Learning回顾#之LeNet、AlexNet、GoogLeNet、VGG、ResNet
- IIS网站或系统验证码不显示问题——"使用了托管的处理程序,但是未安装或未完整安装 ASP.NET"