New To Java - Get the last Object inside a Collection..
Hello there ! to everyone
May I ask a quick question?
I have a java.util.Collection Object to which I am adding an Object of a java Class I created like this:
Collection col =new ArrayList();
col.add(new MyClass("value1","value2"));
I need to know the Source code to Obtain the Object that was last inserted into the Collection.
Could anybody help me?
Thanks in advance. and God Bless.
[642 byte] By [
BCE_Josea] at [2007-11-26 23:15:10]

import java.util.*;public class ListExample {
public static void main(String[] args) {
List < String > x = new ArrayList < String >();
x.add("foo");
x.add("bar");
x.add("baz");
String last = x.get(x.size()-1);
System.out.println(last);
}
}
This gives the last element in a non-empty list. This may not be the "last element inserted", since List supports x.add(index, element)
Hi there, That's great sir.I forgot something important I have to ask.How can I delete the last inserted Object inside a List?Could anybody help me.?
Could anybody help me.?Java List API: http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html"Give a man a fire and he will be warm for a night. Set a man on fire and he will be warm for the rest of his life." -some wise man
Hello again.
I am Sorry. But I can't just switch to a List Object, in my project I can only use the java.util.Collection.
and this Object does not have a remove by index method.
Could anybody suggest me How can I remove the last inserted object inside a java.util.Collection?
Thank you very much.
A Collection object is not guaranteed to have an order
From the javaDoc for Collection
Some are ordered and others unordered
Therefore there is no way to access the last item in a Collection without casting it to a type that does have the concept of ordering - such as a List.
Message was edited by:
passgodev
Your requirements are odd. Why can you only use the collection type? Why tieone hand behind your back? Why do you need to remove the last element added?
Could anybody suggest me How can I remove the last
inserted object inside a
java.util.Collection?
Here's a haphazard way that might take out the last inserted object. However, collections may or may not be ordered. This could cause havoc if the collection isn't ordered like you think it is:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html
import java.util.ArrayList;import java.util.Collection;
import java.util.Iterator;
public class ItTest {
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
c.add("One.");
c.add("Two.");
c.add("Three.");
c.add("Oops.");
Iterator it = c.iterator();
while (it.hasNext()) it.next(); // Bump the iterator to the last position.
it.remove(); // Removes the element pointed to by the iterator.
it = c.iterator();
while (it.hasNext()) System.out.println((String)it.next());
}
}
there's nothing to stop you having 2 references to the object, both a Collection and a List. use whichever one is needed at the time
Well I suppose I could change to a List.
It is just that everyone here at my corp. use a standard to hold multiple custom Class Objects inside a Collection type. I am not sure what the impact could be If I start to hold custom class objects inside a List.
What do you think Dr ?
and....
Thank you kevjava, I think I will try your solution and see how it works.!
God Bless you !
here's nothing to stop you having 2 references to the object, both a Collection and a List. use whichever one is needed at the time
Is that kissing to be clever?
private List < Stuff > list = new ArrayList < Stuff > ();private Collection < Stuff> col = list;
Yes sir, col is not of type List, just of type Collection!
;-))
Well I suppose I could change to a List.
It is just that everyone here at my corp. use a
standard to hold multiple custom Class Objects inside
a Collection type. I am not sure what the impact
could be If I start to hold custom class objects
inside a List.
the only impact could be if they change from using ArrayList to some other Collection like HashSet, in which case you're in trouble. but then, there's no notion of "the last one added" to a Set, so that's doubtful anyway. but bear in mind that you're not actually holding anything inside the Collection or List, you're still holding them in an ArrayList. the interface used is just the type you use to access that object, not the object itself
It is just that everyone here at my corp. use a standard to hold multiple custom
Class Objects inside a Collection type. I am not sure what the impact could be
If I start to hold custom class objects inside a List.
There's a difference between a coding standard and a dogma.
For example A is usually a cleaner design than B:
List < X > list = new ArrayList < X > (); //AArrayList < X > list = new ArrayList < X > (); //B
... because it's usually a good idea to code against the type List without
making assumptions about the implementing class. For example, A can
easily be changed to:
List < X > list = new LinkedList < X > (); //AANow what about the choice of List versus Collection for the field's type?
The Coding Standard is to use the most general type that captures
the needed functionality! I mean, why not do this?
Object list = new LinkedList < X > (); //?Therefore, use the most general type that expresses the needed functionality.
If you need to maintain an ordering, use List. If there is no need to maintain an
ordering, use Collection. Since you need to remember what was last
added, that sounds like at least the beginning to maintaining an ordering,
so I would go with list.
You could also do the following, but it is problematic, I wouldn't recommend it
in general.
private Object last;private Collection col = ...;
public void add(Object element) {
col.add(last = element);
}
public void removeLastAdded() {
if (last != null) {
col.remove(last);
last = null;
} else
throw new IllegalStateException("I don't know the last element :(");
}
Thank you Thank you
All these points of view, are so useful. even if don't apply them, it certainly shows how good you guys are at Java.!
DrLaszloJamf , thank you very much. Now I will see how that concepts apply. Hopefully I will find the best solution for my problem, now that I have so many examples.
Thank you guys.