package work01;
/*
* (1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
(2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
(3)利用四种方式遍历Map集合中的内容,格式:key::value
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class WORK01 {
public static void main(String[] args) {
HashMap<Student, String> studentHashMap = new HashMap<>();
studentHashMap.put(new Student("张华", 16), "吉林省长春市");
studentHashMap.put(new Student("李明", 15), "吉林省四平市");
studentHashMap.put(new Student("王鹏", 17), "吉林省延吉市");
studentHashMap.put(new Student("赵云", 16), "吉林省松原市");
//function01(studentHashMap);
//function02(studentHashMap);
//function03(studentHashMap);
function04(studentHashMap);
}
//遍历方法1 keySet+iterator
public static void function01(HashMap<Student, String> student){
Set<Student> studentSet = student.keySet();
Iterator<Student> studentit = studentSet.iterator();
while(studentit.hasNext()){
Student key = studentit.next();
String value = student.get(key);
System.out.println(key+" "+value);
}
}
//遍历方法2 entrySet+iterator
public static void function02(HashMap<Student, String> student){
Set<Map.Entry<Student, String>> studentSet = student.entrySet();
Iterator<Map.Entry<Student, String>> studentIt = studentSet.iterator();
while(studentIt.hasNext()){
Map.Entry<Student, String> studentEntry = studentIt.next();
System.out.println(studentEntry.getKey()+"::"+studentEntry.getValue());
}
}
// 增强for entry
public static void function03(HashMap<Student, String> student){
for(Map.Entry<Student, String> entry : student.entrySet()){
System.out.println(entry.getKey()+"::"+entry.getValue());
}
}
//增强for keySet
public static void function04(HashMap<Student, String> student){
for(Student key : student.keySet()){
String value = student.get(key);
System.out.println(key+"::"+value);
}
}
}
package work02;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
/*
* (1)利用键盘录入,输入一个字符串
(2)统计该字符串中各个字符的数量
(3)如:
用户输入字符串"If~you-want~to~change-your_fate_I_think~you~must~come-to-the-dark-horse-to-learn-java"
程序输出结果:-(9)I(2)_(3)a(7)c(2)d(1)e(6)f(2)g(1)h(4)i(1)j(1)k(2)l(1)m(2)n(4)o(8)r(4)s(2)t(8)u(4)v(1)w(1)y(3)~(6)
*/
public class Work02 {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
ArrayList<String> array = new ArrayList<>();
HashMap<Object, Integer> arrHashMap = new HashMap<>();
//将输入加入到arraylist集合中
array.add(str);
//统计并把统计存储到 arrHashMap 中
count(array,arrHashMap);
//遍历 hashmap 打印出来
charNum(arrHashMap);
}
//统计字符的 方法 count
public static void count(ArrayList<String> str ,HashMap<Object, Integer> arrHashMap){
char[] ch = str.toString().toCharArray();
int[] num = new int[ch.length];
for(int i = 1 ; i < ch.length-1 ; i++){
int count = 1;
for(int j = 1 ; j < ch.length-1 ; j++){
if(ch[i] == ch[j]){
num[i] = count++;
}
}
}
for(int i = 1 ; i < num.length-1 ; i++){
arrHashMap.put(ch[i], num[i]);
}
}
//打印输出
private static void charNum(HashMap<Object, Integer> arrHashMap) {
Object[] key_arr = arrHashMap.keySet().toArray();
Arrays.sort(key_arr);
for(Object key : key_arr){
Object skey = key;
System.out.print(skey+"("+arrHashMap.get(skey)+") ");
}
}
}