简体   繁体   English

此Java HashMap声明有什么问题? 简单点

[英]What's wrong with this Java HashMap declaration? Easy points

private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();

Seems so innocent yet Eclipse is complaining about the semi-colon, of all things. 似乎很天真,但是Eclipse抱怨所有事物中的分号。

Eclipse error: Syntax error on token ";", invalid AssignmentOperator Eclipse错误:令牌“;”上的语法错误,无效的AssignmentOperator

Thanks 谢谢

The entire class: 全班:

package persistence;

import java.util.UUID;
import java.util.HashMap;

import domain.Task;

public class PersistanceFacade {

    private static PersistanceFacade uniqueInstance = null;
    private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();


    (SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());

    public PersistanceFacade() {};

    public static synchronized PersistanceFacade getUniqueInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new PersistanceFacade();
            return uniqueInstance;
        }
        else return uniqueInstance;
    }

    @SuppressWarnings("unchecked")
    public Object get(UUID oid, Class type) {
        Object mappers;
        IMapper mapper = ((HashMap<String, IMapper>) mappers).get(type);
    }

}

The line 线

(SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());

is out of place. 不合适。 You should put that in the constructor: 您应该将其放在构造函数中:

public PersistanceFacade() {
  mapper.put("SingleTask", new SingleTaskRDBMapper());
}

Also notice I removed the extraneous semicolon after the closing brace of the constructor. 还要注意,在构造函数的右括号之后,我删除了多余的分号。 I'm also not sure what is going on in the get method. 我也不确定get方法中发生了什么。 You declare the object mappers but never initialize it. 您声明对象mappers但不要初始化它。 That will certainly cause an error. 那肯定会引起错误。

Not sure what IMapper is, some interface I assume, but I just dropped this into Eclipse and it doesn't give me any errors: 我不确定IMapper是什么,我假设有一些接口,但是我只是将它放到了Eclipse中,它没有给我任何错误:

private interface IMapper {}
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();

Perhaps you could post more of your code? 也许您可以发布更多代码?

This code works perfectly :), inspired by help from laz 这段代码完美地工作:),受laz的启发

package persistence;

import java.util.UUID;
import java.util.HashMap;

public class PersistanceFacade {

    private static PersistanceFacade uniqueInstance = null;
    private HashMap<String, IMapper> mappers = new HashMap<String, IMapper>();

    public PersistanceFacade() {
        mappers.put("SingleTask", new SingleTaskRDBMapper());

    }

    public static synchronized PersistanceFacade getUniqueInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new PersistanceFacade();
            return uniqueInstance;
        }
        else return uniqueInstance;
    }

    @SuppressWarnings("unchecked")
    public Object get(UUID oid, Class type) {
        IMapper mapper = (IMapper) mappers.get(type);
        return mapper.get(oid);
    }

}

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

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