Skip to content
Advertisement

How to search an item in a queue type list and then change it in Java?

I’m trying to find an item of a node in my queue type list, in which it goes through it until it finds that and then replace it, for example:

I have a “person” object with its respective get and set

public class Person {

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

I have a generic “node” with its respective get and set

public class Node<E> {

    private E item;
    private Node nextNode;

    public Node(E item) {
        this.item = item;
        this.nextNode = null;
    }
}

And finally I have a manual generic “queue” with its respective methods (like add)

public class QueueList<E> {

    private Node<E> firstNode;
    private Node<E> lastNode;

    public QueueList() {
        this.firstNode = null;
        this.lastNode = null;
    }

    public void add(E item) {
        if (lastNode == null) {
            lastNode = new Node<>(item);
            firstNode = lastNode;
        } else {
            Node<E> newNode = new Node<>(item);
            lastNode.setNext(newNode);
            lastNode = newNode;
        }
    }
}

Then I create a queue list of people

QueueList<Person> peopleList = new QueueList<>();

peopleList.add(new Person("Mary", 20));
peopleList.add(new Person("John", 24));

Maybe at some point someone’s age will need to be changed, so I’ll ask the user to write the person’s name he wants to replace the age, (for example I want to change John’s age from “24” to “25”, and I know it will be set through person.setAge()).

How could I go through the list to get the node of the person’s name, to then replace it?

Thanks. 🙂

Advertisement

Answer

Simple solution would be to check them all and update when found

  boolean searchAndReplace(QueueList<Person> peopleList, String name, int age) {
        Node current = peopleList.firstNode;
        if(current == null) {
            System.out.println("Queue is empty");
        }
        else {
            while(current != null) {
                //Compares node to be found with each node present in the list
                if(name.equals(((Person)current.getItem()).getName())) {
                    ((Person)current.getItem()).setAge(age);
                    return true;
                }
                current = current.getNextNode();
            }
        }
        return false;
    }

I also added a boolean to return when something is found (might be nice to know).

Note that

  • it will only change the first one found. You might want to skip the return when found and go through the whole list every time.
  • you cannot create the method in the QueueList class with generics as you do not know what you can check against. Alternative is to create an interface that implements a nodeEquals and a nodeUpdate method
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement