简体   繁体   English

Java HashMap作为方法参数-未指定“值”类型

[英]Java HashMap as method parameter - not specifying the “value” type

This is a question about Java syntax. 这是关于Java语法的问题。 I am not stuck. 我没有被困住。

For lists, let's say I have a List of Strings. 对于列表,假设我有一个字符串列表。

List<String> mylist;

As long as I don't need to access the elements in my list, I can refer to this list as a List (versus List<String> ). 只要不需要访问列表中的元素,就可以将该列表称为List (与List<String> )。
So I can have a method looking like that: 所以我可以有一个像这样的方法:

public int listSize(List list) {
    return list.size();
}

Now let's assume I have a HashMap . 现在假设我有一个HashMap

HashMap<String, String> myhash;

And a method accessing the key but never the value : 还有一种访问但从不访问值的方法

public boolean hashContainsKey(HashMap<String, String> hash, String key) {
    return hash.containsKey(key);
}

Is there a nice way to only specify the "key" part of the HashMap? 有没有一种很好的方法仅指定HashMap的“键”部分?
Something like HashMap<String> ? HashMap<String>

I wish your example is just something to demonstrate your idea instead of something you are going to write, as it is quite meaningless to write method like these. 我希望您的示例只是为了展示您的想法,而不是您将要编写的东西,因为编写这样的方法是毫无意义的。

One way is to use wildcard for the type param: 一种方法是对类型param使用通配符:

your first example should look like this 您的第一个示例应如下所示

// Should be static in this example
public static int listSize(List<?> list) {
    return list.size();
}

and the other one should be: 另一个应该是:

// It is, imho, irrational to deal with HashMap specifically, 
// and again, this should be static
public static boolean mapContainsKey(Map<String, ?> map, String key) {
    return map.containsKey(key);
}

or even better: 甚至更好:

public <T> static boolean mapContainsKey(Map<T, ?> map, T key) {
    return map.containsKey(key);
}

Little update on why use wildcard ( List<?> ) over raw type ( List ) 关于为何对原始类型( List )使用通配符( List<?> )的很少更新

In OP's example, the difference may not be obvious. 在OP的示例中,差异可能并不明显。 However they are not equivalent. 但是,它们并不等效。

For example: 例如:

public void foo(List list1, List list2) {
  list1.addAll(list2);
}

The above is legal, but if you call it with foo(aStringList, anIntegerList); 以上是合法的,但是如果您使用foo(aStringList, anIntegerList);调用foo(aStringList, anIntegerList); , the outcome will be disastrous: the String List will now contains Integer s, everything messed up. ,结果将是灾难性的: String列表现在将包含Integer ,所有内容都混乱了。

But if you write 但是如果你写

public void foo(List<?> list1, List<?> list2) {
  list1.addAll(list2);
}

Although list1 and list2 allows any List<XXX> to pass in, without you explicitly telling the compiler that they are of the same type, compiler will assume the ? 尽管list1和list2允许传入任何List<XXX> ,但无需显式告知编译器它们是同一类型,则编译器将假定? in list1 and the ? 在list1和? in list2 can represent different thing, and hence fail the compilation of list1.addAll(list2); 在list2中可以代表不同的事物,因此list1.addAll(list2);的编译失败list1.addAll(list2); , which avoid you writing stupid things like this. ,这样可以避免您编写诸如此类的愚蠢内容。

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

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