给定下面的类和数据结构,我想计算每个连续 3 个元素的计数总和,类似于以下结果:
public class SaleTxn {
private int id;
private String txnDate;
private int amount;
}
Run Code Online (Sandbox Code Playgroud)
数据如下
id txnDate amount
1 2018-10-10 100
2 2018-10-11 200
3 2018-10-12 100
4 2018-10-13 100
5 2018-10-14 200
6 2018-10-15 200
... ...
Run Code Online (Sandbox Code Playgroud)
并且窗口大小为 3,表示只是过去 3 个元素的总和,预期结果如下
2018-10-10 ~ 2018-10-12 Total: 100+200+100 = 400
2018-10-13 ~ 2018-10-14 Total: 100+200+200 = 500
...
Run Code Online (Sandbox Code Playgroud)
我在下面有一个列表代码:
List<SaleTxn> myList; //
myList.stream().filter(x -> ??????)
.mapToInt(SouthboundShareholding::getAmount)
.sum();
Run Code Online (Sandbox Code Playgroud)
我该如何实施?
考虑这个代码
String[] colours1 = new String[]{"red", "green", "blue"};
String[] colours2 = new String[]{"red", "yellow", "blue"};
String[] colours3 = new String[]{"red", "green", "blue"};
List<String[]> distinct = Stream.of(colours1, colours2, colours3)
.distinct() // Do something here to compare arrays better
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我希望distinct列表包含 olny 2 元素colours1和colours2(因为 1 和 3 是等效的)。然而,因为流distinct()方法执行等于比较,它仍然包含所有 3 种颜色数组。我想要一个自定义的不同功能,您可以在其中提供比较器。在这种情况下Objects#deepEquals就足够了。有没有简单的方法来实现这一目标?
class Employee
{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{ …Run Code Online (Sandbox Code Playgroud) 有两个表,EntryTable和ExitTable。两者都有相似的列:id, date, time. 当员工打卡时,调用此函数。如果他当天已经打了,他的记录就会被输入到退出表中。否则,将其输入到条目表中。
public int addEntryRecord(String id) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Attend","rt","rt");
Statement stmt = con.createStatement();
long millis=System.currentTimeMillis();
java.sql.Date date=new java.sql.Date(millis);
Date today = new Date();
Date currentTime = new java.sql.Time(today.getTime());
stmt.executeQuery("SELECT id from Attend.EntryTable WHERE EntryTable.id ='"+id+"'and
EntryTable.DayOf='"+date+"'");
ResultSet rs = stmt.getResultSet();
System.out.println(rs.next());
if(rs.next()== true){
String sql = "INSERT Attend.ExitTable VALUES('"+id+"','"+date+"','"+currentTime+" ')";
stmt.executeUpdate(sql);
System.out.println("Good job");
}
else{
String sql ="INSERT Attend.EntryTable VALUES('"+id+"','"+date+"','"+currentTime+"')";
stmt.executeUpdate(sql);
System.out.println("Entry noted");
}
}catch (Exception e) { …Run Code Online (Sandbox Code Playgroud)