【学习Java泛型(记得通配符)】教程文章相关的互联网学习教程文章

java – 使用带有通配符支持的SimpleDateFormat解析日期字符串(例如* yyyy * MM * dd * hh * mm * ss)【代码】

首先,我想知道是否存在类似于SimpleDateFormat但是支持通配符的现有库?如果没有,最好的办法是什么? 我有这个问题,我需要匹配并从文件名中提取日期,但我似乎无法找到适合这种情况的方法.虽然我承认下面的场景对于文件名来说根本不实用,但我必须将其作为“WHAT IF”包含在内. 脚本 文件名:19882012ABCseptemberDEF03HIJ12KLM0156_249.zip,模式:yyyyMMMddhhmmss’_.zip’ >预计日期:2012年9月3日上午12:01:56>细分版本:1988-201...

java – Spring metro integration cvc-complex-type.2.4.c:匹配的通配符是strict,但是找不到元素’wss:binding’的声明【代码】

我尝试将地铁网络服务与春天融为一体.但是我在applicationContext.xml中遇到错误<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www...

返回类型为Java Generic方法的上限和下限通配符【代码】

我试图解决一个我无法理解部分答案的问题. 以下是BackLister类:public class BackLister {// INSERT HERE{List<T> output = new LinkedList<T>();for (T t : input)output.add(0, t);return output;} }这个问题询问哪些可以插入//在这里插入BackLister类来编译和运行而没有错误? 以下是选项:A. public static <T> List<T> backwards(List<T> input) B. public static <T> List<T> backwards(List<? extends T> input) C. public...

java – 下限通配符(Comparable <?super K>)【代码】

在集合中,经常使用Comparable接口,例如.在PriorityQueue中:private static <T> void siftUpComparable(int k, T x, Object[] es) {Comparable<? super T> key = (Comparable<? super T>) x;...if (key.compareTo((T) e) >= 0)break;... }比如说,我们创建整数队列并添加一些内容:PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.add(1);如果我得到通配符的概念,使用<?的唯一效果超级T>而不是< T>是编译器扩展compa...

java – 在Gradle设置文件中包含使用通配符的子项目【代码】

在Gradle中,您需要定义要在“settings.gradle”文件中构建的子项目.要构建三个子项目,您可以执行以下操作:include "child1", "child2", "child3"我遇到的问题是我有很多项目要包括在内.有没有办法在这个定义中使用通配符?我在寻找这样的东西:include "*"那当然不行.这将更容易使用,因为我有许多子项目要包括.有没有办法自动将子目录包含为项目?解决方法:你可以这样做:include (1..10).collect { "Child$it" }要将“Child1”包...

java – 不兼容的通用通配符捕获【代码】

在以下代码段中:package test;import java.util.Collection; import java.util.Iterator; import java.util.Map;public class WildcardsTest<K, V> {private Iterator<Map.Entry<K, ? extends Collection<V>>> iterator;public WildcardsTest(Map<K, ? extends Collection<V>> map) {iterator = map.entrySet().iterator();/* Type mismatch: cannot convert fromIterator<Map.Entry<K,capture#1-of ? extends Collection<V>>> to...

C#相当于Java的通配符【代码】

如果存在,那么以下Java代码的C#等价物是什么:new HashMap<Class<? extends BaseClass>, Integer>();我目前使用新的Dictionary< Type,int>(),这更像是新的HashMap< Class<?>,Integer>(),这显然是不一样的. (忽略HashMap和Dictionary之间的差异) 编辑:为了澄清,我不是要尝试定义一个新类,只需创建一个HashMap / Dictionary实例.解决方法:C#中没有Java通配符的等价物.在Java中,类型的类型是Class< T>.其中T是类本身. C#中的等价物是...

具有有界通配符的Java Generic【代码】

类声明之间有什么区别public abstract class Super1<T extends Super1<T>> {...} 和public abstract class Super2<T extends Super2<? super T>> {...}使用第一种方法,我能够创建一个子类(类似于Java中的枚举类型)public class Sub1 extends Super1<Sub1> {...}但是我想知道是否有可能像第二类这样的超类声明..,它有什么意义,但我无法创建像public class Sub2 extends Super2<Object> {...}我认为Object可以用作T ??的超类型我能想...

java – 使用通配符调用泛型方法无法编译【代码】

有人可以解释为什么以下代码fragmnent不编译?public class Main {public static void main(String[] args) {Integer[] integers = {3, 5, 8};Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(integers));Double[] doubles = {3.5, 5.5, 8.5};Set<Double> s2 = new HashSet<Double>(Arrays.asList(doubles));Set<Number> res1 = union(s1, s1); // ->it does not compile Set<Number> res2 = union(s1, s2); //...

Java Collections通配符【代码】

我试图定义一个List对象,然后使用下面的代码向它添加对象:List<? extends File> newFiles = new ArrayList<? extends File>();当我尝试像这样定义它时,我得到编译器错误Cannot instantiate the type ArrayList<? extends File>而List<? extends File> newFiles = new ArrayList<File>()似乎是有效的.目前尚不清楚为什么. File是我创建的bean对象 – 而不是属于Java API的对象.解决方法:您无法直接实例化通配符类型.在这种情况下,那...

在基于Java的Spring配置中搜索资源时如何使用通配符?【代码】

我正在将XML转换为基于Java的Spring 3配置,并且无法找到一种方法来“翻译”使用通配符作为资源路径的bean:<bean id="messageSource" class="MyResourceBundleMessageSource"><property name="resources" value="classpath*:messages/*.properties" /> </bean>相应的类看起来像:public class MyResourceBundleMessageSource extends org.springframework.context.support.ResourceBundleMessageSource {...public void setResourc...

java – 从通用通配符引用调用方法【代码】

以下代码简化为问题的基本要点:public class GenericTest {private interface CopyInterface<T extends CopyInterface<T>>{public void copyFrom(T source);}public void testMethod(){CopyInterface<?> target;CopyInterface<?> source;target.copyFrom(source);} }这会导致以下编译器错误消息: GenericTest.CopyInterface类型中的copyFrom(capture#1-of?)方法不适用于参数(GenericTest.CopyInterface)GenericTest.java / ACA...

为什么Java编译器在使用带扩展名的通配符时限制向列表添加正确类型的元素【代码】

我理解关于扩展和超级其他帖子的PECS规则,但我对代码下面的限制有点困惑List<Integer> integerList = new ArrayList<>(); integerList.add(1); // integerList.add(3.14); => understandably this will not compile, // as we cannot add double to a list of IntegerList<? extends Number> numberList = integerList; // numberList.add(3); => will not compile! // Why the compiler cannot infer from the above line that ...

java – Stream.reduce的accumulator参数中的通配符的目的是什么?【代码】

一个reduce method的Java流:<U> U reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator<U> combiner);有助于根据流中对象的某些属性来减少流. 但是,我不清楚为什么累加器不能更直接并且用于相同的目的: BiFunction< U,T,U>累加器. 累加器签名中的通配符提供了哪些额外的灵活性(以降低可读性为代价)?解决方法:why the accumulator could not be more straightforward and serving the same purpose:它可能...

Java Generics&Collections,为什么我不能用通配符实例化新的List?【代码】

为什么我有一个方法接收带有通配符的列表:public processGenerics(List<? extends User> users){...}但我无法以类似的方式实例化相同的List?List<? extends User> alist = new ArrayList<? extends User>();[已编辑,不是原始问题的一部分,但相关]为什么我不能以与正常继承相同的方式使用集合进行转换:List<User> users = new ArrayList<Admin>();解决方法:您的问题最令人困惑的方面是您必须使用泛型类型与经典原始类型进行的基础...