Java List with Objects - find and replace (delete) entry if Object with certain attribute already ex
Posted
by Sophomore
on Stack Overflow
See other posts from Stack Overflow
or by Sophomore
Published on 2010-03-18T14:00:47Z
Indexed on
2010/03/18
14:31 UTC
Read the original article
Hit count: 459
Hi there
I've been working all day and I somehow can't get this probably easy task figured out - probably a lack of coffee...
I have a synchronizedList
where some Objects are being stored. Those objects have a field
which is something like an ID. These objects carry information about a user and his current state (simplified).
The point is, that I only want one object for each user. So when the state of this user changes, I'd like to remove the "old" entry and store a new one in the List
.
protected static class Objects{
...
long time;
Object ID;
...
}
...
if (Objects.contains(ID)) {
Objects.remove(ID);
Objects.add(newObject);
} else {
Objects.add(newObject);
}
Obviously this is not the way to go but should illustrate what I'm looking for...
Maybe the data structure is not the best for this purpose but any help is welcome!
© Stack Overflow or respective owner