泛型

public class A03 {
   
	@SuppressWarnings("all")
	public static void main(String[] args) {
   
		ArrayList<Employee> arr = new ArrayList<>();
		arr.add(new Employee("ada", 1000, new MyDate(1998,11,9)));
		arr.add(new Employee("ada", 1000, new MyDate(1999,11,9)));
		arr.add(new Employee("ada", 1000, new MyDate(1998,11,9)));
		arr.add(new Employee("tom", 1000, new MyDate(1998,11,9)));
		System.out.println(arr);
		
		arr.sort(new Comparator<Employee>() {
   

			@Override
			public int compare(Employee emp1, Employee emp2) {
   
				//先对传入的参数进行验证
				if(!(emp1 instanceof Employee && emp2 instanceof Employee)){
   
					System.out.println("参数类型不匹配");
					return 0;
				}
				//比较名字
				int i = emp1.getName().compareTo(emp2.getName());
				if( i != 0){
   
					return i;
				}
				/** * 下面由于是对MyData的比较,因此放(改进改写)到MyData类中 */

// //如果名字相同,就比较 birthday - year
// int yearMinus = emp1.getBirthday().getYear() - emp2.getBirthday().getYear();
// if( yearMinus != 0){
   
// return yearMinus;
// }
// //如果 birthday - year相同,就比较 birthday - month
// int monthMinus = emp1.getBirthday().getMonth() - emp2.getBirthday().getMonth();
// if( monthMinus != 0){
   
// return monthMinus;
// }
// //如果 birthday - month相同,就比较 birthday - day
// return emp1.getBirthday().getDay() - emp2.getBirthday().getDay();
				return emp1.getBirthday().compareTo(emp2.getBirthday());
			}
		});
		System.out.println("排序后的结果");
		System.out.println(arr);
	}
}

class Employee{
   
	private String name;
	private int sal;
	private MyDate birthday;
	public String getName() {
   
		return name;
	}
	public void setName(String name) {
   
		this.name = name;
	}
	public int getSal() {
   
		return sal;
	}
	public void setSal(int sal) {
   
		this.sal = sal;
	}
	public MyDate getBirthday() {
   
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
   
		this.birthday = birthday;
	}
	public Employee(String name, int sal, MyDate birthday) {
   
		super();
		this.name = name;
		this.sal = sal;
		this.birthday = birthday;
	}
	@Override
	public int hashCode() {
   
		final int prime = 31;
		int result = 1;
		result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
   
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (birthday == null) {
   
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (name == null) {
   
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public String toString() {
   
		return "Employee [name=" + name + ", sal=" + sal + ", birthday=" + birthday + "]" + "\n";
	}
	
}

class MyDate implements Comparable<MyDate>{
   
	private int year;
	private int month;
	private int 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;
	}

	public MyDate(int year, int month, int day) {
   
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	@Override
	public String toString() {
   
		return "MyDate [year=" + year + ", month=" + month + ", day=" + day + "]";
	}

	@Override
	public int hashCode() {
   
		final int prime = 31;
		int result = 1;
		result = prime * result + day;
		result = prime * result + month;
		result = prime * result + year;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
   
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyDate other = (MyDate) obj;
		if (day != other.day)
			return false;
		if (month != other.month)
			return false;
		if (year != other.year)
			return false;
		return true;
	}

	@Override
	public int compareTo(MyDate o) {
   
		//如果名字相同,就比较 birthday - year
		int yearMinus = year - o.getYear();
		if( yearMinus != 0){
   
			return yearMinus;
		}
		//如果 birthday - year相同,就比较 birthday - month
		int monthMinus = month - o.getMonth();
		if( monthMinus != 0){
   
			return monthMinus;
		}
		//如果 birthday - month相同,就比较 birthday - day
		return day - o.getDay();		
	}		
}

public class A01 {
   
	@Test
	//用Junit不必在主函数里面啦~
	public void testList(){
   
		//给泛型T 指定类型User
		DAO<User> dao = new DAO<>();
		dao.save("001", new User(1, 10, "jack"));
		dao.save("002", new User(2, 17, "king"));
		dao.save("003", new User(3, 23, "ada"));
		
		System.out.println(dao.list());
		dao.delete("001");
		System.out.println(dao.list());
		dao.get("002");
		System.out.println(dao.list());
		dao.update("003", new User(3, 27, "lily"));
		System.out.println(dao.list());
	}
}
class DAO<T>{
   
	private Map<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){
   
		map.put(id, entity);
	}
	
	//返回Map中存放的所有T对象
	public List<T> list(){
   
		ArrayList<T> list = new ArrayList<>();
		//遍历map
		Set<String> keySet = map.keySet();
		for(String key: keySet){
   
			list.add(get(key));//直接使用本类的get方法
// list.add(map.get(key));
		}
		return list;		
	}
	
	//删除指定id对象
	public void delete(String id){
   
		map.remove(id);
	}
}
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) {
   
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
   
		return "\n"+"User [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	
}


线程不同步

//使用多线程,模拟三个窗口同时售票100张
public class A02 {
   
