Modify a LinkedList using another LinkedList

0

I have a List1 and a List2. List1 is very large and List2 has some elements from List1. To avoid looking for the entire List1 to make a modification and thus lose performance, I need to have a List2 in which I can change the data so that I also change in List1.

Example: My list one has 250000 elements. I need to change element 210100. If I can do this by changing a second list where I only have the data that I will change in the future, I gain more performance.

How can I do this?

    
asked by anonymous 08.05.2017 / 20:23

1 answer

2

The Java LinkedList does not expose internal Nodes, so you can not directly reference a second LinkedList.

If you want to update a property of a list item instead of changing the entire item, simply add the same element in both lists, the memory reference will be the same:

LinkedList<Evento> evento = new LinkedList<>();
LinkedList<Evento> usuario = new LinkedList<>();

Evento e = new Evento();

evento.add(e);
usuario.add(e);
//Altera uma propriedade numa lista:
usuario.getFirst().nome="Jose"; 

//Veja que se aplicou à outra lista também:
System.out.println(evento.getFirst().nome); //Jose

If you want to completely replace the element, you can create another class whose sole property is Event - say, EventHolder, and add the same EventHolder to both lists.

    
09.05.2017 / 00:26