在以下代码示例中:
interface Eatable{ public void printMe();}
class Animal { public void printMe(){System.out.println("Animal object");}}
class Dog extends Animal implements Eatable{ public void printMe(){System.out.println("Dog object");}}
class BullTerrier extends Dog{ public void printMe()
我主要是一个C#开发人员,我教我的朋友数据结构,他们在他们的大学里使用Java,我在Java中看到了这样一个表达:
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
我在C#中没有见过这样的东西,所以我想知道Collection<T>和Collection<?>在Java语言中的区别是什么?
void printCollection(Collection<T> c) {
for (O
在java中,我已经做了(并且正在使用),这个方法:
private final HashMap<String, Object> data = new HashMap<>();
public final @NotNull <clazz> Collection<clazz> getCollection(@NotNull String key, @NotNull Object clazz) {
Object object = this.data.get(key);
if (object instanceof
我有一个接受Collection<Object>的方法,其中Object可以是String或CustomClass。然后,它获取集合的每个元素,并将其传递给一个参数为Object的方法,如下所示:
public void foo(Collection<Object> c) {
for(Object o : c)
bar(o);
}
public void bar(Object o) {
if(o instanceof String || o instanceof CustomClass) {
...
}
}
当我
我试图编译这段代码:
import java.util.Collection;
import java.util.function.BiConsumer;
import de.hybris.platform.servicelayer.exceptions.ModelSavingException;
import de.hybris.platform.servicelayer.model.ModelService;
public class Foo {
public static interface ModelService2 {
public abstract vo
我决定在我的设计中使用泛型,这很好用。只有当我进入抽象阶段时,我才被卡住了。我正在收集不同的实例,用不同的泛型实现相同的接口。据我所知,要将它们保存在列表中,我需要使用通配符。所以我现在的代码是这样的:
import java.util.List;
import java.util.ArrayList;
public class Main {
private List<IView<?>> views = new ArrayList<IView<?>>();
public void addView(IView<?>
我对java很陌生,我有这样的代码:
List<? extends Object> k = new ArrayList<Integer>();
k.add(new Integer(1));
我得到了这样的错误:
Error:(13, 10) java: no suitable method found for add(java.lang.Integer)
method java.util.Collection.add(capture#1 of ? extends java.lang.Object) is not applicable
(argumen
我的Dao类中有以下方法
public Collection<Files> findFilesByFolder(Collection<Integer> ids) {
if (ids.size() > 0) {
StringBuffer qbe = new StringBuffer("select f from Files f where f.folders.idFolders in ( :id )");
return super.list(qbe, "id", ids)
考虑到以下不太有用的代码:
package com.something;
import java.util.ArrayList;
import java.util.Collection;
//Not a generic class!
public class Test {
public <T> void plain(T param1, T param2) {}
public <T> void fancy(T param1, Collection<T> param2) {}
public void testMethod() {
/
在上,有一段代码摘录如下:
class Holder<T>{
private T[] contents;
private int index = 0;
public Holder(int size){
//contents = new T[size]; //compiler error - generic array creation
contents = (T[]) new Object[size]; //workaround - casting Object[] to generic Type
}...}
它用于创建泛型数组,但根据类型擦除(我在
由于我是Java新手,所以我对Java泛型行为没有多少疑问。在这里,我使用本机列表作为包装器实现了自己的列表功能。下面是密码,
ListWrapper<String> li = new ListWrapper<String>(); // wrapper for native list implemented
li.add(“Hello”);
通过将列表项转换为数组并在foreach循环中使用来迭代列表中的元素。
//Method which used to convert as array in ListWrapper.
public T[] toArray() {
r
我有两个Java类:LogEntry和Record。
LogEntry类有一个如下所示的方法:
public LogEntry setRec(List<Map<String,List<Record>>> rec)
在Scala中,我尝试将Record放入LogEntry中,如下所示:
import scala.collection.JavaConversions._
import collection.mutable._
val log = new LogEntry()
val rec = new Record()
val map:java.util.M
我使用播放框架2.2:尝试构建一个游戏支持项目。
在我的Build.scala中,我想添加play.Project.playJavaSetting:
val main = play.Project(appName, appVersion, appDependencies)
.settings(play.Project.playJavaSettings) //error here
.settings(
resolvers += "webjars" at "http://webjars.github.com/m2",
resolvers += &
请有人解释一下,为什么需要为ForEachLoop实例指定泛型类型?
编译器抱怨的原因:类型不匹配:不能从元素类型对象转换为字符串
JDK 1.5.0_09
import java.util.ArrayList;
import java.util.Collection;
public class ForEachLoop<T> {
public static void main(String[] args) {
// Non functional version
ForEachLoop f = new ForEachLoop();
// Functi
假设我们有两个班。一个空类Base和这个类Derived的一个子类。
public class Base {}
public class Derived extends Base {}
然后,我们在另一个类中有几个方法:
import java.util.Collection
public class Consumer {
public void test() {
set(new Derived(), new Consumer().get());
}
public <T extends Base> T get() {
在下面的示例中,为什么编译器无法从Collections.emptySet()中推断出正确的类型?
import java.util.*;
import java.io.*;
public class Test {
public interface Option<A> {
public <B> B option(B b, F<A,B> f);
}
public interface F<A,B> {
public B f(A a);
}
public Collection
可能重复:
有人能澄清为什么编译器(Java1.6)不接受iterate1()吗?我不明白为什么iterate2()和iterate3()要好得多。
import java.util.Collection;
import java.util.HashSet;
public class Test<T> {
public Collection<String> getCollection() {
return new HashSet<String>();
}
public void iterate1(Test