Java 无法实例化类型集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18946657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:48:56  来源:igfitidea点击:

Cannot instantiate the type Set

javasethashtable

提问by John Tate

I am trying to create a Set of Strings which is filled with the keys from a Hashtable so a for-each loop can iterate through the Set and put defaults in a Hashtable. I am still learning Java but the way I am trying to do it isn't valid syntax. Could someone please demonstrate the proper way of doing this and explain why my way doesn't work and theirs does.

我正在尝试创建一组字符串,其中填充了来自 Hashtable 的键,以便 for-each 循环可以遍历 Set 并将默认值放入 Hashtable 中。我仍在学习 Java,但我尝试这样做的方式不是有效的语法。有人可以演示这样做的正确方法并解释为什么我的方法不起作用而他们的方法起作用。

private Hashtable<String, String> defaultConfig() {
    Hashtable<String, String> tbl = new Hashtable<String, String>();
    tbl.put("nginx-servers","/etc/nginx/servers");
    tbl.put("fpm-servers","/etc/fpm/");
    tbl.put("fpm-portavail","9001");
    tbl.put("webalizer-script","/usr/local/bin/webalizer.sh");
    tbl.put("sys-useradd","/sbin/useradd");
    tbl.put("sys-nginx","/usr/sbin/nginx");
    tbl.put("sys-fpmrc","/etc/rc.d/php_fpm");
    tbl.put("www-sites","/var/www/sites/");
    tbl.put("www-group","www"); 
    return tbl;
}

//This sets missing configuration options to their defaults.
private void fixMissing(Hashtable<String, String> tbl) {
    Hashtable<String, String> defaults = new Hashtable<String, String>(defaultConfig());
    //The part in error is below...
    Set<String> keys = new Set<String>(defaults.keySet());

    for (String k : keys) {
        if (!tbl.containsKey(k)) {
            tbl.put(k, defaults.get(k));
        }
    }
}

采纳答案by SegFault

Setis not a class, it is an interface.

Set不是一个类,它是一个接口。

So basically you can instantiate only class implementing Set(HashSet, LinkedHashSetorTreeSet)

所以基本上你只能实例化实现Set( HashSet, LinkedHashSetor TreeSet) 的类

For instance :

例如 :

Set<String> mySet = new HashSet<String>();

回答by Aaron

Set is an interface. You cannot instantiate an interface, only classes which implement that interface.

Set 是一个接口。您不能实例化接口,只能实例化实现该接口的类。

The interface specifies behaviour, and that behaviour can be implemented in different ways by different types. If you think about it like that, it makes no sense to instantiate an interface because it's specifying what a thing must do, not how it does it.

接口指定行为,并且该行为可以由不同的类型以不同的方式实现。如果你这样想,实例化一个接口是没有意义的,因为它指定了一个东西必须做什么,而不是它如何做。

回答by Mike Housky

HashMap's keySet()method already creates the set you need, so simply:

HashMap 的keySet()方法已经创建了你需要的集合,所以很简单:

Set<String> keys = defaults.keySet();

This is a viewof the keys in defaults, so its contents will change when changes are made to the underlying (defaults) map. Changes made to keyswill be reflected in the map, as well, but you can only remove...not add...keys from the map.

这是默认值中的键的视图,因此当对底层 ( defaults) 映射进行更改时,其内容将更改。对 所做的更改keys也将反映在地图中,但您只能从地图中删除...而不是添加...键。

If you need a copy of the keys that doesn't interact with the original map, then use one of the types suggested, as in:

如果您需要不与原始地图交互的键的副本,请使用建议的类型之一,如下所示:

Set<String> keys = new HashSet( defaults.keySet() );