ArrayList集合存储HashMap元素并遍历
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ArrayListIncludeHashMapDemo {
public static void main(String[] args) {
//创建ArrayList集合
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm1 = new HashMap<>();
hm1.put("张1","张艺");
hm1.put("张2","张尔");
array.add(hm1);
HashMap<String, String> hm2 = new HashMap<>();
hm2.put("赵1","赵一");
hm2.put("赵2","赵二");
array.add(hm2);
HashMap<String, String> hm3 = new HashMap<>();
hm3.put("钱1","钱意");
hm3.put("钱2","钱而");
array.add(hm3);
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 元素并遍历
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class HashMapIncludeArrayListDemo1 {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
ArrayList<String> sgyy = new ArrayList<>();
sgyy.add("诸葛亮");
sgyy.add("周瑜");
hm.put("三国演义",sgyy);
ArrayList<String> xyj = new ArrayList<>();
xyj.add("孙悟空");
xyj.add("猪八戒");
hm.put("西游记",xyj);
ArrayList<String> hlm = new ArrayList<>();
hlm.add("贾宝玉");
hlm.add("林黛玉");
hm.put("红楼梦",hlm);
Set<String> keySet = hm.keySet();
for (String key:keySet){
System.out.println(key);
ArrayList<String> value = hm.get(key);
for (String s:value){
System.out.println("\t"+s);
}
}
}
}
需求 键盘录入一个字符串,要求统计字符中每个字符串出现的次数 举例:键盘录入“aababcabcdabcde”在控制台输出“a(5)b(4)c(3)d(2)e(1)”
思路 1.键盘录入一个字符串 2.创建HashMap集合,键是Character,值是Integer 3.遍历字符串得到每一个字符 4.得到的每一个字符作为键到HashMap集合中去找对应的值,看其返回值 如果返回值是null:说明该字符在HashMap集合中不存在,就把该字符作为键,1作为值存储 如果返回值不是null,说明该字符在HashMap集合中不存在,把该值加1,然后重新存储该字符和对应的值 5.遍历HashMap集合,得到键和值,按照要求进行拼接 6.输出结果
public class HashMapDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
//创建HashMap集合
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i < line.length(); i++) {
char key = line.charAt(i);
Integer value = hm.get(key);
if(value==null){
hm.put(key,1);
}else{
value++;
hm.put(key,value);
}
}
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);
}
}
需求:ArrayList存储学生对象,使用Collections对ArrayList进行排序
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
package heimaStudy.arrayList;
import heimaStudy.Test2.Student;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionsDemo2 {
public static void main(String[] args) {
ArrayList<Student> stu = new ArrayList<>();
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 17);
Student s3 = new Student("王二", 19);
stu.add(s1);
stu.add(s2);
stu.add(s3);
Collections.sort(stu, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getAge()-s2.getAge();
int num1=num==0?s1.getName().compareTo(s2.getName()):num;
return num1;
}
});
for (Student s:stu){
System.out.println(s.getName()+" ,"+s.getAge());
}
}
}
//学生类
public class Student {
private String name;
private Integer age;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
需求:编写一个程序,获取10个1-20之间的随机数,要求随机数不能重复,并在控制台输出 思路: 1.创建Set集合对象 2.创建随机数对象 3.判断集合长度是不是小于10 是:产生一个随机数添加到集合 回到3继续 4.遍历集合
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class SetDemo1 {
public static void main(String[] args) {
//HashSet不排序
// Set<Integer> set = new HashSet<Integer>();
//TreeSet可排序
Set<Integer> set = new TreeSet<>();
Random r = new Random();
while (set.size()<10){
int num = r.nextInt(20)+1;
set.add(num);
}
for (Integer i:set){
System.out.println(i);
}
}
}
比较器排序comparator的使用
存储学生对象并遍历,创建TreeSet集合使用带参构造方法
要求:按照年龄从小到大排序,年龄相同时按照姓名的字母顺序排序
package heimaStudy.Test4;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo2 {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getAge()-s2.getAge();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
Student stu1 = new Student("张三",18);
Student stu2 = new Student("李四",16);
Student stu3 = new Student("王五",19);
Student stu4 = new Student("赵二",21);
Student stu5 = new Student("孙达",16);
Student stu6 = new Student("赵二",21);
ts.add(stu1);
ts.add(stu2);
ts.add(stu3);
ts.add(stu4);
ts.add(stu5);
ts.add(stu6);
//遍历
for(Student s:ts){
System.out.println(s.getName()+" ,"+s.getAge());
}
}
}
//学生类
public class Student {
private String name;
private Integer age;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}