Reputation: 153
So I have written a program tat outputs as I want it to correctly but for some reason I am still getting some errors at the end of the program which I'd prefer not to be shown. I've tried using Systematize(out);
but all this does is change the color of the errors from red to the same color as the rest of the program. Here is the output of the program:
************Wolf"************
Name = Bob
Age = 2
Noise = Woof!
************Parrot************
Parrot's name = Polly
Age = 6
Noise = Argh!
************Exception examples************
java.lang.Exception: Carnivores only eat meat!
Exception caught above
Exception caught below
java.lang.Exception: Herbivores only eat plants!
************Herbivore non-caught Exception example************
Herbivores eat Vegetables
************Carnivore non-caught Exception example************
Carnivores eat Steak
************Omnivore eating habbit************
Omniivores eat meat or fruit and vegertables
************New herbivore animal************
Parrot's name = Haryy
Age = 2
Noise = Squeek!
************Animal being fed************
Wolf has eaten 10 times
************New wolf creation************
Name = newborn
Age = 0
************ArrayList before sorting************
Sam,5
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Wesley,7
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Sam,5
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Exception in thread "main" java.util.ConcurrentModificationException
George,3
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEachOrdered(ReferencePipeline.java:423)
at QuestionEditor.Main.main(Main.java:124)
C:\Users\lee-pc\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Here is the main method that outputs the part that contains errors:
Demo.main(args); // populate the ArrayList in Demo Class main method 2.
ArrayList<Animal> animalGroup = Demo.animalGroup; // Retrieve the ArrayList in Demo class.
animalGroup.stream().map((animal) -> {
System.out.println(animal.getName()+","+animal.getAge());
return animal;
}).map((Animal _item) -> {
System.out.println("************ArrayList after sorting************");
return _item;
}).map((Animal _item) -> {
Collections.sort(animalGroup,new AgeComparator()); // Sort highest to lowest
return _item;
}).forEachOrdered((Animal _item) -> {
animalGroup.forEach((animal2) -> {
System.out.println(animal2.getName() + "," + animal2.getAge());
});
});
Does anybody know why this is happening? And what is the best way of resolving this issue? Thanks for any feedback, much appreciated.
Upvotes: 0
Views: 84
Reputation: 124656
Collections.sort
modifies the content of animalGroup
if there are any elements not in sorted order.
You are doing this in the middle of iterating over animalGroup
.
If while iterating over the list some elements get swapped,
the outcome would be a mess.
For this reason it is forbidden to modify a list while iterating over it,
hence the ConcurrentModificationException
.
This is known as the fail-fast policy of collection implementations in Java,
you can read more about it here.
What you're doing with the streams doesn't make a lot of sense. It seems you're looking for this:
List<Animal> animalGroup = Demo.animalGroup;
animalGroup.forEach(animal -> System.out.println(animal.getName() + "," + animal.getAge()));
System.out.println("************ArrayList after sorting************");
Collections.sort(animalGroup, new AgeComparator()); // Sort highest to lowest
animalGroup.forEach(animal -> System.out.println(animal.getName() + "," + animal.getAge()));
Upvotes: 2