简体   繁体   English

调用TreeSet时发生ClassCastException <Long> .contains(Long.valueOf(someLongValue))

[英]ClassCastException when calling TreeSet<Long>.contains( Long.valueOf( someLongValue ) )

Im stumped. 我很沮丧 I declare my set thusly: 我这样宣布我的集合:

    private Set<Long> applicationIds;

Then I populate it like this: 然后我像这样填充它:

public void setApplicationIds( Set<Long> applicationIds ) {
    this.applicationIds = new TreeSet<Long>( applicationIds );
    this.applications = null;
}

Then I attempt to use it: 然后,我尝试使用它:

public List<Application> getApplications() {
    if ( applications == null ) {
        applications = new ArrayList<Application>();
        if ( applicationIds != null ) {
            for ( Application application : availableApplications ) {
                if ( applicationIds.contains( Long.valueOf( application.getId() ) ) ) {
                    applications.add( application );
                }
            }
        }
    }
    return applications;
}

And I end up with this: 我最终得到了:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
    at java.lang.Long.compareTo(Long.java:50)
    at java.util.TreeMap.getEntry(TreeMap.java:346)
    at java.util.TreeMap.containsKey(TreeMap.java:227)
    at java.util.TreeSet.contains(TreeSet.java:234)
    at org.mitre.asias.pf.pnp.viewmodel.Subscription.getApplications(Subscription.java:84)

The line causing the exception (line 84 from the stack trace) is this one: 导致异常的行(堆栈跟踪中的第84行)是这一行:

                if ( applicationIds.contains( Long.valueOf( application.getId() ) ) ) {

Perhaps I am missing something, but if the declaration is Set<Long> and I am calling the contains method passing in a Long.valueOf value, how can I be getting this exception? 也许我错过了一些东西,但是如果声明是Set<Long>并且我正在调用contains一个Long.valueOf值的contains方法,那么我该如何获取此异常?

This is a model bean for a JSF application. 这是用于JSF应用程序的模型bean。 I am using Java 6, Tomcat 6.0.32, mojarra 2.1.14, but none of that should really matter as the Generics are supposed to prevent this kind of problem compile time... 我正在使用Java 6,Tomcat 6.0.32,mojarra 2.1.14,但是这些都不应该很重要,因为泛型应该防止这种问题的编译时间...

-------------- EDIT ----------------- --------------编辑-----------------

Its actually the JSF... I threw together a super simplified example with this setter: 它实际上是JSF ...我用这个设置器组合了一个超级简化的示例:

public void setSelectedValues(Set<Long> selectedValues) {
    this.selectedValues = selectedValues;
    if (logger.isTraceEnabled()) {
        StringBuilder message = new StringBuilder("Selected values:");
        for (Object value : selectedValues) {
            message.append("\n\t'").append(value.getClass().getName())
                    .append("': '").append(value.toString()).append("'");
        }
        logger.trace(message.toString());
    }
    this.selections = null;
}

bound to this component: 绑定到此组件:

<p:selectManyCheckbox id="numbers"
   value="#{controller.selectedValues}" layout="pageDirection">
  <f:selectItems value="#{controller.availableValues}" />
</p:selectManyCheckbox>

which writes this to the log: 将其写入日志:

15:45:16.887 [http-bio-8080-exec-9] TRACE com.pastdev.learn.debug.Controller - Selected values:
    'java.lang.String': '1'
    'java.lang.String': '5'

So, the simple answer is the correct one (thank you @PaulTomblin for emphasizing this). 因此,简单的答案就是正确的答案(感谢@PaulTomblin强调这一点)。 The setter is getting called with a Set that contains String s. 设置器获取调用一个Set包含String秒。 So now, what is the best process for conversion? 那么,现在最好的转换过程是什么? Will I need to iterate through the list casting each value to a Long? 我是否需要遍历整个列表,将每个值都转换为Long?

As a side note, I tested this on Tomcat 7 using Java 7 and the ClassCastException went away, however, the contains method always returns false as should be expected. 附带说明一下,我使用Java 7在Tomcat 7上对此进行了测试,但ClassCastException消失了,但是, contains方法总是返回false ,这与预期的一样。

-------------- EDIT 2 ----------------- --------------编辑2 -----------------

I found an answer with the correct way to bind my component here . 我发现我的组件绑定正确的方式回答这里

-------------- EDIT 3 ----------------- --------------编辑3 -----------------

And here is a better explanation of the problem. 是对该问题的更好解释。

Perhaps I am missing something, but if the declaration is Set and I am calling the contains method passing in a Long.valueOf value, how can I be getting this exception? 也许我缺少了一些东西,但是如果声明是Set,并且我正在调用传递Long.valueOf值的contains方法,那么我该如何获取此异常?

Note that in Java, generic type annotations are only hints for the compiler and have no effect at runtime, so it is possible to violate these constraints at runtime (but there will be a compiler warning somewhere). 请注意,在Java中,泛型类型注释仅是编译器的提示,在运行时不起作用,因此有可能在运行时违反这些约束(但在某处会出现编译器警告)。

Looks like your Set<Long> actually contains at least one String. 看起来您的Set<Long>实际上包含至少一个String。 Where is that Set coming from? 那套东西从哪里来?

as the Generics are supposed to prevent this kind of problem compile time 因为泛型应该避免这种问题的编译时间

Yes, there should be a warning somewhere in your code about missing generic types or unchecked casts. 是的,您的代码中应该有一个警告,指出缺少通用类型或未经检查的强制类型转换。 It is only a warning because generics are optional. 这只是一个警告,因为泛型是可选的。 In the places where you did use them, it would have been an error. 在您确实使用过的地方,那将是一个错误。

OK @Lucas I just gave a try using quick and dirty code using the code you posted as part of the question and it works fine. 好的,@ Lucas我只是尝试使用快速和肮脏的代码,使用您在问题中发布的代码,它工作正常。 May be you need to check your Application class once more. 可能是您需要再次检查Application类。

package javaapplication2;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class JavaApplication2 {


private static Set<Long> applicationIds;

private List<Application> applications;

private static final List<Application> availableApplications = new ArrayList<>();

public static void main(String[] args) {

    JavaApplication2 proj = new JavaApplication2();
    for (int i = 1; i <= 11; i++) {
        Application application = new Application();
        application.setId(i);
        availableApplications.add(application);
    }
    Set<Long> applicationIds1 = new TreeSet<>();
    applicationIds1.add(11L);
    applicationIds1.add(12L);
    applicationIds1.add(13L);

    proj.setApplicationIds(applicationIds1);
    for (Application appl : proj.getApplications()) {
        System.out.println(appl.getId());
    }
}

public void setApplicationIds(Set<Long> applicationIds) {
    this.applicationIds = new TreeSet<Long>(applicationIds);
    this.applications = null;
}

public List<Application> getApplications() {
    if (applications == null) {
        applications = new ArrayList<Application>();
        if (applicationIds != null) {

            for (Application application : availableApplications) {
                if (applicationIds.contains(Long.valueOf(application.getId()))) {
                    applications.add(application);
                }
            }
        }
    }
    return applications;
}

} }

