get Last Item from a Collection - Java java.util

Java examples for java.util:Collection Operation

Description

get Last Item from a Collection

Demo Code


//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static void main(String[] argv) {
        Collection items = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(getLastItem(items));
    }//from ww w.ja  va  2s .com

    public static Object getLastItem(Collection items) {
        Object nextObject = new Object();
        Iterator i = items.iterator();
        while (i.hasNext()) {
            nextObject = i.next();
        }
        return nextObject;
    }
}

Related Tutorials