Add the last entry object into an arraylist using for-each loop java
<p>I am trying to get the last element of an Entry object and add into an arraylist of Entry objects(so it will only store one Entry object in the arraylist).The reason I am doing this is that I need that ArrayList to be used in another class.</p>

<p>Source code:</p>

<pre><code>ArrayList&lt;Entry&lt;String,Integer&gt;&gt; allEntries = new ArrayList&lt;Entry&lt;String,Integer&gt;&gt;();
Iterator &lt;Entry &lt;String,Integer&gt;&gt; entries = post.getCategory().entrySet().iterator();
 while(entries.hasNext()) {
  Entry &lt;String,Integer&gt; entry = entries.next();
if(topic.getField("categoryName").equals(entry.getKey())){                                                            
  int increaseCount = entry.getValue();
  increaseCount++;
  entry.setValue(increaseCount);
}
for(Entry&lt;String,Integer&gt; entrySet : post.getCategory().entrySet()){
  if(!entries.hasNext()){
     allEntries.add(entrySet);                      
  }
}
</code></pre>
