map集合的各个子类的区别
HashMap:线程不安全,但是,存储速度快;
HashTable:线程安全,但是,存储速度慢;
ConcurrentHashMap:线程安全,存储速度较快;
存储自定义对象
import java.util.HashMap;
import java.util.Objects;
public class Demo {
public static void main(String[] args) {
HashMap map= new HashMap();
Person p1 = new Person("张三",17);
Person p3 = new Person("张三",17);
map.put(p1,"你好呀!");
Person p2 = new Person("李四",17);
map.put(p2,"你好吗!");
System.out.println(map.get(p1));
System.out.println(map.get(p2));
System.out.println(map.get(p3));
System.out.println(p1.equals(p3));
}
public static class Person{
private String name;
private int age;
public Person() {
}
public Person(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;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
JDK9集合新特性
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<String> list = List.of("锄禾日当午", "汗滴禾下土");
for (String s:list) {
System.out.println(s);
}
}
}
java.io
i值得是input,o值得是output;
即输入和输出;
java.io.File
先讲io流的第一个类File
import java.io.File;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\c\\a\\b.text");
boolean mkdir = file.mkdir();
System.out.println(mkdir);
boolean mkdirs = file.mkdirs();
System.out.println(mkdirs);
}
}
文件遍历的操作
import java.io.File;
public class Demo1 {
public static void main(String[] args) {
File file = new File("C:\\Users\\HP");
File[] files = file.listFiles();
listFiles(files);
System.out.println("hh");
}
public static void listFiles(File[] files) {
if(files != null && files.length>0){
for (File file:files) {
if (file.isFile()) {
if (file.getName().endsWith(".mp4")) {
System.out.println("找到了一个mp4文件" + file.getAbsolutePath());
}
}else{
File[] files1 = file.listFiles();
listFiles(files1);
}
}
}
}
}
文件过滤器
import java.io.File;
import java.io.FileFilter;
public class Demo2 {
public static void main(String[] args) {
File file = new File("C:\\Users\\HP\\Videos\\Captures");
File[] files = file.listFiles();
listFile(files);
}
public static void listFile(File[] file){
Mp4Filter mp = new Mp4Filter();
for (File files:file) {
if (mp.accept(files)) {
if (files.isDirectory()) {
File[] files1 = files.listFiles();
listFile(files1);
} else if(files.getName().endsWith(".mp4")) {
System.out.println("找到了一个MP4 文件" + files.getAbsolutePath()+"文件大小:" + files.length()/1024/1024+"M");
}
}
}
}
public static class Mp4Filter implements FileFilter{
public boolean accept(File pathname) {
if(pathname.length()>100*1024*1024 || pathname.isDirectory()){
return true;
}
return false;
}
}
}