
代码过程:
复制代码
package com.sy.homework;
import java.util.Objects;
public class Student {
public String id = null; // 学号
public String name; // 姓名
public int age; // 年龄
public double score; // 成绩
public Student() {
}
public Student(String id, String name, int age, double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@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(id, student.id) && Objects.equals(name, student.name) && Objects.equals(score, student.score);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, score);
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", score='" + score + '\'' +
'}';
}
}
复制代码
package com.sy.homework;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("S001", "张三", 20, 95.5));
studentList.add(new Student("S002", "李四", 19, 88));
studentList.add(new Student("S003", "王五", 21, 95.5));
studentList.add(new Student("S002", "李四", 19, 88));
Set<Student> set = new LinkedHashSet<>(studentList);
List<Student> lists = new ArrayList<>(set);
lists.sort((s1, s2) ->{
int compare = Double.compare(s2.getScore(), s1.getScore());
if(compare != 0){
return compare;
}
return Integer.compare(s1.getAge(), s2.getAge());
});
System.out.println("===============去重+排序后学生列表==================");
for (Student student : lists) {
System.out.println(student);
}
}
}
代码过程:
复制代码
package com.sy.homework2;
public class Employee extends Person{
private String number; //工号
private String department; //部门
private int salary; //薪资
public Employee() {
}
public Employee(String name, int age, String number, String department, int salary) {
super(name, age);
this.number = number;
this.department = department;
this.salary = salary;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{姓名='" + getName() + "', 部门='" + department + "', 薪资=" + salary + "}";
}
}
复制代码
package com.sy.homework2;
public 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;
}
}
复制代码
package com.sy.homework2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Test {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("张三", 32 ,"1001","技术部",9500));
list.add(new Employee("李四", 30 ,"1002","技术部",12000));
list.add(new Employee("赵六", 31 ,"1003","市场部",7400));
list.add(new Employee("王五", 29 ,"1004","行政部",8500));
list.add(new Employee("小王", 28 ,"1005","市场部",7600));
list.add(new Employee("小张", 27 ,"1006","行政部",8300));
HashMap<String, List<Employee>> map = new HashMap<>();
for (Employee e : list) {
String dept = e.getDepartment();
if (!map.containsKey(dept)){
map.put(dept,new ArrayList<>());
}
map.get(dept).add(e);
}
System.out.println("=====部门薪资统计=====");
for (String string : map.keySet()) {
List<Employee> employees = map.get(string);
double total = 0;
for (Employee e : employees) {
total += e.getSalary();
}
// 部门平均薪资
double avg = total / employees.size();
System.out.println("部门:" + string + "; 总薪资:" + total + "; 平均薪资:" + avg );
}
System.out.println("=====薪资大于8000的员工=====");
ArrayList<Employee> arrayList = new ArrayList<>();
for (Employee emp : list) {
if (emp.getSalary() > 8000){
arrayList.add(emp);
}
}
//打印符合条件的员工
for (Employee emp : arrayList) {
System.out.println(emp);
}
System.out.println("=====技术部员工=====");
List<Employee> employeeList = map.get("技术部");
if (employeeList != null){
for (Employee emp : employeeList) {
System.out.println(emp);
}
}
}
}
代码过程:
复制代码
package com.sy.homework3;
import java.util.Objects;
public class Book {
private String ISBN;
private String name;// 书名
private String author; // 作者
private double price; // 价格
public Book() {
}
public Book(String ISBN, String name, String author, double price) {
this.ISBN = ISBN;
this.name = name;
this.author = author;
this.price = price;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Double.compare(price, book.price) == 0 && Objects.equals(ISBN, book.ISBN) && Objects.equals(name, book.name) && Objects.equals(author, book.author);
}
@Override
public int hashCode() {
return Objects.hash(ISBN, name, author, price);
}
@Override
public String toString() {
return "Book{ISBN='" + ISBN + "', 书名='" + name + "', 价格=" + price + "}";
}
}
复制代码
package com.sy.homework3;
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Book> book1 = new ArrayList<>();
book1.add(new Book("97801" , "Java编程思想" , "Rod" , 108));
book1.add(new Book("97802" , "深入理解JVM" , "Rod" , 89));
book1.add(new Book("97803" , "Spring实战" , "Rod" , 89));
book1.add(new Book("97803" , "Spring实战" , "Rod" , 89));
//去重
Set<Book> book2 = new LinkedHashSet<>(book1);
List<Book> list = new ArrayList<>(book2);
//价格升序 书名字典序
List<Book> sorted = list.stream()
.sorted(new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
int compare = Double.compare(o1.getPrice(), o2.getPrice());
if (compare != 0) {
return compare;
}
return o1.getName().compareTo(o2.getName());
}
})
.collect(Collectors.toList());
System.out.println("======图书列表========");
for (Book book : sorted) {
System.out.println(book);
}
}
}