32

If I have an iterator and I want to check the last value, how can I check it? Like

private Set<String> saarcCountries = new TreeSet<String>();
Iterator iter = saarcCountries.iterator();

while(iter.hasNext()) {

    String name = (String)iter.next();
    int countryId = Integer.parseInt(name);

    newsOrAnnouncementInsert.add(newsId);
    newsOrAnnouncementInsert.add(countryId);
    newsOrAnnouncementInsert.add(1);            //addby

    if (!(last value in iterator))) {
        newsOrAnnouncementInsert.add("~!~");//Delimiter for Multiple Detail Records
    }//end if

} //end of while

How can I check for last value. what should replace last value in iterator?

Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
Basit
  • 8,426
  • 46
  • 116
  • 196

9 Answers9

35

If iter.hasNext() is false after iter.next() then it is the last item.

Happy coding.

34

You can check it just like you do in the while condition:

if ( ! iter.hasNext()) {
    // last iteration
    ...
}
DerMike
  • 15,594
  • 13
  • 50
  • 63
13

This answer is not specific to an Iterator, but can be used for any iterating statement where you need to insert a separator between items. It doesn't need to know the current index nor does it need any condition.

String previousSeparator = "";
for (String value : collection) {
  System.err.println(previousSeparator + value);
  previousSeparator = ",";
}
lilalinux
  • 2,903
  • 3
  • 43
  • 53
  • 1
    Nice and simple. I wonder why that common problem doesn't have a standard solution? It probably has in one of the many libraries, I just don't know it. – jackthehipster Feb 06 '18 at 14:14
3

This question is old, the answer is good, but I used this answer to figure out how to add commas between list items and add an end of list delimiter. In case someone else like me is looking for the same thing here's what I did:

public String toString() {

    StringBuilder sb = new StringBuilder();

    sb.append("List contains:\n");

    Iterator<String> iterator = list.iterator(); 

    while(iterator.hasNext()){

        // Get the next element.
        sb.append(iterator.next());

        // If there is another element add a comma.
        if(iterator.hasNext()){
            sb.append(", ");
        }else{
            sb.append(" ~!~");
        }
    }

    return sb.toString();
}
ross studtman
  • 936
  • 1
  • 8
  • 22
3

Although other answers gave some ideas, I think it is better to add it without the condition and remove the last delimiter after the while loop terminates.

MByD
  • 135,866
  • 28
  • 264
  • 277
3

Use StrBuilder (an apache commons lang class) as the type of newsOrAnnouncementInsert. Then use the StrBuilder.appendSeperator() method.

For example

StrBuilder newsOrAnnouncementInsert = new StrBuilder();

while (iterator.hasNext())
{
    newsOrAnnouncementInsert.appendSeperator("~!~");

    String name = (String)iter.next();
    int countryId = Integer.parseInt(name);

    newsOrAnnouncementInsert.add(newsId);
    newsOrAnnouncementInsert.add(countryId);
    newsOrAnnouncementInsert.add(1);
}

appendSeperator only adds the seperator if the buffer is not currently empty (in this case, if this is the first time through the loop).

Basically, you want to invert the comparison. Don't check to see if you are on the last element of the itnerator, instead check to see that you are not processing the first element. If not the first element, append the seperator then append the current element.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

How about this:

if (!(iter.hasNext())) {
    newsOrAnnouncementInsert.add("~!~");//Delimiter for Multiple Detail Records
}
nwinkler
  • 52,665
  • 21
  • 154
  • 168
0

You can use the hasNext method to check if this is the last value. If you don't have any next values, this means it is the last value.

Tiago Pasqualini
  • 821
  • 5
  • 12
0

If you don't have access to the Iterator you can use this approach.

boolean afterFirst = false;
for(String name : saarcCountries) {
    if(afterFirst)
       newsOrAnnouncementInsert.add("~!~");
    afterFirst = true;

    newsOrAnnouncementInsert.add(newsId);
    newsOrAnnouncementInsert.add(name);
    newsOrAnnouncementInsert.add(1);
} 

Say its a StringBuilder

StringBuilder newsOrAnnouncementInsert = new StringBuidler();
String sep = "";

for(String name : saarcCountries) {
    newsOrAnnouncementInsert.append(sep + newsId + name + 1);
    sep = "~!~";
} 
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130