perl和java高手,Java相当于Perl的哈希

I've been using a lot Perl hashes due to super flexibility and convenient.

for instance, in Perl I can do the following:

$hash{AREA_CODE}->{PHONE}->{STREET_ADDR}

I wondering how can I accomplish the same thing with Java, I guess it has something to do with HashMap?

Thanks,

解决方案

I've been using a lot Perl hashes due to super flexibility and convenient. for instance, in Perl I can do the following:

$hash{AREA_CODE}->{PHONE}->{STREET_ADDR}

I wondering how can I accomplish the same thing with Java, I guess it has something to do with HashMap?

The Java code which approximates the following Perl code:

my %hash;

$hash{AREA_CODE}{PHONE}{STREET_ADDR} = "221B Baker Street";

printf "Street address is %s\n", $hash{AREA_CODE}{PHONE}{STREET_ADDR};

is

HashMap>> hash =

new HashMap>>();

hash.put("AREA_CODE", new HashMap>());

hash.get("AREA_CODE").put("PHONE", new HashMap());

hash.get("AREA_CODE").get("PHONE").put("STREET_ADDR", "221B Baker Street");

System.out.printf("Street address is %s\n",

hash.get("AREA_CODE").get("PHONE").get("STREET_ADDR"));

Isn’t that special? :)

I say ‘approximates’ for many reasons. One of these is that in Java you’ll be frustrated to the point of extreme apoplexy merely for wanting to then do on the next line of Java the equivalent of this perfectly straightforward Perl code:

$hash{AREA_CODE}{PREFIX} = 800;

If you want Perl’s flexibility and convenience in things like this, Java simply isn’t going to give it to you. Even worse, its partisans will often berate you for even expressing such a desire.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值