import java.util.*;
/*
class day16
{
public static void main(String[] args)
{
//新建HashMap集合
HashMap<String,String> hm = new HashMap<String,String>();
//添加字符串元素
hm.put("1","fasdf");
hm.put("2","asdfadsf");
hm.put("3","jmkljm");
//判断元素是否存在
sop(hm.containsKey("2"));
sop(hm.containsValue("fasdf"));
//显示某个键的值,并删除
sop(hm.remove("1"));
//获取某个键对应的值
sop(hm.get("2"));
//直接打印元素集合
sop(hm);
//返回所有键的值
Collection<String> value = hm.values();
sop(value);
//重写某个键的值
hm.put("2","重写");
sop(hm);
//使用两种方法取得map集合中的键值
hm.put("1","重新添加");
for (Iterator<String> it = hm.keySet().iterator();it.hasNext() ; )
{
String key = it.next();
String val = hm.get(key);
sop(key+val);
}
sop("==============取出自定义学生对象及归属地================");
//使用两种方法取出自定义学生对象及归属地
HashMap<Student,String> hm2 = new HashMap<Student,String>();
hm2.put(new Student("张三",44),"山东");
hm2.put(new Student("李四",22),"北京");
hm2.put(new Student("王五",19),"上海");
hm2.put(new Student("赵六",17),"武汉");
for (Iterator<Student> it = hm2.keySet().iterator();it.hasNext() ; )
{
Student key = it.next();
String val = hm2.get(key);
sop(key.getName()+key.getAge()+val);
}
for (Iterator<Map.Entry<Student,String>> it = hm2.entrySet().iterator();it.hasNext() ; )
{
Map.Entry<Student,String> me = it.next();
Student key = me.getKey();
String val = me.getValue();
sop(key.getName()+key.getAge()+val);
}
//定义一组学生对象按顺序存入Map集合中
TreeMap<Student,Integer> tm = new TreeMap<Student,Integer>(new Comparator<Student>()
{
public int compare(Student stu1,Student stu2)
{
if (stu1.getName().equals(stu2.getName()))
{
return stu1.getAge() - stu2.getAge();
}
return stu1.getName().compareTo(stu2.getName());
}
});
tm.put(new Student("张三",44),1);
tm.put(new Student("张三",45),2);
sop(tm);
System.out.println("Hello World!");
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Student stu)
{
return this.name.compareTo(stu.getName());
}
public int hashCode()
{
return this.name.hashCode()+this.age*11;
}
public boolean equals(Student stu)
{
return this.name.equals(stu.getName()) && this.age == stu.getAge();
}
}
*/
/*
练习:
获取字符串中的字母出现的次数
希望打印结果:a(1)c(2)...
步骤:
将字符串转为数组,存入集合中
存储时遍历数组,当集合中存在该字符时,集合中对应的值+1
不存在时,存入字符,和值1
存储时不存入非字母字符
然后将集合中的元素取出到StringBuilder缓冲区中输出
*/
class day16
{
public static void main(String[] args)
{
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
String str = "asfKJF;klasd909(";
char[] chs = str.toCharArray();
for (int x = 0;x< chs.length ;x++ )
{
if (!(chs[x] > 'a' && chs[x] < 'z' || chs[x] > 'A' && chs[x] < 'Z'))
{
continue;
}
if (tm.get(chs[x]) == null)
{
tm.put(chs[x],1);
}
else
tm.put(chs[x],tm.get(chs[x])+1);
}
StringBuilder sb = new StringBuilder();
for (Iterator<Map.Entry<Character,Integer>> it = tm.entrySet().iterator();it.hasNext() ; )
{
Map.Entry<Character,Integer> me = it.next();
sb.append(me.getKey()+"("+me.getValue()+")");
}
System.out.println(sb);
}
}
/*
class day16
{
public static void main(String[] args)
{
//新建HashMap集合
HashMap<String,String> hm = new HashMap<String,String>();
//添加字符串元素
hm.put("1","fasdf");
hm.put("2","asdfadsf");
hm.put("3","jmkljm");
//判断元素是否存在
sop(hm.containsKey("2"));
sop(hm.containsValue("fasdf"));
//显示某个键的值,并删除
sop(hm.remove("1"));
//获取某个键对应的值
sop(hm.get("2"));
//直接打印元素集合
sop(hm);
//返回所有键的值
Collection<String> value = hm.values();
sop(value);
//重写某个键的值
hm.put("2","重写");
sop(hm);
//使用两种方法取得map集合中的键值
hm.put("1","重新添加");
for (Iterator<String> it = hm.keySet().iterator();it.hasNext() ; )
{
String key = it.next();
String val = hm.get(key);
sop(key+val);
}
sop("==============取出自定义学生对象及归属地================");
//使用两种方法取出自定义学生对象及归属地
HashMap<Student,String> hm2 = new HashMap<Student,String>();
hm2.put(new Student("张三",44),"山东");
hm2.put(new Student("李四",22),"北京");
hm2.put(new Student("王五",19),"上海");
hm2.put(new Student("赵六",17),"武汉");
for (Iterator<Student> it = hm2.keySet().iterator();it.hasNext() ; )
{
Student key = it.next();
String val = hm2.get(key);
sop(key.getName()+key.getAge()+val);
}
for (Iterator<Map.Entry<Student,String>> it = hm2.entrySet().iterator();it.hasNext() ; )
{
Map.Entry<Student,String> me = it.next();
Student key = me.getKey();
String val = me.getValue();
sop(key.getName()+key.getAge()+val);
}
//定义一组学生对象按顺序存入Map集合中
TreeMap<Student,Integer> tm = new TreeMap<Student,Integer>(new Comparator<Student>()
{
public int compare(Student stu1,Student stu2)
{
if (stu1.getName().equals(stu2.getName()))
{
return stu1.getAge() - stu2.getAge();
}
return stu1.getName().compareTo(stu2.getName());
}
});
tm.put(new Student("张三",44),1);
tm.put(new Student("张三",45),2);
sop(tm);
System.out.println("Hello World!");
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Student stu)
{
return this.name.compareTo(stu.getName());
}
public int hashCode()
{
return this.name.hashCode()+this.age*11;
}
public boolean equals(Student stu)
{
return this.name.equals(stu.getName()) && this.age == stu.getAge();
}
}
*/
/*
练习:
获取字符串中的字母出现的次数
希望打印结果:a(1)c(2)...
步骤:
将字符串转为数组,存入集合中
存储时遍历数组,当集合中存在该字符时,集合中对应的值+1
不存在时,存入字符,和值1
存储时不存入非字母字符
然后将集合中的元素取出到StringBuilder缓冲区中输出
*/
class day16
{
public static void main(String[] args)
{
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
String str = "asfKJF;klasd909(";
char[] chs = str.toCharArray();
for (int x = 0;x< chs.length ;x++ )
{
if (!(chs[x] > 'a' && chs[x] < 'z' || chs[x] > 'A' && chs[x] < 'Z'))
{
continue;
}
if (tm.get(chs[x]) == null)
{
tm.put(chs[x],1);
}
else
tm.put(chs[x],tm.get(chs[x])+1);
}
StringBuilder sb = new StringBuilder();
for (Iterator<Map.Entry<Character,Integer>> it = tm.entrySet().iterator();it.hasNext() ; )
{
Map.Entry<Character,Integer> me = it.next();
sb.append(me.getKey()+"("+me.getValue()+")");
}
System.out.println(sb);
}
}