【Java泛型通配符 ? 与 T 的区别】教程文章相关的互联网学习教程文章

带通配符的Java通用类型【代码】

我想写一个可以接受参数的方法:Map<String, [the type here could be anything]>因此,这是我编写的代码:/*** Get list value from a map, empty list if there's no key matched */ private List<? extends Object> getListValueFromMap(Map<String, List<? extends Object>> map, String key) {List<? extends Object> list = map.get(key);return list == null ? EMPTY_LIST : list; }EMPTY_LIST定义如下:List<Object> EMPTY_...

从java中的HashMap返回通配符匹配列表【代码】

我有一个Hashmap,可能在String中包含通配符(*). 例如,HashMap<String, Student> students_;可以将约翰*作为一把钥匙.我想知道JohnSmith是否匹配student_中的任何元素.我的字符串可能有几个匹配(John *,Jo * Smith等).有什么方法可以从我的HashMap中获取这些匹配的列表吗? 是否有另一个我可能正在使用的对象,它不需要我遍历我的集合中的每个元素,或者我是否必须将它吸收并使用List对象? 仅供参考,我的收藏品中将包含少于200个元素...

java – 匹配的通配符是strict,但是找不到元素’fss:oauth’的声明【代码】

我正在尝试构建一个Java应用程序,使用Database.com Java SDK访问存储在Database.com中的数据.我已经按照qucik start guide @ http://forcedotcom.github.com/java-sdk/quick-start中给出的步骤进行了操作.当应用程序部署在tomcat服务器上时,我得到了以下异常SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.b...

java-据说通配符可以导入包中的所有类. “它不会导入子程序包,字段或方法.”这是什么意思?【代码】

在Sybex书中,OCA Oracle认证助理Java SE 8程序员I-学习指南,第1章的第10页指出:The * is a wildcard that matches all classes in the package. Everyclass in the java.util package is available to this program when Javacompiles it. It doesn’t import child packages, fields, or methods; itimports only classes. (Okay, it’s only classes for now, but there’s aspecial type of import called the “static import...

java-通配符和泛型错误【代码】

下面的代码是一次性的,使枚举在新的foreach循环中工作是一个失败的想法,但是由于我一直遇到泛型和通配符的问题,因此我希望对其进行编译.无论出于什么原因,我都看不出如何解决它. 那么,需要进行哪些更改才能进行编译?import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;public class Main {private ZipFile...

Java泛型和通配符:如何使此代码编译?【代码】

我正在使用Hamcrest 1.2库编写一些匹配器,但Java通配符却很难.当我尝试编译以下代码时public class GenericsTest {public void doesNotCompile() {Container<String> container = new Container<String>();// this is the desired assertion syntaxassertThat(container, hasSomethingWhich(is("foo")));}// these two are a custom made class and matcher; they can be changedpublic static class Container<T> {public boolean...

java-如何在泛型中为通配符ArrayList添加一个整数元素?【代码】

我有一个ArrayList通用通配符类型,它以Number为扩展名.我正在尝试将整数值添加到ArrayList中. 但是它给我一个错误,说ArrayList<? extends Number> numberList = new ArrayList<Number>();numberList = new ArrayList<Integer>();numberList.add(100);The method add(int, capture#2-of ?) in the type ArrayList<capture#2-of ?> is not applicable for the arguments (int).我也尝试过这种方法,但是给了我同样的错误ArrayList<?> ...

java – 在通配符类型ArrayList中添加元素【代码】

我试图在列表中添加一个元素,其中列表类型参数是扩展Question的通配符ArrayList<? extends Question> id = new ArrayList<? extends Question>();id.add(new Identification("What is my name?","some",Difficulty.EASY));map.put("Personal", id);识别是问题的子类. QUestion是一个抽象类. 它给了我这个错误 在线#1无法实例化类型ArrayList<?扩展问题> 在第2行The method add(capture#2-of ? extends Question) in the type Arr...

java – InetSocketAddress中的通配符地址是什么意思?

在构造函数InetSocketAddress(int port)的文档中,它说:Creates a socket address where the IP address is the wildcard addressand the port number a specified value.通配符地址做什么以及在socket.bind()中使用它意味着什么?解决方法:从文档:通配符是一个特殊的本地IP地址.它通常表示“任何”,只能用于绑定操作. 该IP地址的值为0.0.0.0.如果您有两个网络适配器,一个IP地址为1.1.1.1,另一个IP地址为2.2.2.2,那么您可以创建一...

Java嵌套通配符泛型将无法编译【代码】

我在Java泛型中遇到了有界嵌套通配符的问题. 这是一个常见的案例:public void doSomething(Set<? extends Number> set) {}public void callDoSomething() {Set<Integer> set = new HashSet<Integer>();doSomething(set); }这是标准的Java泛型,工作正常. 但是,如果通配符变为嵌套,则它不再起作用:public void doSomething(Map<String, Set<? extends Number>> map) {}public void callDoSomething() {Map<String, Set<Integer>> m...

使用通配符的Java集合【代码】

public static void main(String[] args) {List<? extends Object> mylist = new ArrayList<Object>();mylist.add("Java"); // compile error}上面的代码不允许您向列表中添加元素,并且通配符只能用作方法中的签名,同样不能用于添加,而只能用于访问.在这种情况下,上述目的是什么?解决方法:假设您有一个接口和两个类:interface IResult {} class AResult implements IResult {} class BResult implements IResult {}然后你有一些类...

java – 不应在返回参数中使用通用通配符类型【代码】

是否可以说通用通配符类型不应该用在方法的返回参数中? 换句话说,声明一个如下所示的接口是有意义的:interface Foo<T> {Collection<? extends T> next(); }另外,可以说通用通配符类型仅在方法的参数声明中有意义吗?解决方法:在方法形式参数中使用通配符类型的主要好处是为用户提供传递的灵活性,比如任何类型的Collection,List或任何实现Collection的东西(假设集合被声明为Collection<?> ).您经常会发现自己在形式参数中使用通配...

无法使用通配符泛型类型为Java集合添加值【代码】

为什么这段代码不能编译(Parent是一个接口)?List<? extends Parent> list = ... Parent p = factory.get(); // returns concrete implementation list.set(0, p); // fails here: set(int, ? extends Parent) cannot be applied to (int, Parent)解决方法:为了安全起见,这样做.想象一下,如果它有效:List<Child> childList = new ArrayList<Child>(); childList.add(new Child());List<? extends Parent> parentList = childL...

java – 向后兼容用像Wild <?>这样的通配符替换像Collection这样的原始类型吗?【代码】

我是某个开源库的作者.其中一个公共接口具有使用像Collection这样的原始类型的方法,例如:public StringBuilder append(..., Collection value);我得到的是一个原始类型.对泛型类型Collection< E>的引用.应该是参数化警告. 我正在考虑修复这些警告.实现实际上并不关心集合中元素的类型.所以我在考虑更换Collection<?>. 但是,这些方法是我的库的公共接口的一部分.客户端代码可以调用这些方法或提供这些公共接口的自己实现,从而实现...

java – 这是一种允许用户进行通配符搜索的正确方法吗?【代码】

例如,给定一个文本框名称,用户要求希望能够进行通配符搜索(例如contains,starts with,ends with). 是否可以接受sql通配符(‘%’和’_’)作为输入,只要我仍在后端(Java)中使用参数化查询?实际上,允许用户构建自己的正则表达式,这是用户的要求所在. 例: >用户类型textbox = '%are%'>此参数将作为后端提供给后端:public class PersonDaoImpl {public List<Person> search(String name){//name gets the value from textbox w/ sql...