【问题标题】:java.util.TreeMap$Entry.entrySet() is applicable for argument types: () values: []java.util.TreeMap$Entry.entrySet() 适用于参数类型:() 值:[]
【发布时间】:2018-05-11 00:57:11
【问题描述】:

我有ChatDayWrappers,其中包含一天内双方数据库中的所有消息。

如何从对象chatWrapper.getChatDayWrappers()[0]; 中获取包含 47 个项目的 chatRowWrappers?

代码

Object object = chatWrapper.getChatDayWrappers()[0];            
getLogger().info("chatWrapper:  " + object.getClass().getName());

输出

java.util.TreeMap$Entry

再试一次:

我已经厌倦了以下内容:

Object object[] = chatWrapper.getChatDayWrappers()[0].entrySet().toArray();             
getLogger().info("chatWrapper:  " + object.getClass().getName());

但我收到了这个错误:

方法没有签名:java.util.TreeMap$Entry.entrySet() 适用于参数类型:() 值:[] 可能的解决方案:every(): groovy.lang.MissingMethodException: No signature of method: java.util.TreeMap$Entry.entrySet() is applicable for argument types: () values: []

CockpitChatWrapper

public class CockpitChatWrapper
        implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String STYLE_CHAT_UNKNOWN = "tpChatUnknown";
    public static final String STYLE_CHAT_FROM = "tpChatFrom";
    public static final String STYLE_CHAT_TO = "tpChatTo";

    /** Used to format the dates in the report. */
    private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {

        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    private final Map<String, ChatDisplayDayWrapper> chatDayWrappers;

    private final Long currentUserId;
    private final Long currentFlexiObjectId;


    /**
     * Constructor.
     * 
     * @param currentUserId The user Id.
     * @param currentFlexiObjectId The flexiObejct Id.
     */
    public CockpitChatWrapper(final Long currentUserId, final Long currentFlexiObjectId) {
        this.chatDayWrappers = new TreeMap<String, ChatDisplayDayWrapper>(new Comparator<String>() {

            public int compare(final String o1, final String o2) {
                return (o1.compareTo(o2) * -1);
            }

        });
        this.currentUserId = currentUserId;
        this.currentFlexiObjectId = currentFlexiObjectId;
    }


    /**
     * 
     * Add a chat entry.
     * 
     * @param chat The chat entry.
     */
    public void addChatEntry(final Chat chat) {

        String key = CockpitChatWrapper.DATE_FORMAT.get().format(chat.getLastWrite());
        ChatDisplayDayWrapper entry = null;
        if (this.chatDayWrappers.get(key) != null) {
            entry = this.chatDayWrappers.get(key);
        } else {
            entry = new ChatDisplayDayWrapper(chat.getLastWrite());
            this.chatDayWrappers.put(key, entry);
        }

        entry.addChat(chat, this.currentUserId, this.currentFlexiObjectId);
    }


    /**
     * Returns the chatDayWrappers.
     * 
     * @return Returns the chatDayWrappers.
     */
    public Object[] getChatDayWrappers() {
        return this.chatDayWrappers.entrySet().toArray();
    }


    public boolean getHasChatDayWrappers() {
        return this.chatDayWrappers != null && this.chatDayWrappers.size() > 0;
    }

    /**
     * 
     * Hold the chat entries for an day.
     * 
     */
    public static class ChatDisplayDayWrapper
            implements Serializable {

        private static final long serialVersionUID = 1L;

        private final Date day;
        private final List<ChatDisplayRowWrapper> chatRowWrappers;


        /**
         * 
         * Constructor.
         * 
         * @param day The day to set.
         */
        public ChatDisplayDayWrapper(final Date day) {
            this.day = day;
            this.chatRowWrappers = new ArrayList<ChatDisplayRowWrapper>();
        }


        private void addChat(final Chat chat, final Long currentUserId, final Long currentFlexiObjectId) {

            String styleClass = CockpitChatWrapper.STYLE_CHAT_UNKNOWN;

            if (currentUserId != null && chat.getFromUser() != null
                    && currentUserId.longValue() == chat.getFromUser().getId().longValue()) {
                styleClass = CockpitChatWrapper.STYLE_CHAT_FROM;
            } else if (currentFlexiObjectId != null && chat.getFromFlexiObject() != null
                    && currentFlexiObjectId.longValue() == chat.getFromFlexiObject().getId().longValue()) {
                styleClass = CockpitChatWrapper.STYLE_CHAT_TO;
            }
            this.chatRowWrappers.add(new ChatDisplayRowWrapper(chat, styleClass));
        }


        /**
         * Returns the day.
         * 
         * @return Returns the day.
         */
        public Date getDay() {
            return this.day;
        }


        /**
         * Returns the chatRowWrappers.
         * 
         * @return Returns the chatRowWrappers.
         */
        public List<ChatDisplayRowWrapper> getChatRowWrappers() {
            return this.chatRowWrappers;
        }

    }

}

截图

【问题讨论】:

  • 能否也添加chatWrapper对象类的代码?
  • @Maddy:我添加了private CockpitChatWrapper chatWrapper;的类
  • 如果您声明一个正确的返回类型而不是 Object[],您的 IDE 会有所帮助。

标签: java groovy


【解决方案1】:

你的函数Object[] getChatDayWrappers

this.chatDayWrappers.entrySet().toArray()

返回Map$Entry 的数组。例如:

groovy:000> [1:1].entrySet().toArray().first().getClass()
===> class java.util.LinkedHashMap$Entry

Entry不是地图,它只是一个条目。一个条目包含keyvalue(但不包含entrySet();这是您的代码失败的地方)。例如

groovy:000> [1:1].entrySet().toArray().first().key
===> 1
groovy:000> [1:1].entrySet().toArray().first().value
===> 1
groovy:000> [1:1].entrySet().toArray().first().entrySet()
ERROR groovy.lang.MissingMethodException:
No signature of method: java.util.LinkedHashMap$Entry.entrySet() is applicable for argument types: () values: []
Possible solutions: every()

因此,要获得显示包装器,您需要在 Groovy 中调用 value,或者因为您的代码看起来更像 Java,无论如何 .getValue() 以获取 ChatDisplayDayWrapper

【讨论】:

    【解决方案2】:

    这里的问题是:

    public Object[] getChatDayWrappers() {
            return this.chatDayWrappers.entrySet().toArray();
        } 
    

    返回一个对象数组,您不能直接访问地图内的 ChatDisplayDayWrapper 对象。相反,您可以将 getter 修改为:

    public List<ChatDisplayRowWrapper> getChatDayWrappers() {
        return m.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList());        }
    }
    

    【讨论】:

    • 我已经尝试过了,但我得到'没有方法签名:de.tp.em.frontend.jsf.cockpit.chat.CockpitChatWrapper.getChatDayWrappers() 适用于参数类型:(java.lang .String) 值:[2017-11-27]'
    • Map中key的数据类型是什么
    • 我已经尝试了你的最后一个代码,我得到了`没有方法签名:[Ljava.lang.Object;.get() 适用于参数类型:(java.lang.String) 值:[ 2017-11-27] 可能的解决方案:getAt(java.lang.String)、grep()、grep()、getAt(groovy.lang.IntRange)、getAt(java.util.Collection)、getAt(groovy.lang.范围)`
    • 您确定用于创建密钥的格式化字符串与用于获取值的格式化字符串相同吗?
    • 我可以这样访问它:chatWrapper.getChatDayWrappers()[0].value.getChatRowWrappers()
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多