Modify a Linkedlist using another Linkedlist

Asked

Viewed 105 times

0

I have a List1 and a List2. List1 is very large and List2 has some elements of List1. To avoid searching the entire List1 to make a modification and thus lose performance, I need to have a List2 in which I can change the data in a way that also changes 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 that?

  • 2

    Do you want to create a copy of the List and keep it up to date? Why do you want to do this?

  • In this case it would be a copy, but actually I have a List1 and a List2. List1 is very large and List2 has some elements of List1. To avoid searching the entire List1 to make a modification and thus lose performance, I need to have a List2 in which I can change the data in a way that also changes 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.

  • You’d better put that information in the question, as it is you might get answers that don’t suit you.

  • You’re right. I’ve already changed my question to make it clearer. Thanks for the tip.

  • 1

    Have you thought about making List2 a Map<Integer, Object> that has as key the index of List1 (the "address") and as value the Item that is in List1 in this Index?

  • Do you say Hash table? I’ve never heard of Map. Can I search about.

  • 1

    What kind of data are you saving to List?

  • My Linkedlist is as follows: public LinkedList<Evento> eventos = new LinkedList<>(); Where in Event I have getters and setters for my data as price, for example. Such list is loaded with about 250,000 positions. I have a second list, public LinkedList<Evento> usuario = new LinkedList<>(), which should hold some positions of the first one for me to modify when necessary, without going through the first one in full.

  • @Douglas Table Hash is a data structure that can be used to implement, among other things, the abstract type Map. The concept is one thing and the implementation is another. Similarly, Linkedlist is an implementation of the List concept using pointers, but there are other ways to do it. It is always interesting to think in the most abstract way possible, not to get stuck to implementation details.

Show 5 more comments

1 answer

2

The Java Linkedlist does not expose internal Nodes, so it is not possible for you to reference directly from a second Linkedlist.

If you want to update a property of a list item instead of changing the entire item, just add the same element in both lists, which 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 only property is Event - say, Eventoholder, and add the same Eventoholder to both lists.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.