Java相当于Perl的哈希 [英] Java equivalent of Perl's hash

查看:93
本文介绍了Java相当于Perl的哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于超强的灵活性和便利性,我一直在使用很多Perl散列。例如,在
中,在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}

我想知道如何用Java完成同样的事情,我想与HashMap有关系?

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

谢谢,

推荐答案


由于超强的灵活性和方便性,我一直在使用很多Perl散列。例如,在Perl中,我可以执行以下操作:
$ hash {AREA_CODE} - > {PHONE} - > {STREET_ADDR}
I想知道如何用Java完成同样的事情,我想这与HashMap有关系?

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?

>近似以下Perl代码:

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};

HashMap<String, HashMap<String, HashMap<String, String>>> hash =
    new HashMap<String, HashMap<String, HashMap<String, String>>>();

hash.put("AREA_CODE", new HashMap<String, HashMap<String, String>>());
hash.get("AREA_CODE").put("PHONE", new HashMap<String, String>());
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"));

不是那个特殊? :)

我说近似有很多原因。其中之一就是在Java中,你将会感到沮丧,只是为了想在下一行Java上执行这个非常简单的Perl代码:

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;

如果您希望Perl在这样的灵活性和方便性方面,Java根本就不会放弃给你。更糟糕的是,它的游击队员经常会骂你甚至表达这样的欲望。

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.

这篇关于Java相当于Perl的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