Java 按类型为集合设置关键帧
我希望有一个实现特定接口的对象集合,但我希望集合中每个具体类型只有一个对象Java 按类型为集合设置关键帧,java,android,Java,Android,我希望有一个实现特定接口的对象集合,但我希望集合中每个具体类型只有一个对象 collection of implementers of dog: - instance of dachshund - instance of beagle - instance of corgi 在.NET中,有一个“KeyedByTypeCollection”。Java中是否存在类似的东西,我可以在Android上使用它 谢谢 你应该看看泛型。例如。: List dogList=newarraylist()
collection of implementers of dog:
- instance of dachshund
- instance of beagle
- instance of corgi
在.NET中,有一个“KeyedByTypeCollection”。Java中是否存在类似的东西,我可以在Android上使用它
谢谢 你应该看看泛型。例如。:
List dogList=newarraylist()代码>
编辑:若要在集合中仅包含唯一实例,应使用Set dogList=new HashSet()代码>您应该查看泛型。例如。:
List dogList=newarraylist()代码>
编辑:若要在集合中仅包含唯一实例,应使用Set dogList=new HashSet()代码>我认为您需要一个自定义HaspMap,它将使用相同的键维护多个值
因此,创建一个简单的类来扩展HashMap并将值放入其中
public class MyHashMap extends LinkedHashMap<String, List<String>> {
public void put(String key, String value) {
List<String> current = get(key);
if (current == null) {
current = new ArrayList<String>();
super.put(key, current);
}
current.add(value);
}
}
输出
{dog=[dachshund, beagle, corgi]}
我认为您需要一个自定义HaspMap,它将使用同一个键维护多个值
因此,创建一个简单的类来扩展HashMap并将值放入其中
public class MyHashMap extends LinkedHashMap<String, List<String>> {
public void put(String key, String value) {
List<String> current = get(key);
if (current == null) {
current = new ArrayList<String>();
super.put(key, current);
}
current.add(value);
}
}
输出
{dog=[dachshund, beagle, corgi]}
这可能就是您正在寻找的:
请参见代码中的注释
// two Dog(interface) implementations
// Beagle, Dachshund implements Interface Dog.
final Dog d1 = new Beagle();
final Dog d2 = new Dachshund();
// here is your collection with type <Dog>
final Set<Dog> set = new HashSet<Dog>();
set.add(d1);
set.add(d2);
// see output here
for (final Dog d : set) {
System.out.println(d.getClass());
}
// you can fill them into a map
final Map<Class, Dog> dogMap = new HashMap<Class, Dog>();
for (final Dog d : set) {
// dog instances with same class would be overwritten, so that only one instance per type(class)
dogMap.put(d.getClass(), d);
}
这可能就是您正在寻找的:
请参见代码中的注释
// two Dog(interface) implementations
// Beagle, Dachshund implements Interface Dog.
final Dog d1 = new Beagle();
final Dog d2 = new Dachshund();
// here is your collection with type <Dog>
final Set<Dog> set = new HashSet<Dog>();
set.add(d1);
set.add(d2);
// see output here
for (final Dog d : set) {
System.out.println(d.getClass());
}
// you can fill them into a map
final Map<Class, Dog> dogMap = new HashMap<Class, Dog>();
for (final Dog d : set) {
// dog instances with same class would be overwritten, so that only one instance per type(class)
dogMap.put(d.getClass(), d);
}
如果您愿意使用第三方库——如果您不关心维护秩序——在这里似乎是适用的
ClassToInstanceMap<Dog> map = MutableClassToInstanceMap.create();
map.putInstance(Corgi.class, new Corgi("Spot"));
map.putInstance(Beagle.class, new Beagle("Lady"));
Corgi corgi = map.getInstance(Corgi.class); // no cast required
ClassToInstanceMap=MutableClassToInstanceMap.create();
map.putInstance(Corgi.class,新的Corgi(“Spot”);
map.putInstance(Beagle.class,新Beagle(“Lady”);
Corgi-Corgi=map.getInstance(Corgi.class);//无需浇铸
(披露:我为番石榴做了贡献。)如果你愿意使用第三方图书馆——如果你不在乎维持秩序——在这里似乎是适用的
ClassToInstanceMap<Dog> map = MutableClassToInstanceMap.create();
map.putInstance(Corgi.class, new Corgi("Spot"));
map.putInstance(Beagle.class, new Beagle("Lady"));
Corgi corgi = map.getInstance(Corgi.class); // no cast required
ClassToInstanceMap=MutableClassToInstanceMap.create();
map.putInstance(Corgi.class,新的Corgi(“Spot”);
map.putInstance(Beagle.class,新Beagle(“Lady”);
Corgi-Corgi=map.getInstance(Corgi.class);//无需浇铸
(披露:我为番石榴做了贡献。)整洁。我认为把这种方法变得通用可以买到一些东西。我认为让这种方法变得通用可以买到一些东西。我已经看过了.Net集合的文档,我很难想象为什么它会有用(可能除了单例工厂)。使用它的原因是什么?也许Java中有一些不同的东西可以满足您的需要。我已经看过了.Net集合的文档,我很难想象它为什么会有用(除了单例工厂)。使用它的原因是什么?也许Java中有一些不同的东西可以满足您的需要。dogMap.values()
是所需的集合,不需要额外的集合。此外,我将向类中添加泛型参数:MapdogMap.values()
是所需的集合,不需要额外的集合。此外,我将向类中添加泛型参数:MapI希望尽可能保持其可移植性。特别是因为我正在考虑在Android上使用我的库。不过谢谢:)嗯?番石榴100%兼容安卓系统。事实上,一些安卓核心库的工作人员在番石榴上工作……事实上,番石榴是安卓应用程序中的一款。但是如果你关心APK的尺寸,我希望它尽可能便于携带。特别是因为我正在考虑在Android上使用我的库。不过谢谢:)嗯?番石榴100%兼容安卓系统。事实上,一些安卓核心库的工作人员在番石榴上工作……事实上,番石榴是安卓应用程序中的一款。但是如果你关心APK的大小。