deliveryInfo.setCreateCrew(crews.values().stream().findFirst().isPresent() ? crews.values().stream().findFirst().get() : null); deliveryInfo.setCreateShift(shifts.values().stream().findFirst().isPresent() ? shifts.values().stream().findFirst().get():null);简化代码
时间: 2024-02-22 08:00:31 浏览: 12
这段代码可以简化为:
```
deliveryInfo.setCreateCrew(crews.isEmpty() ? null : crews.values().iterator().next());
deliveryInfo.setCreateShift(shifts.isEmpty() ? null : shifts.values().iterator().next());
```
这样就不需要使用流式处理和三元表达式了,使用Map的iterator()方法获取第一个元素即可。同时,使用isEmpty()方法判断Map是否为空。
相关问题
deliveryInfo.setCreateCrew(crews.values().stream().findFirst().isPresent() ? crews.values().stream().findFirst().get() : null); deliveryInfo.setCreateShift(shifts.values().stream().findFirst().isPresent() ? shifts.values().stream().findFirst().get():null); 简化
这段代码可以简化为:
```
deliveryInfo.setCreateCrew(crews.values().stream().findFirst().orElse(null));
deliveryInfo.setCreateShift(shifts.values().stream().findFirst().orElse(null));
```
这样就不需要使用三元表达式了,orElse(null)方法可以将Optional对象中的值取出来,如果Optional对象为空则返回null。
C语言编写船舶管理系统
船舶管理系统可以分为多个模块,包括船只信息管理、船员信息管理、航线信息管理、运输任务管理等。以下是一个简单的C语言船舶管理系统的代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ship {
char name[50];
int capacity;
int speed;
char route[50];
};
struct crew {
char name[50];
int age;
char position[50];
};
int main() {
struct ship ships[50];
struct crew crews[50];
int ship_count = 0;
int crew_count = 0;
int choice;
do {
printf("1. Add ship\n");
printf("2. Add crew\n");
printf("3. View ships\n");
printf("4. View crew\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter ship name: ");
scanf("%s", ships[ship_count].name);
printf("Enter ship capacity: ");
scanf("%d", &ships[ship_count].capacity);
printf("Enter ship speed: ");
scanf("%d", &ships[ship_count].speed);
printf("Enter ship route: ");
scanf("%s", ships[ship_count].route);
ship_count++;
break;
case 2:
printf("Enter crew name: ");
scanf("%s", crews[crew_count].name);
printf("Enter crew age: ");
scanf("%d", &crews[crew_count].age);
printf("Enter crew position: ");
scanf("%s", crews[crew_count].position);
crew_count++;
break;
case 3:
printf("Ship List:\n");
for (int i = 0; i < ship_count; i++) {
printf("%s\t%d\t%d\t%s\n", ships[i].name, ships[i].capacity, ships[i].speed, ships[i].route);
}
break;
case 4:
printf("Crew List:\n");
for (int i = 0; i < crew_count; i++) {
printf("%s\t%d\t%s\n", crews[i].name, crews[i].age, crews[i].position);
}
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 5);
return 0;
}
```
此代码示例演示了如何使用结构体(`struct`)来存储船只和船员信息,并使用交互式菜单来添加、查看和退出系统。你可以根据实际需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)