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?










0















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










share




share|improve this answer





























    0














    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();
    }
    }





    share|improve this answer



























      0












      0








      0







      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();
      }
      }





      share|improve this answer















      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();
      }
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 7 at 20:01

























      answered Mar 7 at 19:47









      DeadpoolDeadpool

      7,7572831




      7,7572831























          0














          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;







          share|improve this answer























          • java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line

            – user11164956
            Mar 8 at 6:33















          0














          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;







          share|improve this answer























          • java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 On the 2nd Line

            – user11164956
            Mar 8 at 6:33













          0












          0








          0







          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;







          share|improve this answer













          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;








          share|improve this answer












          share|improve this answer



          share|improve this answer










          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

















          • 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











          0














          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.






          share|improve this answer



























            0














            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.






            share|improve this answer

























              0












              0








              0







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 7 at 20:30









              David ConradDavid Conrad

              10.1k13039




              10.1k13039



























                  draft saved

                  draft discarded
















































                  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.




                  draft saved


                  draft discarded














                  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





















































                  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







                  Popular posts from this blog

                  1928 у кіно

                  Захаров Федір Захарович

                  Ель Греко