I am trying to compare two different List and remove the duplicates. However, the two lists have two different object types and only common attribute is app name.
Here is the code,
public List<TvAppsType> getAvailableAppsTypesByCompanyIdSecond(int comapnyId) {
// put apps to the model that belong to the given company id
TVAppService tvAppService = new TVAppService();
List<ThreatviewApp> apps = new CopyOnWriteArrayList<ThreatviewApp>();
apps = tvAppService.getAllAppsforCompanyId(comapnyId);
// get list of app types
TvAppTypeService types = new TvAppTypeService();
List<TvAppsType> apptypes = new CopyOnWriteArrayList<TvAppsType>();
apptypes = types.getAppTypes();
// add the items to collection for removing
for(TvAppsType app : apptypes){
System.out.println("-----------------------");
System.out.println("app : " + app.getAppType_name());
}
Iterator<TvAppsType> itertypes = apptypes.iterator();
Iterator<ThreatviewApp> it = apps.iterator();
while (itertypes.hasNext()) {
TvAppsType apptype = itertypes.next();
while (it.hasNext()) {
ThreatviewApp tvapp = it.next();
if (tvapp.getApp_name().trim().equals(apptype.getAppType_name().trim())) {
itertypes.remove();
}
}
}
for(TvAppsType app : apptypes){
System.out.println("-----------------------");
System.out.println("app : " + app.getAppType_name());
}
return apptypes;
}
Problem is this works only in first iteration, I suspect that after the List is modified, the iteration behavior is unspecified.
void remove() Removes from the underlying collection the last element
returned by this iterator (optional operation). This method can be
called only once per call to next(). The behavior of an iterator is
unspecified if the underlying collection is modified while the
iteration is in progress in any way other than by calling this method.
As I am trying to modify the lists runtime, I used CopyOnWriteArrayList
bascically, I followed this article article
why the iteration stop after first one? How to fix this and remove all duplicates?
In addition to #Eran's answer, you have another problem. Once you have removed a TvAppsType (in the inner while loop), you should never attempt to remove the same object again. Also, quitting the inner loop as soon as possible will speed up your algorithm.
So, the inner loop should look like this:
while (it.hasNext()) {
ThreatviewApp tvapp = it.next();
if (tvapp.getApp_name().trim().equals(apptype.getAppType_name().trim())) {
itertypes.remove();
break;
}
}
You have to reset the iterator of the inner loop if you wish to iterate more than once on the apps list. Otherwise, the inner while loop will iterate over the apps list only one time, after which it.hasNext() will be false.
Iterator<TvAppsType> itertypes = apptypes.iterator();
while (itertypes.hasNext()) {
TvAppsType apptype = itertypes.next();
Iterator<ThreatviewApp> it = apps.iterator(); // the inner iterator must be
// initialized in each iteration
// of the outer loop
while (it.hasNext()) {
...
Related
I am iterating over a list of POJO objects called txnList (say, size = 2). If an object falls into an invalid criteria, then I am removing it from this txnList list (now, size = 1). However, I want to process the rest of the elements in txnList but since the size is 1, it doesn't process further and exits from the top most loop altogether.
Below is my code, with bare minimum essential details:
for (OrgTransaction txn : txnList) {
String txnId = txn.getTxnNumber());
Order order = util.getOrder(txnId);
boolean isInvalid = false;
List<CollectionType> collectionTypeList = util.getCollectionType(order);
for (CollectionType collectionType : collectionTypeList) {
if (isNotValid(collectionType)) {
// first transaction in the list is invalid so it enters here
txnList.remove(txn);
isInvalid = true;
// Breaking since we do not want to process anything in this nested list if one of the invalid things is found.
break;
}
}
if (!isInvalid) {
// process only valid transactions further
}
// NOTE: now after first iteration of processing invalid case, this should go to top to iterate 2nd transaction but it just returns from the list since the new size is 1 and 1 time the `txnList` loop has iterated.
}
FYI: No exception is thrown in this code as a result of deletion
Remove the item with the iterator, which needs to know about the operation.
for (Iterator<OrgTransaction> iterator = txnList.iterator(); iterator.hasNext(); ) {
OrgTransaction txn = iterator.next();
...
iterator.remove();
...
}
This also avoids exceptions due to concurrent modification.
There are 2 possible ways:
create a list of elements you want to remove, then use list.removeAll(removeList)
for(...){
if (isInvalid) {
removeList.add(txn)
}
...
txnList.removeAll(removeList);
while traversing a list, create a new list which does not contain elements you do not need. Then just reassign.
for(...){
if (!isInvalid) {
newList.add(txn)
}
...
txnList = newList;
One method you can use is to just use a for loop and decrement the index each time you remove an element:
for (int x = 0; x < txnList.size(); x++;) {
txn = txn.get(x); //define txn
String txnId = txn.getTxnNumber());
Order order = util.getOrder(txnId);
boolean isInvalid = false;
List<CollectionType> collectionTypeList = util.getCollectionType(order);
for (CollectionType collectionType : collectionTypeList) {
if (isNotValid(collectionType)) {
// first transaction in the list is invalid so it enters here
txnList.remove(txn);
x--; //decrement index
isInvalid = true;
// Breaking since we do not want to process anything in this nested list if one of the invalid things is found.
break;
}
}
if (!isInvalid) {
// doSomething
}
// NOTE: now after first iteration of processing invalid case, this should go to top to iterate 2nd transaction but it just returns from the list since the new size is 1 and 1 time the `txnList` loop has iterated.
}
Given the following datatype Testcase (XQuery, Testpath, FirstInputFile, SecondInputFile, Expected)
how can I properly delete duplicates.
Definition of duplicates:
If FirstInputFile already in the list as SecondInputFile vice versa.
Here is the Testdata
tcs.add(new HeaderAndBodyTestcase("XQ 1", "/1", "FAIL", "FAIL2", "FAILED"));
tcs.add(new HeaderAndBodyTestcase("XQ 1", "/1", "FAIL2", "FAIL", "FAILED"));
tcs.add(new HeaderAndBodyTestcase("XQ 2", "/2", "FAIL4", "FAIL3", "FAILED2"));
tcs.add(new HeaderAndBodyTestcase("XQ 2", "/2", "FAIL3", "FAIL4", "FAILED2"));
and here is the function
protected void deleteExistingDuplicatesInArrayList(final ArrayList<HeaderAndBodyTestcase> list) {
for (int idx = 0; idx < list.size() - 1; idx++) {
if (list.get(idx).firstInputFile.equals(list.get(idx).secondInputFile)
|| (list.get(idx + 1).firstInputFile.equals(list.get(idx).firstInputFile)
&& list.get(idx).secondInputFile.equals(list.get(idx + 1).secondInputFile)
|| (list.get(idx).firstInputFile.equals(list.get(idx + 1).secondInputFile)
&& list.get(idx).secondInputFile.equals(list.get(idx + 1).firstInputFile)))) {
list.remove(idx);
}
}
}
This solution is already working, but seems very crappy, so is there a better solution to this?
put everything in a Set using a comparator if necessary, and create a list from this set if you really need a List (and not a Collection)
Set<HeaderAndBodyTestcase> set = new Hashset<>(list);
Given your rather peculiar "equality" constraints, I think the best way would be to maintain two sets of already seen first- and second input files and a loop:
Set<String> first = new HashSet<>();
Set<String> second = new HashSet<>();
for (HeaderAndBodyTestcase tc : tcs) {
if (! first.contains(tc.getSecondInputFile()) &&
! second.contains(tc.getFirstInputFile())) {
first.add(tc.getFirstInputFile());
second.add(tc.getSecondInputFile());
System.out.println(tc); // or add to result list
}
}
This will also work if "equal" elements do not appear right after each other in the original list.
Also note that removing elements from a list while iterating the same list, while working sometimes, will often yield unexpected results. Better create a new, filtered list, or if you have to remove, create an Iterator from that list and use it's remove method.
On closer inspections (yes, it took me that long to understand your code), the conditions in your current working code are in fact much different than what I understood from your question, namely:
remove element if first and second is the same (actually never checked for the last element in the list)
remove element if first is the same as first on last, and second the same as second on last
remove if first is same as last second and vice versa
only consider consecutive elements (from comments)
Given those constraints, the sets are not needed and also would not work properly considering that both the elements have to match (either 'straight' or 'crossed'). Instead you can use pretty much your code as-is, but I would still use an Iterator and keep track of the last element, and also split the different checks to make the whole code much easier to understand.
HeaderAndBodyTestcase last = null;
for (Iterator<HeaderAndBodyTestcase> iter = list.iterator(); iter.hasNext();) {
HeaderAndBodyTestcase curr = iter.next();
if (curr.firstInputFile.equals(curr.secondInputFile)) {
iter.remove();
}
if (last != null) {
boolean bothEqual = curr.firstInputFile.equals(last.firstInputFile)
&& curr.secondInputFile.equals(last.secondInputFile);
boolean crossedEqual = curr.secondInputFile.equals(last.firstInputFile)
&& curr.firstInputFile.equals(last.secondInputFile);
if (bothEqual || crossedEqual) {
iter.remove();
}
}
last = curr;
}
I'm learning to iterate, and have implemented an Iterator on my 'CStickChart' Class using the following private property:
private List<CStick> cStickCollection = new ArrayList<CStick>();
and then implementing the method to return CSticks:
public Iterator<CStick> iterator() {
return this.cStickCollection.iterator();
}
Now when I try and iterate through it, I'm able to do so with the assigned localCStick but calling the next() method on the CStickChart Iterator doesn't do what I expected it to. I expected it to give me the next CStick in my CStickChart (hence when I call the getEPIC I was expecting it to give me the next EPIC along).
// Print the EPIC out of the Array using the iterator
for (CStick localCStick : testCStickChart) {
System.out.println(localCStick.getEPIC());
//The below line doesn't return the next CStick and I'm not sure why
System.out.println("next EPIC is " + testCStickChart.iterator().next().getEPIC());
}
Please could someone explain why this is not the case (it always returns the first EPIC)
System.out.println("next EPIC is " + testCStickChart.iterator().next().getEPIC());
This happens because in this line you are getting a new iterator in every iteration of the loop. Each new iterator starts from the beginning of the list again.
It sounds like you don't want to use the enhanced-for structure. The reason: an enhanced-for with an iterable entity will use the iterator provided internally, and will only ever advance forward.
This also means that any calls to a iterator while inside that loop produce an iterator that starts at the beginning of iteration.
So, with that, you have two options - both of which involve abandoning the enhanced-for:
Use a standard for loop with indexing to advance backwards and forwards with the list, or
Use a ListIterator as provided by List to move backwards and forwards in a very seamless way.
Here is an example with using integers - note that every time I advance the iterator I have to move it back to its previous spot so that I don't double-advance it. Also, I have a condition to break out of the loop once we've run out of elements.
List<Integer> integerList = new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
add(4);
add(5);
add(6);
add(7);
add(8);
add(9);
add(10);
}};
for (ListIterator<Integer> iterator = integerList.listIterator(); iterator.hasNext(); ) {
int value = iterator.next();
int nextValue = Integer.MIN_VALUE;
if (iterator.hasNext()) {
nextValue = iterator.next();
// Reset the state of the iterator
iterator.previous();
}
System.out.println("Value = " + value);
if(nextValue != Integer.MIN_VALUE) {
System.out.println("Next value = " + nextValue);
}
}
Because you are getting the top iterator of cStickCollection with .iterator(). I think you wanted to use the same iterator position as you're at in your loop, and peek at the next element. You can't do that with a for-each loop and you also can't do that with an Iterator in general (because they don't implement a peek).
Instead, you could use a traditional for loop on your cStickCollection like
for (int i = 0, len = cStickCollection.size(); i < len; i++) {
CStick localCStick = cStickCollection.get(i);
System.out.println(localCStick.getEPIC());
if (i + 1 < len) { // <-- check that there is a "next"
System.out.println("next EPIC is "+cStickCollection.get(i+1).getEPIC());
}
}
Everytime you call testCStickChart.iterator() inside of that loop, you create a new iterator object. So each call to next() is carried out on a new iterator object, returning the first object. What you want to do is to declare a new Iterator<CStick> just before the loop and use it inside the loop, like so:
Iterator<CStick> it = testCStickChart.iterator();
// Print the EPIC out of the Array using the iterator
for (CStick localCStick : testCStickChart) {
System.out.println(localCStick.getEPIC());
//The below line doesn't return the next CStick and I'm not sure why
System.out.println("next EPIC is " + it.next().getEPIC());
}
I have the following while loop, if I put this.boatTripsList.iterator().hasNext() in the while loop condition, it throws error. When I create iterator then put in the while loop condition, it will work then. Why is this? Thanks & Regards. (the second version throws error)
public Journey(List<BoatTrip> trips) {
this.boatTripsList = new LinkedList<BoatTrip>();
Iterator<BoatTrip> iterator = trips.iterator();
//add the given boat trips to the boattrips list
while (iterator.hasNext()) {
BoatTrip thistrip = iterator.next();
this.boatTripsList.add(thistrip);
}
}
public Journey(List<BoatTrip> trips) {
this.boatTripsList = new LinkedList<BoatTrip>();
//add the given boat trips to the boattrips list
while (trips.iterator().hasNext()) {
BoatTrip thistrip = iterator.next();
this.boatTripsList.add(thistrip);
}
}
This is normal: if your while condition is while(trips.iterator().hasNext()), you create a new iterator each time. If your list is not empty, the condition will therefore always be true...
While in the loop itself, you use the iterator you created before entering the loop... As a result, you'll get a NoSuchElementException when this iterator is empty.
Use:
final Iterator<Whatever> = list.iterator();
Whatever whatever;
while (iterator.hasNext()) {
whatever = iterator.next();
// do whatever stuff
}
But for walking lists, a foreach loop is preferred:
for (final BoatTrip trip: tripList)
// do whatever is needed
And if you want to add the contents of a list to another, use .addAll():
// no need for the "this" qualifier, there is no name conflict
boatTripList.addAll(trips);
You aren't using the iterator you requested on the first line of your code there - you're requesting a new one each time, so it will always have a next.
A call to .iterator() obtains a new iterator. If you do that in the loop, you will always obtain a new iterator rather than iterating over an existing iterator.
this.boatTripsList.iterator().hasNext() is wrong
this.boatTripsList.hasNext() is correct
Im playing around with some code for my college course and changed a method from
public boolean removeStudent(String studentName)
{
int index = 0;
for (Student student : students)
{
if (studentName.equalsIgnoreCasee(student.getName()))
{
students.remove(index);
return true;
}
index++;
}
return false;
}
To:
public void removeStudent(String studentName) throws StudentNotFoundException
{
int index = 0;
for (Student student : students)
{
if (studentName.equalsIgnoreCase(student.getName()))
{
students.remove(index);
}
index++;
}
throw new StudentNotFoundException( "No such student " + studentName);
}
But the new method keeps giving a Concurrent Modification error. How can I get round this and why is it happening?
It is because you continue traversing the list after performing remove().
You're reading and writing to the list at the same time, which breaks the contract of the iterator underlying the foreach loop.
Use Iterator.remove()
for(Iterator<Student> iter = students.iterator(); iter.hasNext(); ) {
Student student = iter.next();
if(studentName.equalsIgnoreCase(student.getName()) {
iter.remove();
}
}
It is described as the following:
Returns the next element in the iteration.
Throws NoSuchElementException if the iteration has no more elements.
You can use Iterator.hasNext() to check if there is a next element available.
foreach construct uses an underlying Iterator.
In the second method you continue to iterate even after removing an item from the list. This is resulting in the exception that you see. Take a look at this statement taken from ConcurrentModificationException documentation:
For example, it is not generally permissible for one thread to modify
a Collection while another thread is iterating over it. In general,
the results of the iteration are undefined under these circumstances.
Some Iterator implementations (including those of all the general
purpose collection implementations provided by the JRE) may choose to
throw this exception if this behavior is detected.
You are not allowed to remove an element from your collection while you iterate over it. The iterator detects a structural change during its usage, and throws the exception. Many collections are implemented in such a way.
Use the iterator directly instead:
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student student = it.next();
if (studentName.equalsIgnoreCase(student.getName())) {
it.remove();
return true;
}
}
return false;
you can avoid concurrent modification error buy just breaking the loop after removing the element or if the method has a return type return a value after removing the element.
This error occurs because you are trying to alter the size of a collection while you are iterating it. If you have 10 students, you start your loop expecting to go through 10 iterations. When you remove a student, how many iterations do still need to go? The answer obviously depends on where you removed your student from the list and where you currently are in your iteation. Obviously, java cannot know this.
To get around this, you must use an iterator. You can accomplish this as follows:
Iterator<Student> studentsIterator;
for(studentsIterator = students.iterator(); studentsIterator.hasNext();)
{
Student student = studentsIterator.next();
if(student... /* condition */)
{
studentIterator.remove(); //This removes student from the collection safely
}
}
You are not allowed to remove an element from students collection while iterating through it.
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances.
http://docs.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html
Try changing to
Iterator<Student> itr = students.iterator();
while (itr.hasNext()) {
Student student = itr.next();
if (studentName.equalsIgnoreCase(student.getName()))
{
itr.remove();
}
}
If you want to remove inside a loop you should use an iterator and its remove method
public boolean removeStudent(String studentName)
{
Iterator<Student> itS = students.iterator();
while(itS.hasNext())
{
Student student = itS.next();
if (studentName.equalsIgnoreCasee(student.getName()))
{
itS.remove();
return true;
}
}
return false;
}
You shouldn't delete objects from a collection while using
a for-each statement - this will cause exceptions as your iterator faces a changed collection in the course of its iterations. (the for loop)
either use a regular for loop (for int i = 0; i < 100; i++) etc...
or keep the objects to remove in a list, and remove them outside of the for loop.
Also, you remove the object by index where index is : 0 , 1 , 2
but index should actaully be the index of the student.