繁体   English   中英

如何在 Java 中返回填充的可增长 Hashmap?

[英]How to return a populated growable Hashmap in Java?

这是我的 Java 代码:

package utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import static com.app.core.AcType.*;
import static java.time.LocalDate.parse;


import com.app.core.BankAccount;

public interface CollectionUtils {
//add a static method to return fixed size populated list of bank accounts
    static List<BankAccount> populateAcctList() {
        return Arrays.asList(new BankAccount(10, "abc1", SAVING, 10000, parse("2020-01-19")),
                new BankAccount(100, "abc4", FD, 200000, parse("2019-01-19")),
                new BankAccount(34, "abc3", CURRENT, 5000, parse("2010-11-29")),
                new BankAccount(15, "abc6", SAVING, 12000, parse("2011-06-23")),
                new BankAccount(64, "abc8", SAVING, 9000, parse("2020-01-19")),
                new BankAccount(49, "abc2", DMAT, 13000, parse("2020-01-19"))
                );
    }
    //add a method to ret a populated growable hashmap from above sample data ??????
    
    static HashMap<String, BankAccount> populateAccountHashMap(){
        return 
    }
}

我添加了一个static方法来返回一个固定大小的银行账户填充列表,现在,我想添加一个static方法来从相同的数据返回一个填充的可增长 Z063A5BC470661C3C7909BCE1B7E971AZ? 我不知道该怎么做?

假设您有一个List<BankAccount>命名的accounts ,您可以这样做。

Map<String, BankAccount> map = 
                    accounts.stream()
                    .collect(Collectors.toMap(BankAccount::getAccountName, acct->acct));

这假定名称是您的银行帐户实例中的第二个元素(或其他一些字符串)并且具有显示的 getter。 如果您希望键是其他东西,请相应地进行调整。 请注意,此特定解决方案假定键和 BankAccounts 是一对一的映射。 如果您有重复的键(即在这种情况下,每个名称有多个帐户),它将引发异常。 然后您可以这样做,其中每个名称将引用一个喜欢的命名帐户列表。

Map<String,List<BankAccount>> map =  accounts.stream()
                    .collect(Collectors.groupingBy(BankAccount::getAccountName)); 

我有我的答案!

//add a method to ret a populated growable hashmap from above sample data ??????
    
    static HashMap<Integer, BankAccount> populateAccountHashMap(){
        HashMap<Integer, BankAccount> temp = new HashMap<Integer, BankAccount>();
        temp.put(10, new BankAccount(10, "abc1", SAVING, 10000, parse("2020-01-19")));
        temp.put(100, new BankAccount(100, "abc4", FD, 200000, parse("2019-01-19")));
        temp.put(34, new BankAccount(34, "abc3", CURRENT, 5000, parse("2010-11-29")));
        temp.put(15, new BankAccount(15, "abc6", SAVING, 12000, parse("2011-06-23")));
        temp.put(64, new BankAccount(64, "abc8", SAVING, 9000, parse("2020-01-19")));
        temp.put(49, new BankAccount(49, "abc2", DMAT, 13000, parse("2020-01-19")));
        return temp;
    }

已验证!

当您使用 Java 9+ 时, Map.of()很有用。

编辑

我修正了我的例子。 请注意, Map.of()返回一个不可变的 map,因此您需要包装它以使其可增长。

  Map<String, BankAccount> map = new HashMap<>(Map.of(
    "abc1", new BankAccount(10, "abc1", SAVING, 10000, parse("2020-01-19")),
    "abc4", new BankAccount(100, "abc4", FD, 200000, parse("2019-01-19"))
  ));

暂无
暂无

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

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