class Person {
String name;
String id;
String dateOfBirth;
int salary;
}
List <Person> listOfPersons; // Consider that list is filled with some data
Run Code Online (Sandbox Code Playgroud)
例如dateOfBirth = "2018-12-10T13:49:51.141Z";
出生日期采用 ISO8601 格式,
如果可以帮助对列表进行排序,则可以将其转换为java.util.Date
with 。Date.from(Instant.parse(dateOfBirth));
那么,有什么方法可以使用 JAVA 8 中的流或不错的解决方案来从人员列表中获取最年轻的人员吗?
附加注释:Person 类不能更改。
我有一个想法,void 函数并不意味着“不返回任何内容”,而是返回一些未知类型的内容,例如void *您可以将它与任何类型的数据一起使用的指针。所以我写了以下代码来确定:
#include <stdlib.h>
#include<stdio.h>
void x_function(void *);
void x_function(void *d)
{
printf("the integer value of d is %d \n " , *(int *)d );
printf("the string value of d is %s \n " , (char *)d );
printf("the character value of d is %c \n " , *(char *)d );
printf("the double value of d is %lf \n " , *(double *)d );
return 10;
}
int main()
{
int x = (int)x_function(520);
printf("The Value is …Run Code Online (Sandbox Code Playgroud) 我有一个使用 PriorityQueue 的简单代码,我希望整数以降序存储。
PriorityQueue<Integer> jumps = new PriorityQueue<>(20,Collections.reverseOrder());
jumps.add(8);
jumps.add(5);
jumps.add(15);
jumps.add(2);
jumps.add(16);
System.out.println(jumps.toString());
Run Code Online (Sandbox Code Playgroud)
这打印
[16, 15, 8, 2, 5]
Run Code Online (Sandbox Code Playgroud)
虽然我会期待
[16, 15, 8, 5, 2]
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
我编写这段代码来找到最年轻的人:
import java.util.Comparator;
import java.util.List;
public class PersonImpl implements PersonInterface {
@Override
public Person youngest(List<Person> personList) {
Integer minAge = personList.stream()
.map(Person::getAge)
.min(Comparator.comparing(Integer::valueOf))
.orElse(null);
return personList.stream()
.filter(person -> person.getAge() == minAge)
.toList()
.stream()
.findFirst()
.orElse(null);
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我做到了并且工作正常。现在我想知道我是否可以以更好的方式做到这一点(也许不是只对一个执行 2 个“语句”?) PS:我只提供了代码,因为我认为不需要发布所有的这里的其他课程只是为了回顾这一课程。
有人可以解释一下如何才能拥有更好的代码(更少的行)吗?谢谢
这是我的Java代码的一部分,IDE是Eclipse.'lines'是一个字符串数组.
//build anchor inner text
int Count = Lines.length;
String Text = ""; //<----------Eclipse shows warning here
for (int Index=0; Index<Count; Index++) {
Text += Lines[Index];
if (Index<Count-1)
Text += "<br/>";
}
Run Code Online (Sandbox Code Playgroud)
'Text'变量在'for'循环之外声明,但是,它在内部使用.Eclispe向我显示了这个警告:" 变量'Text'的声明行中没有使用局部变量Text的值 ".
怎么会这样?它在'for'循环中使用.
C(/*..*/)和C++(//)之间的注释风格有什么区别吗?
MISRA C说
规则2.2(必填):源代码只能使用
/* … */样式注释.
MISRA C++说
规则2.7.1(必填):字符序列
/*不得与c样式注释一起使用.规则2.7.3说明了
//对c ++的建议评论
任何人都可以解释为什么MISRA会说/*在C和//C++中使用?
我正在用HTML创建一个图表.我使用的是API amCharts,但问题是它在图表中显示了文本"amchart".
如何删除该文本以使其看起来不错?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Charts</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="amcharts.js" type="text/javascript"></script>
<script type="text/javascript">
var chart;
var chartData = [{
country: "USA",
visits: 400,
color: "#FF0F00"
}, {
country: "China",
visits: 380,
color: "#FF6600"
}, {
country: "Japan",
visits: 360,
color: "#FF9E01"
}, {
country: "Germany",
visits: 340,
color: "#FCD202"
}, {
country: "UK",
visits: 320,
color: "#F8FF01"
}, {
country: "France",
visits: 300,
color: "#B0DE09"
}, {
country: "India",
visits: 240,
color: "#04D215"
}, …Run Code Online (Sandbox Code Playgroud)