类型不匹配:无法从 Map<String,List<Animal>> 转换为 Map<String,List<Dog>>
Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>>
我在解决泛型问题时遇到了一些问题。我有一个 "Cat" objects 列表和一个 "Dog" objects 列表,我需要将它们传递给相同的方法。该方法的 return 类型是 "String" 的地图和 "List of Animals" 我试图找出一种方法将带有动物列表的地图转换为带有列表的地图猫或狗。
如果我有一个单独的猫和狗方法,这很好用,但我正在寻找更灵活的解决方案。
标题中出现错误的行:
catMap = PetStore.groupAnimalsByOwner(cats);
dogMap = PetStore.groupAnimalsByOwner(dogs);
注意:这是一个简化的示例,我必须能够将地图中的列表用作 "Cat" 或 "Dog" objects.
public class Start {
public static void main(String[] args) {
Cat cat1 = new Cat("Jerry", "cat1");
Cat cat2 = new Cat("Jerry", "cat2");
Cat cat3 = new Cat("Fred", "cat3");
List<Cat> cats = new LinkedList<Cat>();
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
Dog dog1 = new Dog("Frank", "dog1");
Dog dog2 = new Dog("Jerry", "dog2");
Dog dog3 = new Dog("Bob", "dog3");
List<Dog> dogs = new LinkedList<Dog>();
dogs.add(dog1);
dogs.add(dog2);
dogs.add(dog3);
Map<String, List<Dog>> dogMap = new HashMap<String, List<Dog>>();
Map<String, List<Cat>> catMap = new HashMap<String, List<Cat>>();
// catMap should have 2 key/value pairs - key "Jerry" with a list containing cat1 and cat2
// and a pair - key "Fred" with a list containing only cat3
catMap = PetStore.groupAnimalsByOwner(cats);
// dogMap should have 3 key/value pairs - key "Frank" with a list containing dog1
// key "Jerry" with a list containing dog2
// Key "Bob" with a list containing dog3
dogMap = PetStore.groupAnimalsByOwner(dogs);
}
}
public class PetStore {
//Grouping by owner
public static Map<String, List<Animal>> groupAnimalsByOwner(List<? extends Animal> animals) {
Map<String, List<Animal>> groupedMap = new HashMap<String, List<Animal>>();
List<Animal> tempList = null;
for (Animal summary : animals) {
String consolidatedInvoiceId = summary.getOwner();
tempList = groupedMap.get(consolidatedInvoiceId);
if (tempList == null) {
tempList = new LinkedList<Animal>();
}
tempList.add(summary);
groupedMap.put(consolidatedInvoiceId, tempList);
}
return groupedMap;
}
}
public interface Animal {
public String getOwner();
}
public class Cat implements Animal {
private String owner;
private String name;
public Cat(String owner, String name) {
this.owner = owner;
this.name = name;
}
@Override
public String getOwner() {
return owner;
}
public String getName() {
return name;
}
public void doCatStuff() {
System.out.println("Do cat stuff");
}
}
Dog class 与 Cat 相同,但具有 doCatStuff 方法。
提前致谢。
正如 List<Dog>
isn't a List<Animal>
,Map<String, List<Cat>>
不是 Map<String, List<Animal>>
。
使您的 groupAnimalsByOwner
方法通用,以 Animal
作为上限,以便 T
被推断为 Cat
(或 Dog
) .您需要在方法主体中将 Animal
替换为 T
。
public static <T extends Animal> Map<String, List<T>>
groupAnimalsByOwner(List<? extends T> animals)
{
Map<String, List<T>> groupedMap = new HashMap<String, List<T>>();
List<T> tempList = null;
for (T summary : animals) {
String consolidatedInvoiceId = summary.getOwner();
tempList = groupedMap.get(consolidatedInvoiceId);
if (tempList == null) {
tempList = new LinkedList<T>();
}
tempList.add(summary);
groupedMap.put(consolidatedInvoiceId, tempList);
}
return groupedMap;
}
将List<Cat>
和List<Dog>
更改为List<Animal>
也许这会有所帮助:https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
您将该方法声明为返回 Map<String, List<Animal>>
。此类型与 Map<String, List<Dog>>
不兼容,因为 List<Animal>
与 List<Dog>
不兼容。这些不兼容,因为第一个声明 List<Animal>
不允许 Animal
的任何子类型。其次,虽然所有 Dog
都是 Animal
,但并非所有 Animal
都是 Dog
。
对于这种情况,您需要的是占位符。
public static <T extends Animal> Map<String, List<T>> groupAnimalsByOwner(List<T> animals) {
我在解决泛型问题时遇到了一些问题。我有一个 "Cat" objects 列表和一个 "Dog" objects 列表,我需要将它们传递给相同的方法。该方法的 return 类型是 "String" 的地图和 "List of Animals" 我试图找出一种方法将带有动物列表的地图转换为带有列表的地图猫或狗。
如果我有一个单独的猫和狗方法,这很好用,但我正在寻找更灵活的解决方案。
标题中出现错误的行:
catMap = PetStore.groupAnimalsByOwner(cats);
dogMap = PetStore.groupAnimalsByOwner(dogs);
注意:这是一个简化的示例,我必须能够将地图中的列表用作 "Cat" 或 "Dog" objects.
public class Start {
public static void main(String[] args) {
Cat cat1 = new Cat("Jerry", "cat1");
Cat cat2 = new Cat("Jerry", "cat2");
Cat cat3 = new Cat("Fred", "cat3");
List<Cat> cats = new LinkedList<Cat>();
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
Dog dog1 = new Dog("Frank", "dog1");
Dog dog2 = new Dog("Jerry", "dog2");
Dog dog3 = new Dog("Bob", "dog3");
List<Dog> dogs = new LinkedList<Dog>();
dogs.add(dog1);
dogs.add(dog2);
dogs.add(dog3);
Map<String, List<Dog>> dogMap = new HashMap<String, List<Dog>>();
Map<String, List<Cat>> catMap = new HashMap<String, List<Cat>>();
// catMap should have 2 key/value pairs - key "Jerry" with a list containing cat1 and cat2
// and a pair - key "Fred" with a list containing only cat3
catMap = PetStore.groupAnimalsByOwner(cats);
// dogMap should have 3 key/value pairs - key "Frank" with a list containing dog1
// key "Jerry" with a list containing dog2
// Key "Bob" with a list containing dog3
dogMap = PetStore.groupAnimalsByOwner(dogs);
}
}
public class PetStore {
//Grouping by owner
public static Map<String, List<Animal>> groupAnimalsByOwner(List<? extends Animal> animals) {
Map<String, List<Animal>> groupedMap = new HashMap<String, List<Animal>>();
List<Animal> tempList = null;
for (Animal summary : animals) {
String consolidatedInvoiceId = summary.getOwner();
tempList = groupedMap.get(consolidatedInvoiceId);
if (tempList == null) {
tempList = new LinkedList<Animal>();
}
tempList.add(summary);
groupedMap.put(consolidatedInvoiceId, tempList);
}
return groupedMap;
}
}
public interface Animal {
public String getOwner();
}
public class Cat implements Animal {
private String owner;
private String name;
public Cat(String owner, String name) {
this.owner = owner;
this.name = name;
}
@Override
public String getOwner() {
return owner;
}
public String getName() {
return name;
}
public void doCatStuff() {
System.out.println("Do cat stuff");
}
}
Dog class 与 Cat 相同,但具有 doCatStuff 方法。
提前致谢。
正如 List<Dog>
isn't a List<Animal>
,Map<String, List<Cat>>
不是 Map<String, List<Animal>>
。
使您的 groupAnimalsByOwner
方法通用,以 Animal
作为上限,以便 T
被推断为 Cat
(或 Dog
) .您需要在方法主体中将 Animal
替换为 T
。
public static <T extends Animal> Map<String, List<T>>
groupAnimalsByOwner(List<? extends T> animals)
{
Map<String, List<T>> groupedMap = new HashMap<String, List<T>>();
List<T> tempList = null;
for (T summary : animals) {
String consolidatedInvoiceId = summary.getOwner();
tempList = groupedMap.get(consolidatedInvoiceId);
if (tempList == null) {
tempList = new LinkedList<T>();
}
tempList.add(summary);
groupedMap.put(consolidatedInvoiceId, tempList);
}
return groupedMap;
}
将List<Cat>
和List<Dog>
更改为List<Animal>
也许这会有所帮助:https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html
您将该方法声明为返回 Map<String, List<Animal>>
。此类型与 Map<String, List<Dog>>
不兼容,因为 List<Animal>
与 List<Dog>
不兼容。这些不兼容,因为第一个声明 List<Animal>
不允许 Animal
的任何子类型。其次,虽然所有 Dog
都是 Animal
,但并非所有 Animal
都是 Dog
。
对于这种情况,您需要的是占位符。
public static <T extends Animal> Map<String, List<T>> groupAnimalsByOwner(List<T> animals) {