简体   繁体   中英

remove method removes item from first list, but doesn't from the rest

I have this issue in my remove method that I don't quite understand.

When executing this method, it iterates through the first map (titleMap) and removes the title of the book item from that map, but will not go further on to execute the two other iterators.

A book item has a title, seller and price. There is a map for each attribute: titleMap, sellerMap, priceMap. I want to be able to remove the complete book item, so remove title of book from titleMap, seller from sellerMap and price from priceMap.

I've attempted to merge the three iterators together but to no avail. If anyone could help me find the reason for this that would be great. Thanks.

Input:

ListMultiIndex list = new ListMultiIndex();
    list.add("ONE", "Chris", 2);
    list.add("TWO", "Bob", 35);
    list.add("THREE","David", 100);
    list.displaySizeOfList();

    try {
        list.remove(new BookItem ("THREE","David", 100));
        list.remove(new BookItem ("TWO", "Bob", 35));
    } catch (BookStoreException e){
        e.printStackTrace();
    }
    list.displaySizeOfList();

Output:

The size of titleMap is: 3
The size of sellerMap is: 3
The size of priceMap is: 3
book Removed (via titleMap)with title name: THREE
book Removed (via titleMap)with title name: TWO
The size of titleMap is: 1
The size of sellerMap is: 3
The size of priceMap is: 3

Output without error should look like:

The size of titleMap is: 1
The size of sellerMap is: 1
The size of priceMao is: 1

Method:

@Override
public void remove(IBookItem book) throws BookStoreException{

                boolean isRemoved = false;

    for(BookEntry<String,ArrayList<BookItem>> entry : titleMap){
        if(entry.getKey().equals(book.getTitle())){
            if(entry.getValue().contains(book)) {
                entry.getValue().remove(book);
                System.out.println("book Removed (via titleMap)with title name: " + book.getTitle());
                if(entry.getValue().isEmpty()) {
                    isRemoved = true;
                    System.out.println("book removed (titlemap)");
                    titleMap.remove(entry);
                }
            }
        }
    }   
    isRemoved = false;
    for(BookEntry<String,ArrayList<BookItem>> entry2 : sellerMap){
        if(entry2.getKey().equals(book.getSeller())){
            if(entry2.getValue().contains(book)) {
                entry2.getValue().remove(book);
                System.out.println("entry.getValue,getremove");
                System.out.println("book Removed (via titleMap)with title name: " + book.getSeller());
                if(entry2.getValue().isEmpty()) {
                    isRemoved = true;
                    System.out.println("book removed (titlemap)");
                    sellerMap.remove(entry2);
                }
            }
        }
    }

    isRemoved = false;
    for(BookEntry<Integer,ArrayList<BookItem>> entry3 : priceMap){
        if(entry3.getKey().equals(book.getSeller())){
            if(entry3.getValue().contains(book)) {
                entry3.getValue().remove(book);
                System.out.println("entry.getValue,getremove");
                System.out.println("book Removed (via titleMap)with title name: " + book.getSeller());
                if(entry3.getValue().isEmpty()) {
                    isRemoved = true;
                    System.out.println("book removed (titlemap)");
                    sellerMap.remove(entry3);
                }
            }
        }
    }
}

I answered your other question but I didn't realize you wanted every loop to run. Instead of using return just use break . If there is a possibility that multiple item may match in your loops don't break at all and just let the loop finish naturally. I would still advise you to return objects that were removed as a way to tell if the method worked. Throwing an exception is really not the correct way.

I think your problem is also that you are iterating over an iterator and removing things from the backing collection. Instead of this:

titleMap.remove(t);

You should use:

it.remove(t);

Removing items from a collection white iterating is a no no.

Another thing I would do is change your iterator loops. Using a while loop is considerably more readable. Example:

Iterator<BookEntry<String, ArrayList<BookItem>>> it = titleMap.iterator()
while (it.hasNext())
{
  /*if book doesn't exist*/
  it.remove();
}

You have this line in your loops return ;

When this line is executed, the whole method ends and do not continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM