续(Map)
案例1:Map,Student学生信息遍历(value是Student类)
package arrays;
/*Student类*/
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 arrays;
/*两种遍历方式*/
import java.util.*;
public class Demo {
public static void main(String[] args) {
//创建集合对象
HashMap<String, Student> hm = new HashMap<>();
Student s1 = new Student("wen", 20);
Student s2 = new Student("tian", 30);
Student s3 = new Student("sheng", 40);
//Student s4 = new Student("aka",60);
hm.put("cczu001", s1);
hm.put("cczu002", s2);
hm.put("cczu003", s3);
//遍历1 键找值
Set<String> keyset = hm.keySet();
for (String key : keyset) {
Student s = hm.get(key);
System.out.println(key + ',' + s.getName() + ',' + s.getAge());
}
System.out.println("----------------");
//遍历2 键值对
for (Map.Entry<String, Student> me : hm.entrySet()) {//hm.entrySet()表示所有键值对的集合
//Map.Entry<String, Student>表示数据类型
String key = me.getKey(); //获取键值
System.out.println(key + ',' + me.getValue().getName() + ',' + me.getValue().getAge());
}
}
}
案例2:Map,Student学生信息遍历(String是Student,保证唯一性)
package arrays;
import java.util.Objects;
/*Student类*/
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
//创建集合对象
HashMap<Student, String> hm = new HashMap<>();
Student s1 = new Student("wen", 20);
Student s2 = new Student("tian", 30);
Student s3 = new Student("sheng", 40);
Student s4 = new Student("wen", 20);
//如果不重写Student中的方法,键相同就会覆盖
hm.put(s1, "常州");
hm.put(s2, "上海");
hm.put(s3, "北京");
hm.put(s4, "成都");
//遍历
Set<Student> keySet = hm.keySet();//键集合
for (Student key : keySet) {
System.out.println(key.getName() + ',' + key.getAge() + ',' + hm.get(key));
}
/* wen,20,常州
sheng,40,北京
tian,30,上海
wen,20,成都*/ //没有保证键的唯一性,按道理成都覆盖常州
// 重写方法后保证键的唯一性
// wen,20,成都
// sheng,40,北京
// tian,30,上海
}
}
案例3:集合嵌套ArrayList存储HashMap元素,遍历
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
//创建集合对象
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
//创建hashmap集合
HashMap<String, String> hm1 = new HashMap<>();
hm1.put("wts1", "wen");
hm1.put("wts2", "tian");
hm1.put("wts3", "sheng");
arrayList.add(hm1);
HashMap<String, String> hm2 = new HashMap<>();
hm2.put("lx1", "li");
hm2.put("lx2", "xiao");
arrayList.add(hm2);
HashMap<String, String> hm3 = new HashMap<>();
hm3.put("xwb1", "xia");
hm3.put("xwb2", "wen");
hm3.put("xwb3", "biao");
arrayList.add(hm3);
//增强for遍历
for (HashMap<String, String> hashMap : arrayList) {
for (Map.Entry<String, String> entryset : hashMap.entrySet()) {
String key = entryset.getKey();
String value = entryset.getValue();
System.out.println(key + value);
// wts3sheng
// wts2tian
// wts1wen
// lx1li
// lx2xiao
// xwb1xia
// xwb2wen
// xwb3biao
}
}
}
}
案例4:HashMap存储ArrayList集合,遍历
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
//创建集合对象
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("关羽");
arrayList1.add("刘备");
arrayList1.add("张飞");
hashMap.put("三国演义", arrayList1);
ArrayList<String> arrayList2 = new ArrayList<>();
arrayList2.add("武松");
arrayList2.add("鲁智深");
arrayList2.add("宋江");
hashMap.put("水浒", arrayList2);
ArrayList<String> arrayList3 = new ArrayList<>();
arrayList3.add("孙悟空");
arrayList3.add("猪八戒");
arrayList3.add("唐僧");
hashMap.put("西游记", arrayList3);
//遍历
for (Map.Entry<String, ArrayList<String>> entry : hashMap.entrySet()) {
System.out.println(entry.getKey());
for (String arr : entry.getValue()) {
System.out.println("\t" + arr);
//水浒
// 武松
// 鲁智深
// 宋江
//三国演义
// 关羽
// 刘备
// 张飞
//西游记
// 孙悟空
// 猪八戒
// 唐僧
}
}
}
}
案例5:统计字符串每个字符出现的次数
思路,用键值覆盖的思维
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
//键盘输入
Scanner sc = new Scanner(System.in);
System.out.print("输入一个字符串:");
String s = sc.nextLine();
//创建HashMap
HashMap<Character, Integer> hashMap = new HashMap<>();
//遍历字符串得到每一个值
for (int i = 0; i < s.length(); i++) {
char key = s.charAt(i);
Integer value = hashMap.get(key);
if (value == null) {//不存在,设置为1
hashMap.put(key, 1);
} else {//存在,加一
value++;
hashMap.put(key, value);
}
}
StringBuilder sb = new StringBuilder();
Set<Character> set = hashMap.keySet();
for (Map.Entry<Character, Integer> entry : hashMap.entrySet()) {
sb.append(entry.getKey()).append('(').append(entry.getValue()).append(')');
}
String ss = sb.toString();
System.out.println(ss);
//aaabbc
//a(3)b(2)c(1)
}
}
Collections
1. Collections概述和使用
- 针对集合操作的工具类
常用方法:
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(20);
list.add(10);
list.add(50);
list.add(40);
//指定列表升序排列
Collections.sort(list);
System.out.println(list);//[10, 20, 30, 40, 50]
//指定列表顺序反转
Collections.reverse(list);
System.out.println(list);//[50, 40, 30, 20, 10]
//使用默认的随即源,随机排列指定列表
Collections.shuffle(list);
System.out.println(list);//[10, 50, 20, 30, 40] //[10, 20, 50, 40, 30]
}
}
案例:ArrayList学生对象(年龄升序)
比较器(匿名内部类)
package arrays;
import java.util.*;
public class Demo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("wts", 20);
Student s2 = new Student("xwb", 10);
Student s3 = new Student("cyc", 50);
Student s4 = new Student("ljc", 20);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Collections.sort(list, new Comparator<Student>() {//Collections.sort()比较器实现
@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 num;
}
});
for (Student s : list) {
System.out.println(s.getName() + ',' + s.getAge());
}
}
}
File
1. File的概述和使用方法
概述:
- 文件和目录是可以通过File封装成对象的
- 对File而言,封装的仅仅是一个路径名。将来要通过具体的操作,把路径内容转换成具体存在
常用方法:
三个表示一个意思:
2. File的创建功能
有手就行
3. File类判断和获取功能
4. File类的删除
5. 递归
有手就行
package file;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("输入一个数:");
int k = sc.nextInt();
System.out.println(k + "的阶乘是" + jc(k));
}
public static int jc(int n) {
if (n == 1) {
return 1;
} else {
return n * jc(n - 1);
}
}
}
案例:查看文件夹目录下所有文件
package file;
import java.io.File;
public class Demo {
public static void main(String[] args) {
File srcfile = new File("G:\\Program\\java_basic_learning");
getAllFilePath(srcfile);
}
public static void getAllFilePath(File f) {
File[] arrays = f.listFiles();
if (arrays != null) {
for (File file : arrays) {
if (file.isDirectory()) {//判断文件是否是目录
//是,递归
getAllFilePath(file);
} else {
//不是,输出文件绝对路径
System.out.println(file.getAbsolutePath());
}
}
}
}
}