java练习
学习Java,可考取Oracle的Java SE或EE认证 #生活技巧# #工作学习技巧# #技能培训认证#
1. 定义一个 Employee 类。
该类包含: private 成员变量 name,age,birthday ,其中 birthday 为
MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday
MyDate 类包含 :
private 成员变量 year,month,day ;并为每一个属性定义 getter, setter
方法;
创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:
TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使 Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
public class Employee implements Comparable<Employee>{
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Employee() {
}
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 "Employee{" + "name='" + name + '\'' + ", age=" + age + ", birthday=" + birthday + '}';
}
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
}
public class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public MyDate() {
}
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 m) {
int minusYear = this.getYear() - m.getYear();
if(minusYear != 0){
return minusYear;
}
int minusMonth = this.getMonth() - m.getMonth();
if(minusMonth != 0){
return minusMonth;
}
return this.getDay() - m.getDay();
}
}
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class MyTest {
@Test
public void test1(){
TreeSet<Employee> set = new TreeSet<Employee>();
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()){
Employee employee = iterator.next();
System.out.println(employee);
}
//Employee{name='guofucheng', age=44, birthday=MyDate{year=1987, month=5, day=9}}
//Employee{name='liangzhaowei', age=21, birthday=MyDate{year=1978, month=12, day=4}}
//Employee{name='liming', age=51, birthday=MyDate{year=1954, month=8, day=12}}
//Employee{name='liudehua', age=55, birthday=MyDate{year=1965, month=5, day=4}}
//Employee{name='zhangxueyou', age=43, birthday=MyDate{year=1987, month=5, day=4}}
}
@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());
}
//Employee{name='liming', age=51, birthday=MyDate{year=1954, month=8, day=12}}
//Employee{name='liudehua', age=55, birthday=MyDate{year=1965, month=5, day=4}}
//Employee{name='liangzhaowei', age=21, birthday=MyDate{year=1978, month=12, day=4}}
//Employee{name='zhangxueyou', age=43, birthday=MyDate{year=1987, month=5, day=4}}
//Employee{name='guofucheng', age=44, birthday=MyDate{year=1987, month=5, day=9}}
}
}
定义个泛型类 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 对象
定义一个 User 类:
该类包含: private 成员变量( int 类型)
id , age ;( String 类型) name 。
定义一个测试类:
创建 DAO 类的对象,
分别调用其 save 、 get 、 update 、 list 、 delete 方
法来操作 User 对象,
使用 Junit 单元测试类进行测试。
import java.util.*;
public class DAO<T> {
private Map<String,T> map = new HashMap<String,T>();
//保存 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);
}
}
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 + '\'' +
'}';
}
}
import org.junit.Test;
import java.util.List;
public class DAOtest {
@Test
public void test(){
DAO<User> dao=new DAO<User>();
dao.save("1001",new User(1001,34,"周杰伦"));
dao.save("1002",new User(1002,20,"昆凌"));
dao.save("1003",new User(1003,25,"蔡依林"));
dao.update("1003",new User(1003,30,"方文山"));
List<User> list = dao.list();
list.forEach(System.out::println);
}
}
User{id=1003, age=30, name='方文山'}
User{id=1002, age=20, name='昆凌'}
User{id=1001, age=34, name='周杰伦'}
网址:java练习 https://www.yuejiaxmz.com/news/view/733166
相关内容
JAVA学习五2020年Java实习:实战攻略与职场生存指南
java I/O学习(二)
10年Java面试总结:Java程序员面试必备的面试技巧
Java技能提升指南
Java并发编程深入学习指南
Java基于Java的运动健身平台(源码+mysql+文档)
java ip地址 split
java软件工程师
Java 零基础入门学习(小白也能看懂!)