简体   繁体   English

有关使用HashMap的方式的设计实现

[英]Design implementation about the way of using HashMap

When showing accounts in the GUI, I need to show primary accounts ( for secondary accounts ) There can be multiple secondary accounts for each primary account. 在GUI中显示帐户时,我需要显示主要帐户(对于次要帐户)每个主要帐户可以有多个次要帐户。

I am trying to save the primary to secondary accounts information in the HashMap. 我正在尝试在HashMap中保存主要帐户到次要帐户的信息。 Because, that needs to be retrieved later. 因为,这需要稍后进行检索。

While saving, I also need to save secondary account instruction. 保存时,我还需要保存辅助帐户说明。 So, I need to save two objects with key as Primary account. 因此,我需要将两个具有键的对象保存为主帐户。

1) Secondary Account
2) Secondary Instruction.

I have equals and hashcode overridden for account and instruction objects. 我为帐户和指令对象设置了等于和哈希码。

I am trying to use Primary account hashcode as key and value as a List of Object[2] 我正在尝试使用主帐户哈希码作为键和值作为对象列表[2]

-- Initialization -初始化

private static final Map<Integer, ArrayList<Object[]>> primaryToSecondaryAcct = new ConcurrentHashMap<Integer, ArrayList<Object[]>>();

-- Putting values -投入价值

final Object[] acctInstr = new Object[2];
acctInstr[0] = acct;
acctInstr[1] = instr;
if(primaryToSecondaryAcct.get(getExistingAccount().hashCode()) != null) {
    primaryToSecondaryAcct.get(getExistingAccount().hashCode()).add(acctInstr);
} else {
    final ArrayList<Object[]> acctInstrList = new ArrayList<Object[]>();
    acctInstrList.add(acctInstr);
    primaryToSecondaryAcct.put(getExistingAccount().hashCode(), acctInstrList);
}

I am wondering if this is correct and if there a better way of doing it. 我想知道这是否正确以及是否有更好的方法。 Could you suggest? 你能建议吗?

Instead of: 代替:

Map<Integer, ArrayList<Object[]>>

why not have 为什么没有

Map<Account, SecondaryInfo>

At the moment you're storing a collection against the key, and you have to manage that, iterate through it etc. when you pull it from the Map . 目前,您正在根据密钥存储一个集合,并且必须对其进行管理,在从Map拉出它时对其进行迭代等。 I think it's much better to create a suitable abstraction and delegate to it. 我认为最好创建一个合适的抽象并将其委托给它。 That abstraction will look after validation, iteration etc. in one location, rather than you having to worry about it each time you access the Map . 该抽象将在一个位置进行验证,迭代等,而不必每次访问Map时都需要担心。

Remember - OO is about telling objects to do things for you , not asking them for info and doing it yourself. 记住-OO是告诉对象为您做事情 ,而不是要求他们提供信息并自己做。

I would replace your Integer representation of an account by a specific Account object. 我将用特定的Account对象替换您的帐户的Integer表示形式。 otherwise you're going to have to manage lots of integers representing different types, and it's very easy to mix these up. 否则,您将不得不管理代表不同类型的许多整数,并且将它们混合起来非常容易。 Typing them (albeit using a trivial class) means you can refactor trivially using automated tools and easily determine types without resorting to naming conventions. 键入它们(尽管使用琐碎的类)意味着您可以使用自动化工具琐碎地重构并轻松确定类型,而无需诉诸命名约定。

我建议使用Account作为密钥,因为两个不同对象的哈希码可以相同。

The first thing you want is a MultiMap , which you can find in the Guava library from Google. 您需要的第一件事是MultiMap ,您可以从Google的Guava库中找到它。 This is similar to a Map<K, Collection<V>> , and maps a key to multiple values for you so you don't have to reinvent that. 这类似于Map<K, Collection<V>> ,并且为您将一个键映射到多个值,因此您无需重新设计。

Next, replace Object[] with your own custom class: 接下来,用您自己的自定义类替换Object[]

public class SecondaryInformation {
    private SecondaryAccount secondaryAccount;
    private SecondaryInstruction secondaryInstruction;

    // Constructors, getters, setters, etc.
}

So you'll have a MultiMap<Integer, SecondaryInformation> . 因此,您将拥有一个MultiMap<Integer, SecondaryInformation> Here's some wiki info on the MultiMap . 这是MultiMap上的一些Wiki信息

If for both Primary and Secondary accounts you have classes as follows: 如果对于Primary帐户和Secondary帐户,您都具有以下类别:

public class PrimaryAccount 
{
    int id;
    private List<SecondaryAccount> secondaryAccounts;   
}

public class SecondaryAccount
{
    int id;
    private List<String> instructions;
    PrimaryAccount primaryAccount;
}

Then, perhaps you will not even need HashMap . 然后,也许您甚至不需要HashMap However, you may still want to maintain a Hashmap for quick look up of Accounts: HashMap<Integer,PrimaryAccoount> , where you will store account id and primary account. 但是,您可能仍需要维护一个Hashmap以便快速查找帐户: HashMap<Integer,PrimaryAccoount> ,您将在其中存储帐户ID和主要帐户。

This makes the implementation cleaner. 这使实现更清洁。 How ever you do need write classes for Primary and Secondary accounts. 您实际上需要如何为PrimarySecondary帐户编写类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM