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

发布于 2024-10-08 01:41:46 字数 1086 浏览 1 评论 0原文

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

看起来很无辜,但 Eclipse 却在抱怨分号。

Eclipse 错误:标记“;”上的语法错误,无效的AssignmentOperator

谢谢

整个类:

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

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

Seems so innocent yet Eclipse is complaining about the semi-colon, of all things.

Eclipse error: Syntax error on token ";", invalid 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);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-10-15 01:41:46

线路

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

没到位。您应该将其放在构造函数中:

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

另请注意,我删除了构造函数右大括号后的无关分号。我也不确定 get 方法中发生了什么。您声明了对象mappers,但从未初始化它。这肯定会导致错误。

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. You declare the object mappers but never initialize it. That will certainly cause an error.

反差帅 2024-10-15 01:41:46

不确定 IMapper 是什么,我假设是某个接口,但我只是将其放入 Eclipse 中,它不会给我任何错误:

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

也许您可以发布更多代码?

Not sure what IMapper is, some interface I assume, but I just dropped this into Eclipse and it doesn't give me any errors:

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

Perhaps you could post more of your code?

╭⌒浅淡时光〆 2024-10-15 01:41:46

这段代码完美运行:),受到 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);
    }

}

This code works perfectly :), inspired by help from 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);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文