Better to use a list of pairs or two lists?

I am writing a method that is part of the public interface of the Java class. On the whole, it allows the caller to specify values ​​for assignment to several database entities - therefore, they must provide both identifiers of the objects themselves and values ​​for their assignment.

I am trying to implement this as List<Pair<Integer, Integer>>or only two arguments List<Integer>. Both will obviously work, and none of them will cause any problems with implementation or efficiency in my method. This is basically the same information anyway (2xn array), just alternating in different ways.

So, I would like some opinions on what you think would be better, and why.

The advantages that I see so far for the list of pairs:

  • More accurately reflects the actual relationship between objects
  • Eliminates some dynamic error classes (for example, inconsistent list lengths)

Benefits for a pair of lists:

  • Does not rely on any classes other than the JDK (just as it Pairshould be understood as a concept)
  • It does not require the creation of any auxiliary objects for transferring data around
  • Callers most often have arguments in separate lists, so they don’t need to rebuild the data before calling the method

, (, , - , ). , Integer, - PrimaryKey, , .

, - objectId . , . , , .

+5
11

, , , . , , .

. . , , ( -JDK-, Java , ).

, . . IDE .

+21

, , , .

Pair, :

  • (, )

.

, . , .

Pair , , . , Pair , - , List .

+5

( , Java , , Hashtable)? .

+5

API , , , , , .

(Integer entityId, Integer someOtherId) , .

, acces util, "Assignment", getEntityId() getSomeOtherId() , DefaultAssignment, .

, , "Pair".

+2

List of Pair s, " ".

, , . ; .

, Pair, , , JDK ", , - Pair, : List<Integer[]>, Integer. (-, , Pair).

+2

- - , , , .

, , - ( ).

+2

:

  • - , JDK (, Pair, ).

, JDK Pair, : Map.Entry, /.

+1

, ( ), , -, , . .

0

.

"con".

. - , - , , . , , . , , . , .

0

. , , .

0

You might want to make parameter a Iterator<Pair>. If the calling class has List<Pair>, getting the iterator is trivial. If not, creating an iterator can be easier to build, faster, and / or more natural. In some cases, this may even help you resolve memory problems.

0
source

All Articles