package exer;
/**
*
* @create 2022-04-08 16:40
*/
public class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public int compareTo(MyDate o) {
int minusYear=this.getYear()-o.getYear();
if(minusYear!=0){
return minusYear;
}
int minusMonth=this.getMonth()-o.getMonth();
if(minusMonth!=0){
return minusMonth;
}
return this.getDay()-o.getDay();
}
}
package exer;
/**
*
*
*
* @create 2022-04-08 16:46
*/
public class Employee implements Comparable<Employee>{
private String name;
private int age;
private MyDate birthday;
public Employee() {
}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "exer.Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
}
package exer;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
*
*
* @create 2022-04-08 17:00
*/
public class EmployeeTest {
@Test
public void test2(){
TreeSet<Employee> set=new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
MyDate b1=o1.getBirthday();
MyDate b2=o2.getBirthday();
return b1.compareTo(b2);
}
});
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator<Employee> iterator=set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void test1(){
TreeSet<Employee> set=new TreeSet<>();
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator<Employee> iterator=set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
package java0;
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
*
* @create 2022-04-08 17:36
*/
public class GenericTest {
@Test
public void test1(){
Map<String,Integer> map=new HashMap<>();
map.put("Tom",87);
map.put("Jerry",87);
map.put("Jack",67);
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
while(iterator.hasNext()){
Map.Entry<String, Integer> e = iterator.next();
String key=e.getKey();
Integer value=e.getValue();
System.out.println(key+"----->"+value);
}
}
}
package java0;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @create 2022-04-08 17:51
*/
public class GenericTest1 {
@Test
public void test1(){
//如果定义了泛型类,实例化没有指明类的泛型,则认为此泛型类型为Object类型。
Order order=new Order();
order.setOrderT(123);
order.setOrderT("ABC");
Order<String> order1=new Order<>("orderAA",1001,"order:AA");
order1.setOrderT("AA:hello");
}
/*
子类不保留父类的泛型:
1)没有类型
class Son extends Father{}
等价于 class Son extends Father<Object,Object>{}
2)具体类型
class Son extends Father<Integer,String>{}
子类保留父类的泛型:
1)全部保留
class Son<T1,T2> extends Father<T1,T2>{}
2)部分保留
class Son<T2> extends Father<Integer,T2>{}
子类不保留父类的泛型
1)没有类型
class Son<A,B> extends Father{}
等价于class Son extends Father<Object,Object>{}
2)具体类型
class Son<A,B> extends Father<Integer,String>{}
子类保留父类的泛型
1)全部保留
class Son<T1,T2,A,B> extends Father<T1,T2>{}
2)部分保留
class Son<T2,A,B> extends Father<Integer,T2>{}
*/
@Test
public void test2(){
SubOrder sub1=new SubOrder();
sub1.setOrderT(1111);
SubOrder1<Integer> sub2=new SubOrder1<>();
sub2.setOrderT(12312);
}
@Test
public void test3(){
ArrayList<String> list1=null;
ArrayList<Integer> list2=new ArrayList<>();
//泛型不同的引用不能相互赋值
// list1=list2;会报错
}
@Test
public void test4(){
Order<String> order=new Order<>();
Integer[] arr=new Integer[]{1,2,3,4};
List<Integer> list=order.copy(arr);
System.out.println(list);
}
}
package java0;
/**
*
*
* @create 2022-04-08 18:45
*/
public class SubOrder1<T> extends Order<T> {
//SubOrder1是泛型类
}
package java0;
/**
*
*
* @create 2022-04-08 18:40
*/
public class SubOrder extends Order<Integer> {//SubOrder:不是泛型类
}
package java0;
import java.util.ArrayList;
import java.util.List;
/**
*
* 自定义泛型类
* @create 2022-04-08 17:54
*/
public class Order<T> {
String orderName;
int orderId;
T orderT;
public Order(){
T[] arr=(T[]) new Object[10];
}
public Order(String orderName,int orderId,T orderT){
this.orderName=orderName;
this.orderId=orderId;
this.orderT=orderT;
}
public T getOrderT(){
return orderT;
}
public void setOrderT(T orderT){
this.orderT=orderT;
}
@Override
public String toString() {
return "Order{" +
"orderName='" + orderName + '\'' +
", orderId=" + orderId +
", orderT=" + orderT +
'}';
}
//静态方法中不能使用类的泛型,原因:类的生命周期
//泛型方法:在方法中出现了泛型的结构,泛型参数与类的泛型参数没有任何关系。
//泛型方法:可以声明为静态的。s
public static <E> List<E> copy(E[] arr){
ArrayList<E> list=new ArrayList<>();
for(E e:arr){
list.add(e);
}
return list;
}
}
package java2;
import org.junit.Test;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @create 2022-04-10 12:14
*/
public class GenericTest {
/*
泛型在继承方面的体现
虽然类A是类B的父类,但是G<A>和G<B>二者不具备子父类关系,二者是并列关系。
若类A是类B的父类,A<G>是B<G>的父类。
*/
@Test
public void test1(){
Object obj=null;
Object str=null;
obj=str;
Object[] arr1=null;
String[] arr2=null;
arr1=arr2;
List<Object> list1=null;
List<String> list2=new ArrayList<>();
//此时的list1和list2的类型不具有子父类关系
// list1=list2; //编译不通过
}
@Test
public void test2(){
AbstractList<String> list1=null;
List<String> list2=null;
ArrayList<String> list3=null;
list1=list3;
list2=list3;
list2=list1;
}
/*
通配符的使用:
通配符:?
类A是类B的父类,G<A> 和G<B>是没有关系的,二者共同的父类是:G<?>
*/
@Test
public void test3(){
List<Object> list1=null;
List<String> list2=null;
List<?> list=null;
list=list1;
list=list2;
List<String> list3=new ArrayList<>();
list3.add("AA");
list3.add("BB");
list3.add("CC");
list=list3;
//list<?>不能向其内部添加数据,除了null之外。
// list.add("DD"); //编译不通过
list.add(null);
//获取:允许list获取数据,读取的数据类型为Object。
Object o=list.get(0);
System.out.println(o);
}
/*
有限制条件的通配符的使用:
? extends A:
G<? extends A> 可以作为G<A>和G<B>的父类,其中B是A的子类。
? super A:
G<? super A> 可以作为G<A>和G<B>的父类,其中B是A的父类。
*/
@Test
public void test4(){
List<? extends Person> list1=null;
List<? super Person> list2=null;
List<Student> list3=new ArrayList<>();
List<Person> list4=new ArrayList<>();
List<Object> list5=new ArrayList<>();
list1=list3;
list1=list4;
// list1=list5;//编译不通过
// list2=list3;//编译不通过
list2=list4;
list2=list5;
//读取数据
list1=list3;
Person p=list1.get(0);
list2=list4;
Object obj=list2.get(0);
//写入数据
// list1.add(new Person());//编译不通过
list2.add(new Person());
list2.add(new Student());
}
}
package exer1;
/**
*
* 定义一个 User 类:
该类包含:private成员变量(int类型) id,age;(String 类型)name。
*
* @create 2022-04-10 18:32
*/
public class User {
private int id;
private int age;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (id != user.id) return false;
if (age != user.age) return false;
return name != null ? name.equals(user.name) : user.name == null;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + age;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
package exer1;
import java.util.*;
/**
*
*定义个泛型类 DAO<T>,在其中定义一个Map 成员变量,Map 的键为 String 类型,值为 T 类型。
分别创建以下方法:
public void save(String id,T entity): 保存 T 类型的对象到 Map 成员变量中
public T get(String id):从 map 中获取 id 对应的对象
public void update(String id,T entity):替换 map 中key为id的内容,改为 entity 对象
public List<T> list():返回 map 中存放的所有 T 对象
public void delete(String id):删除指定 id 对象
* @create 2022-04-10 18:34
*/
public class DAO<T> {
private HashMap<String,T> map=new HashMap<>();
//保存T类型的对象到Map成员变量中
public void save(String id,T entity){
map.put(id,entity);
}
//从map中获取id对应的对象
public T get(String id){
return map.get(id);
}
//替换map中key为id的内容,改为entity对象
public void update(String id,T entity){
if(map.containsKey(id)){
map.put(id,entity);
}
}
//返回map中存放的所有T对象
public List<T> list(){
ArrayList<T> list=new ArrayList<>();
Collection<T> values=map.values();
for(T t:values){
list.add(t);
}
return list;
}
//删除指定id对象
public void delete(String id){
map.remove(id);
}
}
package exer1;
import java.util.List;
/**
*
*
* @create 2022-04-10 18:45
*/
public class DAOTest {
public static void main(String[] args) {
DAO<User> dao=new DAO<>();
dao.save("1001",new User(1001,999,"太阳骑士"));
dao.save("1002",new User(1002,444,"帕奇"));
dao.save("1003",new User(1003,555,"爷"));
dao.update("1003",new User(1003,111,"朕"));
dao.delete("1002");
List<User> list = dao.list();
list.forEach(System.out::println);
}
}
package java3;
import org.junit.Test;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
/**
*
*
* @create 2022-04-10 19:48
*/
public class FileTest {
/*
1.如何创建File类的实例
File(String filePath)
File(String parentPath,String childPath)
File(File parentFile,String childPath)
2.
相对路径:相较于某个路径下,指明的路径
绝对路径:包含盘符在内的文件或文件目录的路径
3.路径分隔符
windows:\\
unix:/
*/
@Test
public void test1(){
//构造器1
File file1=new File("hello.txt");
File file2=new File("D:\\java\\workspace_idea\\ioTest\\hi.txt");
System.out.println(file1);
System.out.println(file2);
}
/*
pubilc String getAbsolutePath():获取绝对路径
public String getPath():获取路径
pubilc String getName():获取名称
public String getParent():获取上层文件目录路径。若无,返回null
public long length():获取文件长度(即:字节数)。不能获取目录的长度
public long lastModified():获取最后一次的修改时间,毫秒值
适用于文件目录的方法:
public String[] list():获取指定目录下的所有文件或者文件目录的名称(String类型)数组
public File[] listFiles():获取指定目录下的所有文件或者文件目录的File数组。
*/
@Test
public void test2(){
File file1=new File("hello.txt");
File file2=new File("D:\\java\\workspace_idea\\ioTest\\hi.txt");
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(file1.lastModified());
System.out.println();
System.out.println(file2.getAbsoluteFile());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
@Test
public void test3(){
File file=new File("D:\\java\\workspace_idea");
String[] list=file.list();
for(String s:list){
System.out.println(s);
}
System.out.println();
File[] files=file.listFiles();
for(File f:files){
System.out.println(f);
}
}
/*
public boolean renameTo(File dest):把文件重命名为指定的文件路径
比如:file1.renameTo(file2)为例
要想保证返回true,需要file1在硬盘中是存在的,且file2不能在硬盘中存在。
*/
@Test
public void test4(){
File file1=new File("hello.txt");
File file2=new File("D:\\java\\workspace_idea\\ioTest\\hi.txt");
boolean renameTo=file2.renameTo(file1);
System.out.println(renameTo);
}
/*
public boolean isDirectory():判断是否是文件目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
pubilc boolean isHidden():判断是否隐藏
*/
@Test
public void test5(){
File file1=new File("hello.txt");
file1=new File("hello1.txt");
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.exists());
System.out.println(file1.canRead());
System.out.println(file1.canWrite());
System.out.println(file1.isHidden());
System.out.println();
File file2=new File("D:\\java\\workspace_idea\\ioTest");
file2=new File("D:\\java\\workspace_idea\\ioTest1");
System.out.println(file2.isDirectory());
System.out.println(file2.isFile());
System.out.println(file2.exists());
System.out.println(file2.canRead());
System.out.println(file2.canWrite());
System.out.println(file2.isHidden());
}
/*
创建硬盘中对应的文件或文件目录
public boolean createNewFile():创建文件。若文件存在,则不创建,返回false.
public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,不创建。
public boolean mkdirs():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,一并创建
删除磁盘中的文件或文件目录
public boolean delete():删除文件或者文件夹
删除注意事项:java中的删除不走回收站。
*/
@Test
public void test6() throws IOException {
File file1=new File("hi.txt");
if(!file1.exists()){
file1.createNewFile();
System.out.println("创建成功!");
}else{
file1.delete();
System.out.println("删除成功");
}
}
@Test
public void test7(){
//文件目录的创建
File file1=new File("D:\\java\\workspace_idea\\ioTest1\\222");
boolean mkdir=file1.mkdir();
if(mkdir){
System.out.println("创建成功1");
}
File file2=new File("D:\\java\\workspace_idea\\ioTest1\\222");
boolean mkdir2=file2.mkdirs();
if(mkdir2){
System.out.println("创建成功2");
}
//删除文件
File file3=new File("D:\\java\\workspace_idea\\ioTest1\\222");
System.out.println(file3.delete());
}
}