	public static void main(String[] args) {
   
		//测试
// SellTicket01 sellTicket01 = new SellTicket01();
// SellTicket01 sellTicket02 = new SellTicket01();
// SellTicket01 sellTicket03 = new SellTicket01();
// 
// //启动三个售票窗口(线程)
// sellTicket01.start();
// sellTicket02.start();
// sellTicket03.start();
		
		SellTicket02 sellTicket02 = new SellTicket02();
		new Thread(sellTicket02).start();//启动第一个线程
		new Thread(sellTicket02).start();
		new Thread(sellTicket02).start();
	}
}
//使用Thread方式
class SellTicket01 extends Thread{
   
	private int ticketNum = 100; //让三个窗口(线程)共享票数
	@Override
	public void run() {
   
		while(true){
   
			if(ticketNum <= 0){
   
				System.out.println("售票结束……");
				break;
			}
			
			//休眠59ms
			try {
   
				Thread.sleep(50);
			} catch (InterruptedException e) {
   
				e.printStackTrace();
			}
			System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票"
					+ "剩余票数:" + (--ticketNum));
		}
	}
}

//实现接口
class SellTicket02 implements Runnable{
   
	private static int ticketNum = 100; //让三个窗口(线程)共享票数
	@Override
	public void run() {
   
		while(true){
   
			if(ticketNum <= 0){
   
				System.out.println("售票结束……");
				break;
			}
			
			//休眠59ms
			try {
   
				Thread.sleep(50);
			} catch (InterruptedException e) {
   
				e.printStackTrace();
			}
			System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票"
					+ "剩余票数:" + (--ticketNum));
		}
	}
}

public class A04{
   
	public static void main(String[] args) throws InterruptedException {
   
		Thread t = new Thread(new T());
		for(int i = 0; i < 10; i++){
   
			System.out.println("hi");
			if(i == 5){
   
				t.start();
				t.join();
			}
		}
		System.out.println("主线程结束");
	}
}

class T implements Runnable{
   
	private int count = 0;
	@Override
	public void run() {
   		
		while(true){
   			
			System.out.println("hello" + (++count));
			try {
   
				Thread.sleep(1000);
			} catch (InterruptedException e) {
   
				e.printStackTrace();
			}
			if(count == 10){
   
				System.out.println("子线程结束");
				break;				
			}			
		}
	}	
}


public class A05 {
   
	public static void main(String[] args) {
   
		new Thread(new T1()).start();
		new Thread(new T2(new T1())).start();//把线程T1传给T2
	}
}

class T1 implements Runnable{
   
	private boolean loop = true;

	public void setLoop(boolean loop) {
   
		this.loop = loop;
	}

	@Override
	public void run() {
   	
		while(loop){
   
			System.out.println(1 + (int)(Math.random() * 100));
			try {
   
				Thread.sleep(1000);
			} catch (InterruptedException e) {
   
				e.printStackTrace();
			}
		}
		
	}	
}