And: 和:

package javaapplication2;

class Application {
private int id;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

}

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

相关问题 Long.valueOf无法编译,无法将int转换为long - Long.valueOf not compiling for converting int to long 为什么在使用Base64加密给java.lang.ClassCastException时使用Long.valueOf(stateMap.get(“ time”)) - Why Long.valueOf(stateMap.get(“time”)) when using Base64 encrytion giving java.lang.ClassCastException Java 性能:关于使用 Long.valueOf(..) - Java Performance: regarding using Long.valueOf(..) Long a = Long.valueOf(1)或Long a = 1L之间有什么区别吗? - Is there any difference between Long a = Long.valueOf(1) or Long a = 1L? 为什么使用Long.valueOf(...)而不是长文字? - Why use Long.valueOf(…) rather than a long literal? 为什么Long.valueOf(0).equals(Integer.valueOf(0))为false? - Why is Long.valueOf(0).equals(Integer.valueOf(0)) false? Java中Long.valueOf(0)和0L的区别是什么? - Which is the difference between Long.valueOf(0) and 0L in Java? 为什么 Double.valueof 可以处理“1D”而 Long.valueof 不能处理“1L”? - why Double.valueof can handle "1D" while Long.valueof cannot handle "1L"? Long.valueOf(java.lang.String)和new Long(java.lang.String)之间的区别? - Difference between Long.valueOf(java.lang.String) and new Long(java.lang.String)? 插入TreeSet时发生ClassCastException - ClassCastException when inserting into a TreeSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM