compare two java objects

Asked

Viewed 1,136 times

-2

good evening. I am working with an arraylist, and needed to ensure that no repeated objects are inserted. My object has a matrix[5][5] which is the main element that I need to ensure is not repeated. someone would have some suggestion?

  • 1

    Wouldn’t it be better to use a TreeSet?

  • So, I did the whole program in arraylist. Maybe you would know how to go from treeset to arraylist, then just change this part of insertion.

  • 1

    You would need to understand how your code is working to see if it is possible or not to use a TreeSet, but if it cannot repeat elements, usually the Set is far more appropriate.

  • 1

    Please show how your code is. It is unclear what you are trying to do: you have a ArrayList with elements in the form matriz[5][5]? What is the type of this matrix? Is it whole that cannot be repeated? Or is your data represented differently? The Set (TreeSet, HashSet) is the natural way to create sets (i.e. collections where elements do not repeat), you do not need to do anything else but add them, so it makes no sense to "go from treeset to arraylist". Show us your code and we can probably help you better.

  • It is a matrix[5][5] integer, with Treeset will work. But in the comparison methodTo, I will have to compare element by element of this matrix?

  • @Edit your question by placing the code you have so far, will help a lot... As to compare, I do not know if the TreeSet is the most appropriate solution, perhaps the HashSet is better. If your elements can be ordered, the TreeSet is better (then you will use the compareTo or a Comparator), otherwise the HashSet is better (then you will use the hashCode). And to ensure that two elements are not repeated, you have to compare them completely, no?

  • blz, I’m gonna use treeSet here if the code doesn’t work.

  • 1

    where you come in comparar dois objetos em java in your question? arrange the title, post your code and explain better what you want to do

  • public class BingoSorte { private String nome; private int identificador = 0; private int cartela[][] = new int[5][5]; private int cont;} This is my entity with its attributes. This Card[][] is filled with integers. Using a Treeset<Bingosorte> to store objects of the above type. The question is. How should I implement my compareTo body so that when inserting objects, it compares so that it does not insert repeaters.In case how to compare the cards[][] of each object.

  • 2

    Do you have a Cartela class? The cartela attribute of your Bingosorte class is for what? Please add the question details in the question and not in the comments

Show 5 more comments

1 answer

2

It’s unclear what you’re getting at, but I’ll try to answer with what I’ve understood so far:

If you cannot have repeated elements, a Set (set) is the most appropriate data structure. It remains to know which set is best for you.

If its elements can be placed in a total order, then the TreeSet is the most suitable: not only does it guarantee uniqueness, but it also keeps its elements in order:

Comparator<int[][]> c = new Comparator<int[][]>() {
    public int compare(int[][] a, int[][] b) {
        ...
    }
};
TreeSet<int[][]> c = new TreeSet<int[][]>(c);

Otherwise, a HashSet is more appropriate. However, comparing Java arrays is by reference, not by value. If you want the content of the arrays are compared, it is necessary to use an object wrapper who makes use of deepEquals and deepHashCode:

class MeuObjeto {
    private int[][] array;

    public boolean equals(Object o) {
        if ( o instanceof MeuObjeto )
            return Arrays.deepEquals(this.array, ((MeuObjeto)o).array);
        else
            return false;
    }

    pubilc int hashCode() {
        return Arrays.deepHashCode(array);
    }
}
HashSet<MeuObjeto> c = new HashSet<MeuObjeto>();

Finally, if you want to use the HashSet but would like the elements to be returned in the same order in which they were entered, you can use LinkedHashSet instead of it. The example is identical to the previous item, only changes the class.

Reference article.

Browser other questions tagged

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