class T2 implements Runnable{
   
	private T1 t;
	private Scanner scanner = new Scanner(System.in);
	
	public T2(T1 t) {
   //构造器中直接传入T1对象
		super();
		this.t = t;
	}

	@Override
	public void run() {
   	
		while(true){
   
			System.out.println("请输入你的指令(Q)表示退出:");
			char key = scanner.next().toUpperCase().charAt(0);
			if(key == 'Q'){
   
				//以通知的方式终止T1线程
				t.setLoop(false);
				break;
			}
		}
	}	
}

public class A06 {
   
	public static void main(String[] args) {
   
		new Thread(new Draw()).start();
		new Thread(new Draw()).start();
	}
}

class Draw implements Runnable{
   //涉及到多线程,使用实现接口的方法
	public static int money = 10000;
	@Override
	public void run() {
   
		synchronized (this) {
   
			while(money >= 1000){
   
				money -= 1000;
				System.out.println("当前余额为:" + money);
			}	
		}		
	}
}

public class A07 {
   
	public static void main(String[] args) throws Exception {
   
		String directoryPath = "e\\te";
		File file = new File(directoryPath);
		
		//目录是否存在
		if(!file.exists()){
   
			//创建目录
			if(file.mkdirs()){
   
				System.out.println("创建"+ directoryPath +"成功");
			}else{
   
				System.out.println("创建"+ directoryPath +"失败");
			}
		}
		
		//文件是否存在
		String filePath = directoryPath + "\\hello.txt";
		file = new File(filePath);
		if(!file.exists()){
   
			//创建文件
			if(file.createNewFile()){
   
				System.out.println("创建"+ filePath +"成功");
				//写入内容
				BufferedWriter br = new BufferedWriter(new FileWriter(file));
				br.write("hello world");
				br.close();
			}else{
   
				System.out.println("创建"+ filePath +"失败");
			}
		}else{
   
			System.out.println("文件已经存在");
		}
	}
}

public class A08 {
   
	public static void main(String[] args) throws Exception {
   
		BufferedReader br = null;
		String lines = "";
		int lineNum = 0;
		
		File file = new File("C:\\Users\\Desktop\\a.txt");
		br = new BufferedReader(new FileReader(file));
		while((lines = br.readLine()) != null){
   
			System.out.println( ++lineNum + lines);
		}
		br.close();
	}
}


eclipse中properties的写入
new——file——输入名称:dog.properties——再输入即可

public class A09 {
   
	public static void main(String[] args) throws Exception, IOException {
   
		String filePahth = "src\\experiments\\dog.properties";
		
		//创建对象
		Properties properties = new Properties();
		//对象加载
		properties.load(new FileReader(filePahth));
		//输出
		properties.list(System.out);
		String name = (String) properties.get("name");
		int age = Integer.parseInt((String)properties.get("age"));
		String color = (String) properties.get("color");
		
		Dog dog = new Dog(name, age, color);
		System.out.println("=== dog 对象的信息=====");
		System.out.println(dog);
		
		//序列化 
		String serFilePath = "src\\experiments\\dog.dat";
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
		oos.writeObject(dog);
		
		//关闭流
		oos.close();
		System.out.println("dog序列化完成");
	}
	//反序列化
	@Test
	public void m1() throws Exception{
   
		String serFilePath = "src\\experiments\\dog.dat";
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath));
		Dog dog = (Dog) ois.readObject();
		System.out.println("反序列化后dog信息" + dog);
		ois.close();//一定要关闭流!!!
	}
}

class Dog implements Serializable{
   
	private String name;
	private int age;
	private String color;
	public Dog(String name, int age, String color) {
   
		super();
		this.name = name;
		this.age = age;
		this.color = color;
	}
	@Override
	public String toString() {
   
		return "Dog [name=" + name + ", age=" + age + ", color=" + color + "]";
	}
	
}