ArrayList removes objects based on the equals(Object obj) method. So you should implement properly this method. Something like:
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof ArrayTest)) return false;
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
Or
public boolean equals(Object obj) {
if (obj instanceof ArrayTest) {
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
return false;
}
Answer from Valchev on Stack OverflowHow to remove specific object from ArrayList in Java? - Stack Overflow
java - Remove object from ArrayList with some Object property - Stack Overflow
How to remove object from ArrayList depending on attribute?
java - Removing items from a list - Stack Overflow
Videos
ArrayList removes objects based on the equals(Object obj) method. So you should implement properly this method. Something like:
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof ArrayTest)) return false;
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
Or
public boolean equals(Object obj) {
if (obj instanceof ArrayTest) {
ArrayTest o = (ArrayTest) obj;
return o.i == this.i;
}
return false;
}
If you are using Java 8 or above:
test.removeIf(t -> t.i == 1);
Java 8 has a removeIf method in the collection interface. For the ArrayList, it has an advanced implementation (order of n).
Using Java-8 Collection#removeIf
myList.removeIf(obj -> obj.id == 10);
With Java-7 you'll have to use iterator:
for(Iterator<MyType> iterator = myList.iterator(); iterator.hasNext(); ) {
if(iterator.next().id == 10)
iterator.remove();
}
Note that list iteration is necessary in any case. In Java-8 removeIf method it's just performed internally.
Maybe I don't understand the question but why nobody suggested to use override equals and hashcode for that user class?
class MyObject {
final String id;
final String name;
MyObject(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return Objects.equals(id, ((MyObject) o).id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
@Override
public String toString() {
return "MyObject{id='" + id + "', name='" + name + "'}";
}
}
in this case you can easy remove any object from list
final ArrayList<MyObject> list = new ArrayList<>();
list.add(new MyObject("id1", "name1"));
list.add(new MyObject("id2", "name2"));
list.add(new MyObject("id3", "name3"));
MyObject removeCandidate = new MyObject("id2", "name2");
list.remove(removeCandidate);
System.out.println(list);
code above prints
[MyObject{id='id1', name='name1'}, MyObject{id='id3', name='name3'}]
I am creating a phone book program and need a method to remove an entry from the directory depending on what name or phone number the user enters.
So far I have the following code:
public void deleteEntry(String string) {
for(Entry e : phonebook){
if (e.getFirstName().equalsIgnoreCase(string)){
phonebook.remove(e);
System.out.println("Entry " + e.toString() + " removed");
}
else {
System.out.println("Entry not deleted");
}
}
}Which I thought should work, however it gives the following errors. The program outputs the deletion message as well which is weird so I am not sure what is causing this error.
The code the method is being called in is shown here:
case 2:
printDirectory();
System.out.println("Enter name or number: ");
String nameOrNum = input.nextLine();
deleteEntry(nameOrNum);
break;I have an Entry class which contains information for a single Entry object. Is there a way I could override the equals() method to compare first names and implement the delete method this way? Or can my solution be resolved easily?
Thanks in advance!
for (Iterator<String> iter = list.listIterator(); iter.hasNext(); ) {
String a = iter.next();
if (...) {
iter.remove();
}
}
Making an additional assumption that the list is of strings.
As already answered, an list.iterator() is needed. The listIterator can do a bit of navigation too.
โ---------
Update
As @AyushiJain commented, there is
list.removeIf(...);
You need to use Iterator and call remove() on iterator instead of using for loop.