Let's say I have this code where Element is a class:
import java.util.*; import java.io.*; public class HelloWorld { public static void main(String[] args) { Deque<Element> list = new LinkedList<Element>(); Element ele3 = new Element(); ele3.x = 3; ele3.y = 3; q.add(ele3); } } class Element { public int x; public int y; public String moves; } Let's say that during the run of my program, I come across an element with x=3 and y=3 which has better Element.moves than the previous one inside the queue. What I want is to remove the last one from the queue and add this one. To do this I could use the method removeFirstOccurrence of Dequeue. Since I don't know the last object's (with x=y=3) name how can I apply this method only knowing the object's x and y?
An other solution I thought is creating an array of objects and hash tuple (x,y) with i value so if I have (x,y) I could know that these belong in let's say A[i] object. In this case the problem is that I don't know how many objects I will need to create and thus can't initialize an array. Moreover if I use ArrayList or something similar I can't create let's say an A[i] object and refer to it using i.
Any ideas?
stream()the collection, filter on the correct values ofxandy,findFirst()then remove it if present. But the underlying problem is almost certainly that you're not using the correct data structure for the task at hand.