Removing full object from a value The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?PHP: Delete an element from an arrayHow to get an enum value from a string value in Java?Checking if a key exists in a JavaScript object?How do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?
How should I connect my cat5 cable to connectors having an orange-green line?
Physiological effects of huge anime eyes
Arrows in tikz Markov chain diagram overlap
How exploitable/balanced is this homebrew spell: Spell Permanency?
Can Sri Krishna be called 'a person'?
How can I replace x-axis labels with pre-determined symbols?
What difference does it make matching a word with/without a trailing whitespace?
Arity of Primitive Recursive Functions
Creating a script with console commands
Man transported from Alternate World into ours by a Neutrino Detector
Is there a rule of thumb for determining the amount one should accept for a settlement offer?
Direct Implications Between USA and UK in Event of No-Deal Brexit
Find a path from s to t using as few red nodes as possible
Find the majority element, which appears more than half the time
Is a linearly independent set whose span is dense a Schauder basis?
Is it possible to make a 9x9 table fit within the default margins?
How do I secure a TV wall mount?
Prodigo = pro + ago?
Strange use of "whether ... than ..." in official text
Why doesn't Shulchan Aruch include the laws of destroying fruit trees?
pgfplots: How to draw a tangent graph below two others?
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
Free fall ellipse or parabola?
How can I prove that a state of equilibrium is unstable?
Removing full object from a value
The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?PHP: Delete an element from an arrayHow to get an enum value from a string value in Java?Checking if a key exists in a JavaScript object?How do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?
Helper Class
public class HomeScreenChatsHelper implements Comparable
private String ID;
private String Name;
private String Image;
private String From;
private String Seen;
private String LastMessage;
private String LastMessageTime;
public HomeScreenChatsHelper()
public HomeScreenChatsHelper(String id, String name, String image, String from, String seen, String lastmessage, String lastMessageTime)
this.ID=id;
this.Name = name;
this.Image = image;
this.From = from;
this.Seen = seen;
this.LastMessage = lastmessage;
this.LastMessageTime = lastMessageTime;
public String getID()
return ID;
public void setID(String id)
ID = id;
public String getName()
return Name;
public void setName(String name)
Name = name;
public String getImage()
return Image;
public void setImage(String image)
Image = image;
public String getMessage()
return LastMessage;
public void setMessage(String message)
LastMessage = message;
public String getTime()
return LastMessageTime;
public void setTime(String time)
LastMessageTime = time;
public String getFrom()
return From;
public void setFrom(String from)
From = from;
public String getSeen()
return Seen;
public void setSeen(String seen)
Seen = seen;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public int compareTo(Object comparestu)
long compareage= Long.parseLong(((HomeScreenChatsHelper)comparestu).getTime());
long a = Long.parseLong(LastMessageTime);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
return Long.compare(a,compareage);
@Override
public boolean equals(Object o)
if (this == o) return true;
if (!(o instanceof HomeScreenChatsHelper)) return false;
HomeScreenChatsHelper that = (HomeScreenChatsHelper) o;
return getID().equals(that.getID());
@Override
public int hashCode()
return getID().hashCode();
Activity
for(HomeScreenChatsHelper str : mChats)
if (str.getID().equals(ID))
mChats.remove(ID);
break;
There are a ton of tutorials on how to do it and I've spent the past week looking for a solution and I still don't have it. Is there anyway I can remove an whole object by just specifying just the ID? I wont have the values of all the other fields so I just want to remove a particular object by its ID. Also I cant use the clear option because I need the other data. So can someone help me out please?
With the present code nothing happens. No errors but doesn't work
java android arrays for-loop arraylist
add a comment |
By using java-8 you can filter the list, result will be the List<HomeScreenChatsHelper>
that does have HomeScreenChatsHelper
with same id
List<HomeScreenChatsHelper> mChats = new ArrayList<>();
//filter
List<HomeScreenChatsHelper> result = mChats.stream()
.filter(str->!str.getId().equals(Id)).
.collect(Collectors.toList());
Or by using Iterator
// Iterator.remove()
Iterator itr = mChats.iterator();
while (itr.hasNext())
HomeScreenChatsHelper x = itr.next();
if (x.getId().equals(Id))
itr.remove();
}
}
add a comment |
By using java-8 you can filter the list, result will be the List<HomeScreenChatsHelper>
that does have HomeScreenChatsHelper
with same id
List<HomeScreenChatsHelper> mChats = new ArrayList<>();
//filter
List<HomeScreenChatsHelper> result = mChats.stream()
.filter(str->!str.getId().equals(Id)).
.collect(Collectors.toList());
Or by using Iterator
// Iterator.remove()
Iterator itr = mChats.iterator();
while (itr.hasNext())
HomeScreenChatsHelper x = itr.next();
if (x.getId().equals(Id))
itr.remove();
}
}
By using java-8 you can filter the list, result will be the List<HomeScreenChatsHelper>
that does have HomeScreenChatsHelper
with same id
List<HomeScreenChatsHelper> mChats = new ArrayList<>();
//filter
List<HomeScreenChatsHelper> result = mChats.stream()
.filter(str->!str.getId().equals(Id)).
.collect(Collectors.toList());
Or by using Iterator
// Iterator.remove()
Iterator itr = mChats.iterator();
while (itr.hasNext())
HomeScreenChatsHelper x = itr.next();
if (x.getId().equals(Id))
itr.remove();
}
}
edited Mar 7 at 20:01
answered Mar 7 at 19:47
DeadpoolDeadpool
7,7572831
7,7572831
add a comment |
add a comment |
Your question is quite unclear. is mChats
a List containing HomeScreenChatsHelper
objects?
I assume so. If this is the case, then you can change your foreach
loop into the normal loop
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++)
if (mChats.get(i).getID().equals(ID))
mChats.remove(i);
break;
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
add a comment |
Your question is quite unclear. is mChats
a List containing HomeScreenChatsHelper
objects?
I assume so. If this is the case, then you can change your foreach
loop into the normal loop
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++)
if (mChats.get(i).getID().equals(ID))
mChats.remove(i);
break;
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
add a comment |
Your question is quite unclear. is mChats
a List containing HomeScreenChatsHelper
objects?
I assume so. If this is the case, then you can change your foreach
loop into the normal loop
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++)
if (mChats.get(i).getID().equals(ID))
mChats.remove(i);
break;
Your question is quite unclear. is mChats
a List containing HomeScreenChatsHelper
objects?
I assume so. If this is the case, then you can change your foreach
loop into the normal loop
//Assuming mChats is List e.g ArrayList
for (int i = 0; mChats.size(); i++)
if (mChats.get(i).getID().equals(ID))
mChats.remove(i);
break;
answered Mar 7 at 19:52
Ezekiel SebastineEzekiel Sebastine
14617
14617
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
add a comment |
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line
– user11164956
Mar 8 at 6:33
add a comment |
The easiest way in Java 8 or later is with Collection#removeIf
:
mChats.removeIf(str -> str.getID().equals(ID));
By the way, the convention in Java is for fields to begin with a lowercase letter.
add a comment |
The easiest way in Java 8 or later is with Collection#removeIf
:
mChats.removeIf(str -> str.getID().equals(ID));
By the way, the convention in Java is for fields to begin with a lowercase letter.
add a comment |
The easiest way in Java 8 or later is with Collection#removeIf
:
mChats.removeIf(str -> str.getID().equals(ID));
By the way, the convention in Java is for fields to begin with a lowercase letter.
The easiest way in Java 8 or later is with Collection#removeIf
:
mChats.removeIf(str -> str.getID().equals(ID));
By the way, the convention in Java is for fields to begin with a lowercase letter.
answered Mar 7 at 20:30
David ConradDavid Conrad
10.1k13039
10.1k13039
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051652%2fremoving-full-object-from-a-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown