构造一个 List<Student> , Student 有 name 和 age 两个属性。将 List<Student> 转换成 List<User>。 User里有 userName 和 userAge两个属性。
时间: 2024-05-11 17:18:06 浏览: 217
可以使用 Java 8 的 Stream API 和 Lambda 表达式来实现这个需求:
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("Alice", 20));
studentList.add(new Student("Bob", 21));
studentList.add(new Student("Charlie", 22));
List<User> userList = studentList.stream()
.map(student -> new User(student.getName(), student.getAge()))
.collect(Collectors.toList());
// 输出转换后的 User 列表
for (User user : userList) {
System.out.println(user.getUserName() + " " + user.getUserAge());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
class User {
private String userName;
private int userAge;
public User(String userName, int userAge) {
this.userName = userName;
this.userAge = userAge;
}
public String getUserName() {
return userName;
}
public int getUserAge() {
return userAge;
}
}
```
输出结果为:
```
Alice 20
Bob 21
Charlie 22
```
可以看到,List<Student> 成功转换为了 List<User>,并且转换后的 User 列表中包含了 Student 列表中的所有元素。
阅读全文
相关推荐






<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2025/2/28
Time: 15:47
To change this template use File | Settings | File Templates.
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生列表</title>
</head>
<body>
欢迎您,${name}
<form action="<%=request.getContextPath()%>/student/selectStudentAndGrade" method="post">
学生姓名:<input type="text" name="studentName" placeholder="请输入要搜索的学生姓名" value="${student.studentName}">
班级:<select name="gradeId">
<option value=>请输入班级</option>
<option value="1"<c:if test="${student.gradeId=='1'}">selected</c:if>>java1班</option>
<option value="2"<c:if test="${student.gradeId=='2'}">selected</c:if>>java2班</option>
<option value="3"<c:if test="${student.gradeId=='3'}">selected</c:if>>java3班</option>
<option value="4"<c:if test="${student.gradeId=='4'}">selected</c:if>>java4班</option>
</select>
<input type="submit" value="查询"> <input type="button" value="重置" onclick="resetQuery()">
</form>
</form>
<input type="button" value="新增" onclick="location.href='<%=request.getContextPath()%>/studentAdd.jsp'">
序号
学生姓名
年龄
出生日期
班级
<c:forEach items="${studentList}" var="tea" varStatus="sta">
${sta.count}
${tea.studentName}
${tea.age}
<%-- ${stu.birth} --%>
<fmt:formatDate value="${tea.birth}" pattern="yyyy-MM-dd"/>
${tea.grade.gradeName}
<%-- ${stu.grade.gradeName} --%>
修改
删除
</c:forEach>
</body>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.9.1.js"></script>
<script>
function resetQuery(){
$("input[type=text]").val("");
$('select').prop('selectedIndex',0);
}
</script>
</html>加css样式













