如何分割数组?

huangapple go评论104阅读模式
英文:

How can I divide the array?

问题

  1. public ResponseEntity<?> findByHourlyAggregated(@PathVariable String hours) {
  2. HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();
  3. Date startDate = new Date();
  4. Date date = new Date();
  5. Timestamp endDate = null;
  6. Date endDate = new Date();
  7. List<Post> postAll = (List<Post>) findAll();
  8. Instant afterHours = null;
  9. List<Post> totalDates = new ArrayList<>();
  10. List<Date> allDates = new ArrayList<Date>();
  11. Date firstDate;
  12. firstDate = postAll.get(0).getDateCreated();
  13. for (Post p : postAll) {
  14. startDate = p.getDateCreated(); //2020-08-11 10:42:08.0 , time of incoming data
  15. date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Tue Aug 11 12:42:08 EET 2020
  16. endDate = new Timestamp(date.getTime()); // 2020-08-11 12:42:08.0, add the parameter entered with the incoming data
  17. if (firstDate.getTime() <= endDate.getTime() && startDate.getTime() <= endDate.getTime()) {
  18. totalDates.add(p);
  19. }
  20. else {
  21. map.put(firstDate, totalDates);
  22. firstDate.setTime(endDate.getTime()); // value of new values when values in range are exhausted
  23. date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Adds 2 hours
  24. endDate = new Timestamp(date.getTime());
  25. }
  26. }
  27. return new ResponseEntity<>(map, HttpStatus.OK);
  28. }

以上是你提供的代码的翻译部分。

如果你需要将代码输出的内容翻译成中文,请提供代码输出的内容以便我进行翻译。

英文:

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.

  1. public ResponseEntity&lt;?&gt; findByHourlyAggregated(@PathVariable String hours) {
  2. HashMap&lt;Date, List&lt;Post&gt;&gt; map = new HashMap&lt;Date, List&lt;Post&gt;&gt;();
  3. Date startDate = new Date();
  4. Date date = new Date();
  5. Timestamp endDate = null;
  6. Date endDate = new Date();
  7. List&lt;Post&gt; postAll = (List&lt;Post&gt;) findAll();
  8. Instant afterHours = null;
  9. List&lt;Post&gt; totalDates = new ArrayList&lt;&gt;();
  10. List&lt;Date&gt; allDates = new ArrayList&lt;Date&gt;();
  11. Date firstDate;
  12. firstDate = postAll.get(0).getDateCreated();
  13. for (Post p : postAll) {
  14. startDate = p.getDateCreated(); //2020-08-11 10:42:08.0 , time of incoming data
  15. date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Tue Aug 11 12:42:08 EET 2020
  16. endDate = new Timestamp(date.getTime()); // 2020-08-11 12:42:08.0, add the parameter entered with the incoming data
  17. if (firstDate.getTime() &lt;= endDate.getTime() &amp;&amp; startDate.getTime() &lt;= endDate.getTime()) {
  18. totalDates.add(p);
  19. }
  20. else {
  21. map.put(firstDate, totalDates);
  22. firstDate.setTime(endDate.getTime()); // value of new values when values in range are exhausted
  23. date = new Date(firstDate.getTime() + TimeUnit.HOURS.toMillis(Integer.parseInt(hours))); // Adds 2 hours
  24. endDate = new Timestamp(date.getTime());
  25. }
  26. }
  27. return new ResponseEntity&lt;&gt;(map, HttpStatus.OK);
  28. }

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.

  1. {
  2. &quot;2020-08-11T10:42:08.000+03:00&quot;: [
  3. {
  4. &quot;id&quot;: 1,
  5. &quot;postBody&quot;: &quot;Post 1&quot;,
  6. &quot;author&quot;: &quot;michael&quot;,
  7. &quot;dateCreated&quot;: &quot;2020-08-11T10:42:08.000+03:00&quot;
  8. },
  9. {
  10. &quot;id&quot;: 2,
  11. &quot;postBody&quot;: &quot;Post 2&quot;,
  12. &quot;author&quot;: &quot;jam&quot;,
  13. &quot;dateCreated&quot;: &quot;2020-08-11T10:45:21.000+03:00&quot;
  14. },
  15. {
  16. &quot;id&quot;: 3,
  17. &quot;postBody&quot;: &quot;Post 3&quot;,
  18. &quot;author&quot;: &quot;jim&quot;,
  19. &quot;dateCreated&quot;: &quot;2020-08-11T10:57:33.000+03:00&quot;
  20. },
  21. {
  22. &quot;id&quot;: 4,
  23. &quot;postBody&quot;: &quot;Post 4&quot;,
  24. &quot;author&quot;: &quot;dwight&quot;,
  25. &quot;dateCreated&quot;: &quot;2020-08-11T11:02:35.000+03:00&quot;
  26. },
  27. {
  28. &quot;id&quot;: 5,
  29. &quot;postBody&quot;: &quot;Post 5&quot;,
  30. &quot;author&quot;: &quot;scott&quot;,
  31. &quot;dateCreated&quot;: &quot;2020-08-11T11:52:51.000+03:00&quot;
  32. }
  33. ]
  34. }
  35. 2020-08-12T12:42:08.000+03:00&quot;: [
  36. {
  37. ...
  38. },
  39. {
  40. ...
  41. }],
  42. ....

答案1

得分: -1

我认为你应该在 else 块中的 map.put 语句之后初始化 totalDates。然后帖子将会被分成不同的 totalDates 列表对象。

  1. map.put(firstDate, totalDates);
  2. totalDates = new ArrayList<>(); // 这将创建一个新的 ArrayList 实例

更新 1

考虑将你的 HashMap

  1. HashMap<Date, List<Post>> map = new HashMap<Date, List<Post>>();

更改为

  1. HashMap<String, List<Post>> map = new HashMap<String, List<Post>>();

映射中的键应该是不可变的。使用 String 作为键会确保这一点。然后你可以将 put 逻辑更改为 -

  1. 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.

  1. map.put(firstDate, totalDates);
  2. totalDates = new ArrayList&lt;&gt;(); // This will create a new ArrayList instance

Update 1

Consider changing your HashMap

  1. HashMap&lt;Date, List&lt;Post&gt;&gt; map = new HashMap&lt;Date, List&lt;Post&gt;&gt;();

to

  1. HashMap&lt;String, List&lt;Post&gt;&gt; map = new HashMap&lt;String, List&lt;Post&gt;&gt;();

The keys in the map should be immutable. Using String as key would ensure that. You can then change the put logic to -

  1. map.put(firstDate.toString(), totalDates);

Hope it helps.

huangapple
  • 本文由 发表于 2020年8月23日 17:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63545394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定