Two key hash sets?

I need a HashSet implementation where the elements are a pair of integers
eg. Set s = { {1,2} , {3,4} , {1,4}} . Here the set s has 3 elements.

This kind of two key HashSet is necessary in many situations, such as: I have a relationship in my database where the candidate key is a combination of two columns.
Is there some kind of library that already provides this? If such an implementation is not available, then instead of embedding the entire data structure from scratch, will it be easier (and efficient?) To extend the HashSet implementation in Java?

+4
source share
2 answers

For this requirement, I would create a data holder with 2 integers as attributes and provide an implementation of equals and hashcode. Then put these objects in Set.

+8
source

And won't this work to put an array with 2 elements as a member of the set? I.e:.

 Set<int[]> s = new HashSet<int[]>(); s.add(new int[] {1,2}); s.add(new int[] {3,4}); 

or create a CandidateKey class that has two fields and custom methods equals() and hashCode() ?

That’s all said, are you sure you want to process object-relational mapping (mapping from database to objects) yourself, and not use a library like Hibernate or EclipseLink?

+1
source

Source: https://habr.com/ru/post/1306551/


All Articles