private Collection collection=new ArrayList<String>(); private Collection<Integer> collection2=new ArrayList<>(); public static void main(String[] args) { Collection2 collection2=new Collection2(); //collection2.men(); collection2.add(collection2.collection); } public void add(Collection<? extends Integer> collection) { }这段代码为什么正确
时间: 2023-04-06 14:01:21 浏览: 61
这段代码正确是因为使用了通配符 "? extends Integer",表示这个集合中的元素必须是 Integer 类型或其子类,所以可以将 collection2 集合添加到 add 方法中。同时,使用了泛型可以在编译期间进行类型检查,避免了运行时出现类型转换异常的情况。
相关问题
hutool5.3.10 List<Bean>转List<Map<String,Object>>
可以使用Hutool的BeanUtil工具类来实现List<Bean>转List<Map<String,Object>>,示例代码如下:
```java
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import java.util.List;
import java.util.Map;
public class BeanToListMapDemo {
public static void main(String[] args) {
// 假设有一个List<Bean>对象列表
List<Bean> beanList = CollUtil.newArrayList(
new Bean(1, "张三", 20),
new Bean(2, "李四", 30),
new Bean(3, "王五", 40)
);
// 将List<Bean>转为List<Map<String,Object>>
List<Map<String, Object>> mapList = BeanUtil.beanToList(beanList, Map.class);
// 输出转换后的结果
System.out.println(mapList);
}
static class Bean {
private Integer id;
private String name;
private Integer age;
public Bean(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Bean{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
```
输出结果:
```
[{id=1, name=张三, age=20}, {id=2, name=李四, age=30}, {id=3, name=王五, age=40}]
```
创建一个员工类,每个员工有工号(int)、姓名(String)、部门(String)、工资(int),使用Collection接口的实现下面操作: 1)、添加几条信息 2)、列出所有的员工姓名 3)、列出所有员工姓名及其工资 4)、输出Jack的工资,并将其工资改为1500元(通过changePay(int)方法修改) 5)、将所有工资低于1000元的员工的工资上涨20%(通过changePay(float)方法修改) 6)、计算每个部门的平均工资 7)、删除名叫“Tom”的员工信息 具体员工情况见下表: 工号 姓名 部门 工资 1001 Tom Market 1200 1002 Jack Department 1300 1003 Simth Market 2000 1004 Tony Department 3000
以下是一个可能的实现:
```
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class Employee {
private int id;
private String name;
private String department;
private int salary;
public Employee(int id, String name, String department, int salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDepartment() {
return department;
}
public int getSalary() {
return salary;
}
public void changePay(int newSalary) {
this.salary = newSalary;
}
public void changePay(float percentage) {
this.salary = (int) (this.salary * (1 + percentage));
}
public static void main(String[] args) {
Employee e1 = new Employee(1001, "Tom", "Market", 1200);
Employee e2 = new Employee(1002, "Jack", "Department", 1300);
Employee e3 = new Employee(1003, "Simth", "Market", 2000);
Employee e4 = new Employee(1004, "Tony", "Department", 3000);
Collection<Employee> employees = new ArrayList<>();
employees.add(e1);
employees.add(e2);
employees.add(e3);
employees.add(e4);
// 列出所有的员工姓名
System.out.println("所有员工姓名:");
for (Employee e : employees) {
System.out.println(e.getName());
}
// 列出所有员工姓名及其工资
System.out.println("所有员工姓名及其工资:");
for (Employee e : employees) {
System.out.println(e.getName() + ": " + e.getSalary());
}
// 输出Jack的工资,并将其工资改为1500元
for (Employee e : employees) {
if (e.getName().equals("Jack")) {
System.out.println("Jack的工资为:" + e.getSalary());
e.changePay(1500);
break;
}
}
// 将所有工资低于1000元的员工的工资上涨20%
for (Employee e : employees) {
if (e.getSalary() < 1000) {
e.changePay(0.2f);
}
}
// 计算每个部门的平均工资
Map<String, Double> avgSalaryByDept = new HashMap<>();
Map<String, Integer> countByDept = new HashMap<>();
for (Employee e : employees) {
String dept = e.getDepartment();
double salary = e.getSalary();
double sum = avgSalaryByDept.getOrDefault(dept, 0.0) * countByDept.getOrDefault(dept, 0);
countByDept.put(dept, countByDept.getOrDefault(dept, 0) + 1);
avgSalaryByDept.put(dept, (sum + salary) / countByDept.get(dept));
}
System.out.println("每个部门的平均工资:");
for (String dept : avgSalaryByDept.keySet()) {
System.out.println(dept + ": " + avgSalaryByDept.get(dept));
}
// 删除名叫“Tom”的员工信息
employees.removeIf(e -> e.getName().equals("Tom"));
}
}
```