This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Is there an accepted best practice in Java for deleting a list element while iterating over the list?
(5 answers)
Closed 9 years ago.
How would I write an iterator for this code? I want to remove multiple entries based on input.
public void cancelRegistration(String someName)
{
for (Name n: ArrayList)
{
if(n.Name.equals(someName))
{
ArrayList.remove(n);
}
}
}
You can use Iterator.remove().
Assuming you have a class called Name with a String field called Name and assuming the class cancelRegistration is in has a field called ArrayList of type List<Name>:
public void cancelRegistration(String someName) {
for (Iterator<Name> iterator = ArrayList.iterator(); iterator.hasNext();)
if (iterator.next().Name.equals(someName))
iterator.remove();
}
Related
This question already has answers here:
Ways to iterate over a list in Java
(13 answers)
Closed 2 years ago.
So I am using List as object and I want to print values.
public List<Entry> getEntry() {
return entry;
}
public void setEntry(List<Entry> entry) {
this.entry = entry;
}
I am currently printing them one by one like this:
System.out.println(feed.getEntry().get(0).getTitle());
System.out.println(feed.getEntry().get(1).getTitle());
System.out.println(feed.getEntry().get(0));
System.out.println(feed.getEntry().get(1));
How do I need to change that I dont need to print one by one them?
To print all the entires:
feed.getEntry().forEach(System.out::println);
To print all the titles:
feed.getEntry().stream().map(Entry::getTitle).forEach(System.out::println);
To print all entries in the same line separated by spaces:
System.out.println(feed.getEntry().stream().collect(joining(" ")));
List<Entry> entries = Collections.emptyList();
for (Entry entry : entries)
System.out.println(entry);
Pay attetion on not to call entries.get() every time. You have to use Iterator instead.
This question already has answers here:
How to store all ArrayList<ArrayList<String>> values into ArrayList<String>?
(2 answers)
Closed 4 years ago.
I have a source list of type ArrayList<ArrayList<String>>. I want to put all the strings from the source list into a destination list of type ArrayList<String>. How do I do this?
Example:
sourceList = { {"a","b"}, {"c","b"} }
destinationList = { "a","b","c","d" }
You can use for-each and addAll():
for (ArrayList<String> subList : listOfLists) {
listofStrings.addAll(subList);
}
See:
https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(java.util.Collection)
This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 8 years ago.
I am trying delete a String that occurs 5 times in my List. This is what I've tried:
for(String obj : list1)
{
if(new ArzList().countOccurrence(list1, obj ) == 5)
{
list2.add(obj);
list1.removeAll(Collections.singleton(obj));
}
else{
list3.add(obj);
list1.removeAll(Collections.singleton(obj));
}
}
But I get java.util.ConcurrentModificationException each time.
How do I solve it?
You shouldn't iterate and remove through the same list.
Create a copy of the original list. Iterate through the original list but remove items from the copy version. Return the duplicate(copy) version of the list.
You're not allowed to modify the data structure mid-way through an iterator.
You could just use a normal loop here:
for (int i = 0; i < list1.size(); i++) {
String obj = list1.get(i);
...
}
You can only safely delete an element during a loop from a collection using the Iterator.
Iterator<String> myIterator = list1.iterator();
while(myIterator.hasNext()) {
//add logic to search list and delete element here
}
This question already has answers here:
How does the Java 'for each' loop work?
(29 answers)
Closed 9 years ago.
I have a list of objects called employee() and I used emp as my object. I want to increment over employees and print the name of all the objects. I really lost on how to do this. Thanks.
for( emp ; ;employees())
{
System.out.println(emp.name);
}
Try using foreach()
for(Employee employee : employees())
{
System.out.println(employee.name);
}
This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 9 years ago.
I need to delete some objects from a list if they meet a condition.
But I am getting java.util.ConcurrentModificationException.
Here is my code:
collegeList.addAll(CollegeManager.findByCollegeID(stateCode, districtCode));
for(College clg:collegeList){
if(!clg.approve()){
collegeList.remove(clg);
}
}
You can't remove elements while iterating through them in that manner. Use an Iterator instead.
Iterator<College> iter = collegeList.iterator();
while(iter.hasNext()) {
College clg = iter.next();
if(!clg.approve()) {
iter.remove();
}
}
You need to use an Iterator to iterate through List and remove objects using Iterator#remove().