I have try methods like remove, removeAll, delete. But all of these words is not available in android studio. What word should i used instead?
public List<ContactObject> receipt = new ArrayList<>();
receipt.add(new ContactObject(object.product_title, object.product_price,
object.img1, object.quantity));
I know how to add to the list receipt, but how to remove the data added?
The remove() of an ArrayList depends on the implementation of equals() method, in your case in ContactObject.
Invoking remove() then will solve your issue.
receipts.remove(contactObject);
If you don't add the new object directly, you can remove it like this:
List<ContactObject> receipts = new ArrayList<>();
ContactObject contactObject = new ContactObject(object.product_title, object.product_price,
object.img1, object.quantity)
receipts.add(contactObject);
// Remove previous added object
receipts.remove(contactObject);
removeAll removes all the given objects and returns the state of the operation.while remove only remove the object that is currently provided.
In your case you want to remove a single object by
yourarraylist.remove(yourobject);
Related
I have MyFinalSalad class consisting of the following elements:
AppleClass apple;
BananaClass banana;
PearClass pear;
List<SpicesClass> spices;
I have equals implemented such as 2 MyFinalSalad objects are equal, if they have same AppleClass, BananaClass, PearClass objects in them.
Now, I am creating a set of MyFinalSalad objects.
And I have the following code:
MyFinalSalad mySalad = new MyFinalSalad(apple, banana, pear);
SpiceClass cinnamon = new SpiceClass("cinnamon");
if (mySet.contains(mySalad)) {
// I want to fetch mySalad object in the set and add cinnamon to the list of spices
} else {
List<SpiceClass> spices = new ArrayList<>();
spices.add(cinnamon);
mySalad.setSpices(spices);
mySet.add(mySalad);
}
To summarize, if mySalad is already present in mySet, add the spice object to the list of spices in mySalad from mySet, else add mySalad to mySet after creating a new spice list, adding cinnamon to it and inserting list in mySalad.
My question is, if set already has mySalad and I want to add a new spice to the list in that object, how do I achieve it?
From https://stackoverflow.com/a/7283419/887235 I have the following:
mySet.stream().filter(mySalad::equals).findAny().orElse(null).getSpices().add(cinnamon);
Is this the only way or the right way to do it? Or is there a better way?
I was thinking that as I am already entering if after doing a contains check, orElse(null) will never be encountered. Thus null.getSpices() will never occur. Is this assumption correct?
Is there a better way to do it?
I cannot change Set to Map.
Your assumption is correct. The orElse(null) will never take place since you check if the set contains the salad right before. You could replace it with get().
However, I would also go one level before and handle it as an Optional, taking the advantage of isPresent and get method.
Salad mySalad = new Salad();
Optional<Salad> possibleSalad = set.stream().filter(mySalad::equals).findAny();
if (possibleSalad.isPresent()) {
Salad alreadyExistingSalad = possibleSalad.get();
// combine spices
} else {
// add new salad
}
Now how to store that values of set in a list. Here the set and list is of different type(ProductAttribute and PersistableProductAttribute).
for(ProductRelationship productrelationship:productRelationshipList)
{
Product product=productrelationship.getRelatedProduct();
PersistableProduct ppTem=new PersistableProduct();
Set<ProductAttribute> aSet = product.getAttributes();
List<PersistableProductAttribute> aList = new ArrayList<PersistableProductAttribute>();
// I need to store here in a list.
}
Its just a simple one I have got it after trying several times by myself.First I need to create a set for PersistableProductAttribute and I have to get the attributes of it using loop and I need to copy that set to list. Now it works fine because the set and list are same type. So we can copy a set to list using add() method in java.
Set<PersistableProductAttribute> ppaSet = new
PersistableProductAttribute<>();
for(ProductAttribute x:aSet)
{
// Get all the attributes here using get()
//and set() methods
// Copy the set to list as follows,
aList.add(ppaSet);
}
So I have this code here:
List<XMLMessage> xmlMessageList = new ArrayList<XMLMessage>();
now XMLMessage has set and get methods in here such as setFileContent, setFileName, and setFileDirectory and these are all Strings.
So how do I access those setmethods?
I am guessing it's something like this?
List xmlMessageList = new ArrayList();
xmlMessageList.add(setFileContent);
You can access a specific XMLMessage from the list by using the get get(int) method. You can then call methods on this instance. E.g.:
List<XMLMessage> xmlMessageList = new ArrayList<>();
xmlMessageList.add(new XMLMessage());
XMLMessageList.get(0).setFileName("my_file.txt");
i have a custom arrayadapter with some pre_populated data and more data is to be added to it from the user input.The problem is that duplicate values are still added to the arrayadapter..here is my code:
String s;//holds data from user input
for(int i=0 ; i<my_adapter.getCount() ; i++){
MyCollection itemObject=my_adapter.getItem(i);
//MyCollection is an object from the collection class
String c=itemObject.toString();
if(c.matches(s)){
//do not add s to array adapter
}else{
//add s to arrayadapter
my_arrayvalues.add(new MyCollection(s));
my_adapter.notifyDataSetChanged();
}
Running the above code,no change is effected on the adapter even when the the values do not match..if i just run the project without including the above
duplicate values are added.how can i correct this?
after following the answers i have done as suggested but still the duplicates are being added:
the updated code
hs = new HashSet();
my_arrayvalues.add(new MyCollections(s));
hs.addAll(my_arrayvalues);
my_arrayvalues.clear();
my_arrayvalues.addAll(hs);
my_adapter.notifyDataSetChanged();
you can use a Collection that does not allow duplicates. Since you have implemented your custom adapter you can avoid to pass to the super constructor of ArrayAdapter the dataset, and override the methods you need.
For instance you could use a
LinkedHashSet
and the getCount method should returns the this of the LinkedHashSet. Also, your class could extend BaseAdapter instead of ArrayAdapter
Use String.equals(s) instead of String.matches(s).
Equals will compare the string objects for equality.
String s;//holds data from user input
for(int i=0 ; i<my_adapter.getCount() ; i++){
MyCollection itemObject=my_adapter.getItem(i);
//MyCollection is an object from the collection class
String c=itemObject.toString();
if(c.equals(s)){
//do not add s to array adapter
}else{
//add s to arrayadapter
my_arrayvalues.add(new MyCollection(s));
my_adapter.notifyDataSetChanged();
}
!!!EDIT!!!
While the above should fix your problem, I agree with others solution that you should use a HashSet instead of checking all elements manually. Using HashSet or equivalent will improve performance. Check this answer for a better explanation.
ArrayList<Persons> persList = new ArrayList<Persons>();
for(Persons p : persList){
Persons pers = new Persons();
pers = service.getPersons(id);
p.setAddress(pers.getAddress());
persList.add(pers);
}
Is this the right way to add all found Persons to persList? Thank you in advance.
No, you shouldn't modify a list while you're iterating over it, other than via the Iterator.remove method. Aside from anything else, even if this code didn't throw an exception, it would go on forever unless persList was empty... there's always be new people to iterate over!
You should basically create a new list collecting the items to add, and then use addAll at the end:
ArrayList<Persons> persList = new ArrayList<Persons>();
// Populate the list, presumably
List<Persons> extraPeople = new ArrayList<Persons>();
for(Persons p : persList){
// Note: there's no point in creating a new object only to ignore it...
Persons pers = service.getPersons(id);
p.setAddress(pers.getAddress());
extraPeople.add(pers);
}
persList.addAll(extraPeople);
This code still doesn't make much sense in my view, as you're fetching via the same id value on every iteration... I can only hope this was an example rather than real code.
Also note that if each instance of your Persons class is meant to be a single person, it would be better to call it Person.