用户登录
用户注册

分享至

在 Java 中使用对或 2 元组

  • 作者: 奸情哥哥
  • 来源: 51数据库
  • 2022-12-16

问题描述

Java 中的我的 Hashtable 将受益于具有元组结构的值.我可以在 Java 中使用什么数据结构来做到这一点?

My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that?

Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...

推荐答案

我认为 Java 中没有通用的元组类,但自定义一个可能像下面这样简单:

I don't think there is a general purpose tuple class in Java but a custom one might be as easy as the following:

public class Tuple<X, Y> { 
  public final X x; 
  public final Y y; 
  public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
  } 
} 

当然,如何进一步设计这个类有一些重要的含义,涉及平等、不变性等,特别是如果您计划使用实例作为散列键.

Of course, there are some important implications of how to design this class further regarding equality, immutability, etc., especially if you plan to use instances as keys for hashing.

软件
前端设计
程序设计
Java相关