Tag: generics

generics无法实例化`new HashMap `。 为什么?

为什么以下代码行在HashMap中出错? 它并没有在任务的右侧使用外卡。 Map map=new HashMap();

java中的通用访客模式

以下java实现的访问者模式使用generics,一般是否有用? (我想是的)。 它能以某种方式得到改善吗? 使用匿名类轻松调用很重要。 谢谢。 (使用示例): Vector numbers = new Vector(); numbers.add(new Double(1.2)); numbers.add(new Float(-1.2)); numbers.add(new Double(4.8)); numbers.add(new Float(-3.4)); numbers.add(new Long(123456)); numbers.add(new Short(“14”)); For.each(numbers, new Visitor() { public void doIt(Double n) { System.out.println(“doIt() for double: ” + n); } public void doIt(Float n) { System.out.println(“doIt() for float: ” + n); } public void doIt(Number n) […]

Spring + Mongo + Generics +灵活性

以下代码(当然)不起作用,因为标记的行不能编译: MyClass { //singleton stuff private static MyClass instance; private MyClass () {} public static MyClass getInstance() { if(instance==null) { instance = new MyClass (); } return instance; } // method creating problems public NonGenericSuperClassOfGenericClass create(Class… classes) { if(someCondition) return new GenericClass; // DOES NOT COMPILE else return new OtherGenericClass; } } 因此,我实际上不知道“创造”是否会回归 GenericClass 要么 […]

Java通配符和generics? 超级T和? 延伸T.

当处理通配符,如设置/添加通用项目到某个容器时,是否建议使用这样的东西? void add(List someList,someitem){ someList.add(someItem); } 当检索项目时,建议使用这样的东西 void f1(List obj, T item) { obj.add(item); } 这背后的原理是什么? 我什么时候才能知道我是否应该使用它?

Object的通用下限

可以将通用参数编码为: public void someMethod(T t); 是否有这种约束的有效用法?

如何通过检查找到返回类型的参数化类型?

我正在使用reflection来获取所有类中的所有方法: Method[] allMethods = c.getDeclaredMethods(); 之后我正在迭代这些方法 for (Method m: allMethods){ //I want to find out if the return is is a parameterized type or not m.getReturnType(); } 例如:如果我有这样的方法: public Set getCats(); 如何使用reflection找出包含Cat作为参数化类型的返回类型?

两个类之间的递归javagenerics

这个问题类似于两个类之间可能递归的Javagenerics中提出的问题。 但是,到目前为止,在其他线程中,一切正常。 在我的情况下,我不幸地需要使用递归方法的其他类。 更具体一点:我想使用边和节点定义图类。 然后,我想从图表类派生一个街道图,它使用道路和交汇点作为边和节点。 另外,我想使用铁路和停靠点作为边缘和节点来推导公共交通图。 最后,我想定义一个包含街道图或公共交通图的变量。 最后一步是迄今为止我无法实现的目标。 语言如此之多; 现在让我们来看看Java代码: public interface Node<NT extends Node, ET extends Edge> { /* internal datastructures here */ } public interface Edge<NT extends Node, ET extends Edge> { /* internal datastructures here */ } public interface Graph<NT extends Node, ET extends Edge> { /* internal datastructures here */ } […]

为什么java编译器不报告Intellij中多播表达式的未经检查的强制转换警告?

为什么下面的代码没有报告Intellij IDEA与jdk 1.8.0_121未经检查的警告,因为Supplier & Serializable是T的超类型? <T extends Supplier & Serializable> T createdBy(AtomicInteger counter) { // v— if I removed the first cast expression, I can’t compile it return (T) (Supplier & Serializable) counter::incrementAndGet; // ^— it should be reports unchecked warnings, but it doesn’t } 并且以下代码报告了未经检查的强制转换警告: R apply(T value) { return (R) value; // ^— […]

在Java中使用Type Erasure

因此,我使用的组减少了我们为某些事情键入的代码量。 在这种情况下,Spring网页使用DisplayTag库显示列表。 它的完成方式是使用generics扩展Controller对象的类,然后是它应该处理的每个页面的子类。 此控制器显示SystemErrorReport ,并将类型定义为SystemErrorReportCommand 。 public class SystemErrorReportController extends GenericSearchAndSelectController { 问题是,作为一个类型传递的SystemErrorReportCommand需要在其构造函数中有一个自己的手动声明,如下所示: public SystemErrorReportCommand() { super(SystemErrorReport.class); } 稍后将命令对象传递给需要知道其显式类型的东西。 如果没有在某处手动指定它,它会返回GenericCommand ,这是我们的另一个类,因为SystemErrorReportCommand位在编译时丢失。 我对此并不感到兴奋,因为我们可以进一步自动化以减少开发人员的错误。 有没有更优雅的方法来做到这一点,还是我因为类型擦除而坚持这个?

使用enum参数自动推断返回类型

如果我将Enum作为参数提供给Java中的方法,是否可以自动推断返回类型? 我希望能够通过使getValue通用并使用Enum中的信息来支持这些调用 String myval = obj.getValue(MyEnum.Field1) int myval2 = obj.getValue(MyEnum.Field2) 当前解决方案 我目前的解决方案是这样的: String val = typeRow.getColumnValueGen(TYPEGODKENDCRM.COLUMN_TYPEGODKENDNR, String.class) 它用于访问表的ORM表示中的列。 我知道这个字段是一个字符串,我想避免在每次调用时告诉它。