find an item from ArrayDeque and push it to last in Java

952 Views Asked by At

I have and ArrayDeque<Integer> which I am looping through to find an element and once found I want to push it to the bottom of the deque. But it gives me java.util.ConcurrentModificationException Error.

Any help with a demo is appreciated. Maybe using a different util as well.

2

There are 2 best solutions below

0
On

using this may help you

If you are using a pojo havin the Equals implemented is sufficient to help the list to remove the wanted Object

the pojo class

 class User {

    private String username;

    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }



    /**
     * Get the value of password
     *
     * @return the value of password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set the value of password
     *
     * @param password new value of password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get the value of username
     *
     * @return the value of username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Set the value of username
     *
     * @param username new value of username
     */
    public void setUsername(String username) {
        this.username = username;
    }



    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (!Objects.equals(this.username, other.username)) {
            return false;
        }
        if (!Objects.equals(this.password, other.password)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "\nUser{" + "username=" + username + ", password=" + password + '}';
    }


}

use case tutorial

    ArrayDeque arrayDeque = new ArrayDeque();
    arrayDeque.add("A");
    arrayDeque.add("B");
    arrayDeque.add("C");
    arrayDeque.add("D");

    System.out.println(arrayDeque);

    arrayDeque.remove("A");
    arrayDeque.add("A");
    System.out.println(arrayDeque);

    ArrayDeque<User> usersDeque = new ArrayDeque();
    final User userTaleb = new User("Taleb", "Mohammed");
    usersDeque.add(userTaleb);
    usersDeque.add(new User("Mansouri", "Abdellah"));
    usersDeque.add(new User("Ziane", "Nadir"));

    System.out.println(usersDeque);

    usersDeque.remove(userTaleb);
    usersDeque.add(userTaleb);

    System.out.println(usersDeque);
0
On

I do not see your example, but you problem is you use two different iterators (they could be implicit by JVM, e.g. when you use for loop) for reading and for modification deque. Each Iterator contains counter to check concurrent modification. To avoid java.util.ConcurrentModificationException you have to use one Iterator at the same time:

Variant 1:

public static void moveLast(Deque<Integer> deque, int val) {
    if (deque.remove(val))
        deque.add(val);
}

Variant 2:

public static void moveLast(Deque<Integer> deque, int val) {
    boolean exists = false;

    for (int i = 0, size = deque.size(); i < size; i++) {
        Integer item = deque.remove();

        if (exists)
            deque.add(item);
        else if (val == item)
            exists = true;
        else
            deque.add(item);
    }

    if (exists)
        deque.add(val);
}