HashMap集合存储学生对象并遍历
package 集合进阶.Map双列.HashMap集合存储学生对象并遍历;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package 集合进阶.Map双列.HashMap集合存储学生对象并遍历;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
//创建HashMap对象 键是学号,值是学生对象
HashMap<String,Student> hm = new HashMap<String,Student>();
//创建学生对象
Student s1=new Student("赵",20);
Student s2=new Student("钱",21);
Student s3=new Student("孙",22);
//把学生添加到集合
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
//遍历集合
//方式1: 键找值
Set<String> keySet = hm.keySet();
for(String key : keySet) {
Student value = hm.get(key);
System.out.println(key+","+value.getName()+","+value.getAge());
}
//方式2: 键值对对象找键和值
Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
for(Map.Entry<String, Student> entry : entrySet) {
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key+","+value.getName()+","+value.getAge());
}
}
}
ArrayList集合存储HashMap元素并遍历
package 集合进阶.Map双列;
/*
需求:创建一个ArrayList集合,存储三个元素
每一个元素都是HashMap,每一个HashMap的键和值都是String
并遍历
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ArrayList集合存储HashMap元素并遍历 {
public static void main(String[] args) {
//创建ArrayList集合
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
//创建HashMap集合,并添加键值对元素
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("001", "赵");
hm1.put("002", "钱");
//把HashMap作为元素添加到ArrayList集合
array.add(hm1);
//创建HashMap集合,并添加键值对元素
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("003", "周");
hm2.put("004", "郑");
//把HashMap作为元素添加到ArrayList集合
array.add(hm2);
//创建HashMap集合,并添加键值对元素
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("005", "王");
hm3.put("006", "冯");
//把HashMap作为元素添加到ArrayList集合
array.add(hm3);
//遍历ArrayList集合
for (HashMap<String, String> hm : array) {
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "," + value);
}
}
}
}
HashMap集合存储ArrayList元素并遍历
package 集合进阶.Map双列;
/*
需求:
创建一个HashMap集合,存储三个键值对元素
每一个键值对元素的键是String,值是ArrayList
每一个ArrayList的元素是String
并遍历
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class HashMap集合存储ArrayList元素并遍历 {
public static void main(String[] args) {
//创建HashMap集合
HashMap<String, ArrayList<String>> hm=new HashMap<String, ArrayList<String>>();
//创建ArrayList集合,并添加元素
ArrayList<String> sgyy=new ArrayList<String>();
sgyy.add("诸葛亮");
sgyy.add("刘备");
//把ArrayList作为元素添加到HashMap集合
hm.put("三国",sgyy);
//创建ArrayList集合,并添加元素
ArrayList<String> xyj=new ArrayList<String>();
xyj.add("孙悟空");
xyj.add("八戒");
//把ArrayList作为元素添加到HashMap集合
hm.put("西游记",xyj);
//创建ArrayList集合,并添加元素
ArrayList<String> shz=new ArrayList<String>();
shz.add("武松");
shz.add("鲁智深");
//把ArrayList作为元素添加到HashMap集合
hm.put("水浒传",shz);
//遍历HashMap集合
Set<String> keySet = hm.keySet();
for (String key : keySet) {
ArrayList<String> value = hm.get(key);
for (String s:value){
System.out.println(s);
}
}
}
}
统计字符串中每个字符出现次数
package 集合进阶.Map双列;
/*
统计字符串中每个字符出现的次数
1.需求: 键盘录入一个字符串,要求统计字符串中每个字符串出现的次数。
2.举例: 键盘录入"aababcabcdabcde" 在控制台输出:“a(5)b(4)c(3)d(2)e(1)
3.分析:
①我们可以把结果分成几个部分来看:a(5),b(4),c(3),d(2),e(1)
②每一个部分可以看成是:字符和字符对应的次数组成
这样的数据,我们可以通过HashMap集合来存储,键是字符,值是字符出现的次数
注意:键是字符,类型应该是Character;值是字符出现的次数,类型应该是Integer
思路:
1.键盘录入一个字符串
2.创建HashMap集合,键是Character,值是Integer
3.遍历字符串,得到每一个字符
4.拿得到的每一个字符作为键到HashMap集合中去找对应的值,看其返回值
如果返回值是null:说明该字符在HashMap集合中不存在,就把该字符作为键,1作为值存储
如果返回值不是null:说明该字符在HashMap集合中存在,把该值加1,然后重新存储该字符和对应的值
5.遍历HashMap集合,得到键和值,按照要求进行拼接
6.输出结果
*/
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class 统计字符串中每个字符出现次数 {
public static void main(String[] args) {
//键盘录入一个字符串
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = scanner.nextLine();
//创建HashMap集合,键是Character,值是Integer
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
//遍历字符串,得到每一个字符
for (int i=0;i<line.length();i++){
char key = line.charAt(i);
//4.拿得到的每一个字符作为键到HashMap集合中去找对应的值,看其返回值
Integer value = hm.get(key);
if(value == null){
//如果返回值是null:说明该字符在HashMap集合中不存在,就把该字符作为键,1作为值存储
hm.put(key,1);
}else{
//如果返回值不是null:说明该字符在HashMap集合中存在,把该值加1,然后重新存储该字符和对应的值
value++;
hm.put(key,value);
}
}
//遍历HashMap集合,得到键和值,按照要求进行拼接
StringBuilder sb=new StringBuilder();
Set<Character> keySet = hm.keySet();
for (Character key : keySet){
Integer value = hm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String s = sb.toString();
//输出结果
System.out.println(s);
}
}