Store <Integer,Integer,Arraylist<Integer>> in a collection

Asked

Viewed 158 times

2

What I intend to do is make a data association as follows:

(<<Integer>, <Integer>, ArrayList<Integer>)

I tried to create a HashMap<Integer,HashMap<Integer,ArrayList<Integer>>> the problem is I forgot the keys have to be unique and I need to have equal values sometimes.

Ex: (1,1,{1,2,3,4}) (1,2,{1,3})

How best to aggregate this data?

  • and if you create a class with these values does not give?

  • I have not experienced or know how well I do. A class that holds these values? Values are not static.

  • The fact that the values are not static is unimportant, because you would update the class through the get and set, but you would have the same key problem (now thinking), because how would you distill the class x from the class y, if the values you compare are equal? Are you sure you can’t get a single key?

  • it is possible for the two keys to be the same in other example: (<<1>, <1>, ArrayList<Integer>), (<<1>, <2>, ArrayList<Integer>) , (<<1>, <1>, ArrayList<Integer>): type in this case example 1 is equal to 3 ?

  • It’s the same but the 3 examples have to be inserted in the BD. Using Hashmap he wouldn’t duplicate my data and I need to be able to duplicate them

  • because with the HasMap It replaces the older one with the new one when they’re the same, so it doesn’t... That data will be changed that is, after being in the collection needs updated?

  • No, once they’re in the collection, they’re sent straight to the comic book and you don’t care about those figures anymore. Simply afterwards I can make another collection and send other data, but being in the collection, they are only sent to the comic

  • What I would do, @Hugo, is create a separate class to support the data you need, and together with this data implement a variable id, because then in the hr of persisting, with the proper manipulation, you could insert identical objects more than once.

Show 3 more comments

1 answer

5


One possible solution is to create the class where you aggregate this data, and then create a list of all objects in that class.

//crias a classe com todos os dados que o teu objeto precisa
public class myNewObjecto
{
    private integer num1;
    private integer num2;
    private ArrayList<Integer> listNumeros;

    //crias os get/set

    /
    @Override
    public int hashCode() {
         //implementar o hashCode
    }

    @Override
    public boolean equals(Object obj) {
         //implementar o equals   
    }

}

Cubs to list to guard your objects:

private ArrayList<myNewObjecto> listmyNewObjecto;

Then add the data to your object:

//se criares o construtor na class fazes:
myNewObjecto obj = new myNewObjecto(1,2,listaInteiros);
//senão usas os set´s
myNewObjecto obj = new myNewObjecto();
obj.setNum1(1);
obj.setNum2(2);
obj.set(listNumeros);

//adicionas o objeto a tua lista
listNumeros.add(obj);
  • The point is that the values I want to add to the created list or object are in a class and come from checkbox values

  • Yes, but what is your doubt exactly? go to check box for the values or for being in different classes?

  • It is because it is in different classes. Just put the Checkbox as public to have access in the new class?

  • Do everything you can to avoid using public access to properties. Does Checkbox have an event that is activated when changed, or when a form is sent for example? If yes, it is only in having a fixed reference of an object of the new class, and at the time of the event, set the new value.

  • Not that is not the logic that you should follow (although it works) your new class myNewObjecto will be a class alone (as I think it should already be), in the other class where you have the checkbox, you create the object, or the list. And you also create the get that will return you that list to the other class... I don’t know if I can figure it out, it’s not easy to explain like this

  • 1

    @Hugomachado You got it working?

  • I’ll try now, I’ve had other things back. One question, it might be an Inner Class?

  • If I understand you correctly, I don’t think so, because you’re gonna need it in several classes.

  • So I’m going to create a separate class. Can I briefly explain what this is about Hashcode and the implementation of equals?! Sorry, I’m learning :D Thank you in advance for your availability

  • 1

    No problem, I am also learning soon my explanation may not be the best, but summarizing both the hashcode and equals are used for when comparing objects and I advise you to take a read on it, it is really very important, because one thing and when you have (1).equals(1) another is when you have (conta).equals(conta), once again I can’t explain here, but if you use the Netbeans (and almost sure that in eclipse also has these features) it creates it automatically, I do not mean that it is exactly what you need, but it gives to have an idea...

  • Áhh I get the idea :) I’m trying to do, anything I ask for help. At the end of adding the entries to my list, how can I go through the list and search for each element of each object ?

  • I already got what I wanted. It worked well! Thanks @jsantos1991 :D

  • cool... glad you made it...

Show 8 more comments

Browser other questions tagged

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