草庐IT

generic-type-argument

全部标签

Java 泛型 : chaining together generic function object

我一直在努力解决以下问题。我有一系列函数对象,每个函数对象都有自己的输入和输出类型,这些类型是通过java中的泛型类型参数定义的。我想将它们排列成一个链,以便将原始数据输入到第一个函数,转换为输出类型,这是下一个对象的输入类型,依此类推。当然,这对于硬编码来说是微不足道的,但我希望代码可以插入到新的函数对象中。如果我只是省略了类型参数(只有最终输出类型),这就是事情的样子:publicTprocess(){Iteratorit=source.provideData();for(Pipepipe:pipeline){it=pipe.processIterator(it);}returns

java - Play Framework : Redirect to controller method with arguments

我正在使用PLAY框架2.2.1构建一个Web应用程序,并试图在地址栏中显示所请求站点的所有可用httpget查询参数,即使是那些未在请求中设置的参数。在这种情况下并非所有httpget参数都已设置,我想添加具有默认值的未设置参数并进行重定向。我有一个可以使用GET请求的站点:GET/testcontrollers.Application.test(q:String,w:String?=null,f:String?=null,o:String?=null)这是我希望在controllers.Application中使用的方法:publicstaticResulttest(Stringq

java - 好的设计 : How to pass InputStreams as argument?

我有一个大文件,我正在打开一个FileInputStream。该文件包含一些文件,每个文件都具有相对于开头的偏移量和大小。此外,我有一个解析器应该评估这样一个包含的文件。Filefile=...;//thebigfilelongoffset=1734;//acontainedfile'soffsetlongsize=256;//acontainedfile'ssizeFileInputStreamfis=newFileInputStream(file);fis.skip(offset);parse(fis,size);publicvoidparse(InputStreamis,long

java - 获取 Class<T> 的实例 [Runtime-Type Token]

我创建了一个Preferences类,并且我不想为Getters使用运行时类型token。所以这是我的getter方法:publicTget(Stringkey,Classclazz){//dosomecrazystuff(e.g.DoubleFloat)}到目前为止,一切正常。但我希望类参数是可选的。booleanb=preferences.get(key);所以我添加了一个额外的方法:publicTget(Stringkey){//returnget(key,Class);}现在问题:有没有办法做到这一点?有没有办法获取Class的实例?.可以通过一个小的解决方法:publicTg

java - 需要不兼容的类型 : Class<T> found: Class<CAP#1> where T is a type-variable

以下代码:publicclassA{Classklass;Tinstance;publicA(Tinstance){this.klass=instance.getClass();//thisrequiresanexplicitcasttoClasstosatisfythecompilerthis.instance=instance;}}编译时给出:A.java:7:error:incompatibletypesthis.klass=instance.getClass();^required:Classfound:ClasswhereTisatype-variable:TextendsO

java - Spring 启动 CommandLineRunner : filter option argument

考虑到SpringBootCommandLineRunner应用程序,我想知道如何过滤作为外部化配置传递给SpringBoot的“开关”选项。例如:@ComponentpublicclassFileProcessingCommandLineimplementsCommandLineRunner{@Overridepublicvoidrun(String...strings)throwsException{for(Stringfilename:strings){Filefile=newFile(filename);service.doSomething(file);}}}我可以调用jav

java - 处理/java : cannot invoke length() on the array type boolean[]

这个问题在这里已经有了答案:lengthandlength()inJava(8个答案)关闭7年前。很惊讶这还没有发布。我正在制作一个for循环,它以前工作过,但由于某种原因我找不到boolean数组的长度。for(intz=0;z

java - Eclipse 3.5.1 编译器错误 :The type OutputFormat is not accessible due to restriction on required library . ./rt.jar

这个错误很奇怪,我无法理解它。我已经安装了EclipseRCP3.5.1,JavaSE1.6更新16并切换到SWT3.5。我创建了一个新项目,设置了依赖项并尝试编译。尝试导入时,请使用以下内容:importcom.sun.org.apache.xml.internal.serialize.OutputFormat;importcom.sun.org.apache.xml.internal.serialize.XMLSerializer;我得到错误:Accessrestriction:ThetypeXMLSerializerisnotaccessibleduetorestrictiono

java - Java 中的通配符 Generic 和 <? super T> 意思,下界或上界

所以我在阅读泛型方法时感到很困惑。先说一下这里的问题:在这个例子中:假设我需要一个适用于任何类型T的selectionSort版本,方法是使用调用者提供的外部可比较对象。第一次尝试:publicstaticvoidselectionSort(T[]arr,ComparatormyComparator){....}假设我有:定义车辆类别创建了实现Comparator的VehicleComparator,同时按价格比较车辆。创建卡车扩展车辆实例化卡车[]arr;车辆比较器myComparator现在,我做:selectionSort(arr,myComparator);它不会工作,因为my

java - 我可以将 Eclipse 设置为忽略 "Unhandled exception type"

是否可以让Eclipse忽略错误“未处理的异常类型”?在我的具体情况下,原因是我已经检查过文件是否存在。因此,我认为没有理由放入trycatch语句。file=newFile(filePath);if(file.exists()){FileInputStreamfileStream=openFileInput(filePath);if(fileStream!=null){还是我遗漏了什么? 最佳答案 IsitpossibletogetEclipsetoignoretheerror"UnhandledexceptiontypeFile