如何分割数组?

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

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&lt;?&gt; findByHourlyAggregated(@PathVariable String hours) {

    HashMap&lt;Date, List&lt;Post&gt;&gt; map = new HashMap&lt;Date, List&lt;Post&gt;&gt;();
    Date startDate = new Date();
    Date date = new Date();
    Timestamp endDate = null;
    Date endDate = new Date();
    List&lt;Post&gt; postAll = (List&lt;Post&gt;) findAll();
    Instant afterHours = null;
    List&lt;Post&gt; totalDates = new ArrayList&lt;&gt;();
    List&lt;Date&gt; allDates = new ArrayList&lt;Date&gt;();
    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() &lt;= endDate.getTime() &amp;&amp; startDate.getTime() &lt;= 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&lt;&gt;(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.

{
&quot;2020-08-11T10:42:08.000+03:00&quot;: [
{
&quot;id&quot;: 1,
&quot;postBody&quot;: &quot;Post 1&quot;,
&quot;author&quot;: &quot;michael&quot;,
&quot;dateCreated&quot;: &quot;2020-08-11T10:42:08.000+03:00&quot;
},
{
&quot;id&quot;: 2,
&quot;postBody&quot;: &quot;Post 2&quot;,
&quot;author&quot;: &quot;jam&quot;,
&quot;dateCreated&quot;: &quot;2020-08-11T10:45:21.000+03:00&quot;
},
{
&quot;id&quot;: 3,
&quot;postBody&quot;: &quot;Post 3&quot;,
&quot;author&quot;: &quot;jim&quot;,
&quot;dateCreated&quot;: &quot;2020-08-11T10:57:33.000+03:00&quot;
},
{
&quot;id&quot;: 4,
&quot;postBody&quot;: &quot;Post 4&quot;,
&quot;author&quot;: &quot;dwight&quot;,
&quot;dateCreated&quot;: &quot;2020-08-11T11:02:35.000+03:00&quot;
},
{
&quot;id&quot;: 5,
&quot;postBody&quot;: &quot;Post 5&quot;,
&quot;author&quot;: &quot;scott&quot;,
&quot;dateCreated&quot;: &quot;2020-08-11T11:52:51.000+03:00&quot;
}
]
}
2020-08-12T12:42:08.000+03:00&quot;: [
{
...
},
{
...
}], 
....

答案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&lt;&gt;(); // This will create a new ArrayList instance

Update 1

Consider changing your HashMap

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

to

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 -

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:

确定