英文:
How can I divide the array?
问题
public ResponseEntity<?> findByHourlyAggregated(@PathVariable String hours) {
HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();
Date startDate = new Date();
Date date = new Date();
Timestamp endDate = null;
Date endDate = new Date();
List<Post> postAll = (List<Post>) findAll();
Instant afterHours = null;
List<Post> totalDates = new ArrayList<>();
List<Date> allDates = new ArrayList<Date>();
Date firstDate;
firstDate = postAll.get(0).getDateCreated();
for (Post p : postAll) {
startDate = p.getDateCreated(); //2020-08-11 10:42:08.0 , time of incoming data
date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Tue Aug 11 12:42:08 EET 2020
endDate = new Timestamp(date.getTime()); // 2020-08-11 12:42:08.0, add the parameter entered with the incoming data
if (firstDate.getTime() <= endDate.getTime() && startDate.getTime() <= endDate.getTime()) {
totalDates.add(p);
}
else {
map.put(firstDate, totalDates);
firstDate.setTime(endDate.getTime()); // value of new values when values in range are exhausted
date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Adds 2 hours
endDate = new Timestamp(date.getTime());
}
}
return new ResponseEntity<>(map, HttpStatus.OK);
}
以上是你提供的代码的翻译部分。
如果你需要将代码输出的内容翻译成中文,请提供代码输出的内容以便我进行翻译。
英文:
I have a block of code like the one below. As for the else condition, I want to add what I add to the totalDates
array as if it were a single array.
public ResponseEntity<?> findByHourlyAggregated(@PathVariable String hours) {
HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();
Date startDate = new Date();
Date date = new Date();
Timestamp endDate = null;
Date endDate = new Date();
List<Post> postAll = (List<Post>) findAll();
Instant afterHours = null;
List<Post> totalDates = new ArrayList<>();
List<Date> allDates = new ArrayList<Date>();
Date firstDate;
firstDate = postAll.get(0).getDateCreated();
for (Post p : postAll) {
startDate = p.getDateCreated(); //2020-08-11 10:42:08.0 , time of incoming data
date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Tue Aug 11 12:42:08 EET 2020
endDate = new Timestamp(date.getTime()); // 2020-08-11 12:42:08.0, add the parameter entered with the incoming data
if (firstDate.getTime() <= endDate.getTime() && startDate.getTime() <= endDate.getTime()) {
totalDates.add(p);
}
else {
map.put(firstDate, totalDates);
firstDate.setTime(endDate.getTime()); // value of new values when values in range are exhausted
date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Adds 2 hours
endDate = new Timestamp(date.getTime());
}
}
return new ResponseEntity<>(map, HttpStatus.OK);
}
I separate the date value from the Post data according to the value from the parameter. For example, when the value 2 is entered, I need to assign values between 10-12 to one array and values between 12-14 to the other array. As a result, I want to get a print out.
{
"2020-08-11T10:42:08.000+03:00": [
{
"id": 1,
"postBody": "Post 1",
"author": "michael",
"dateCreated": "2020-08-11T10:42:08.000+03:00"
},
{
"id": 2,
"postBody": "Post 2",
"author": "jam",
"dateCreated": "2020-08-11T10:45:21.000+03:00"
},
{
"id": 3,
"postBody": "Post 3",
"author": "jim",
"dateCreated": "2020-08-11T10:57:33.000+03:00"
},
{
"id": 4,
"postBody": "Post 4",
"author": "dwight",
"dateCreated": "2020-08-11T11:02:35.000+03:00"
},
{
"id": 5,
"postBody": "Post 5",
"author": "scott",
"dateCreated": "2020-08-11T11:52:51.000+03:00"
}
]
}
2020-08-12T12:42:08.000+03:00": [
{
...
},
{
...
}],
....
答案1
得分: -1
我认为你应该在 else 块中的 map.put 语句之后初始化 totalDates。然后帖子将会被分成不同的 totalDates 列表对象。
map.put(firstDate, totalDates);
totalDates = new ArrayList<>(); // 这将创建一个新的 ArrayList 实例
更新 1
考虑将你的 HashMap
HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();
更改为
HashMap<String, List<Post>> map = new HashMap<String, List<Post>>();
映射中的键应该是不可变的。使用 String 作为键会确保这一点。然后你可以将 put 逻辑更改为 -
map.put(firstDate.toString(), totalDates);
希望能有所帮助。
英文:
I think you should initialize totalDates after map.put statement in else block. Then posts will be divided into different totalDates list object.
map.put(firstDate, totalDates);
totalDates = new ArrayList<>(); // This will create a new ArrayList instance
Update 1
Consider changing your HashMap
HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();
to
HashMap<String, List<Post>> map = new HashMap<String, List<Post>>();
The keys in the map should be immutable. Using String as key would ensure that. You can then change the put logic to -
map.put(firstDate.toString(), totalDates);
Hope it helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论