我有一個實作串列SomeInterface
List<SomeInterface> listOfThingsThatImplementsSomeInterface
我想根據它的類名從串列中提取一個特定的實作
private SomeInterface getThisType(SomeInterfaceImpl myType) {
SomeInterfaceImpl impl = null
for (SomeInterface current: listOfThingsThatImplementsSomeInterface) {
if (current instanceof myType) {
impl = current
break;
}
}
return impl;
}
這在Java中可能嗎?我應該使用泛型嗎?
uj5u.com熱心網友回復:
首先,您需要Class所需類的實體(您擁有,您提到“根據其類名”)。您可以Class使用從類名中獲取實體
Class<?> cls = SomeInterfaceImpl.class;
或者如果您將類名作為字串:
Class<?> cls = Class.forName("package.ClassName")
或從使用的實體
SomeInterfaceImpl myType = ...;
Class<?> cls = myType.getClass()
然后你可以呼叫這個方法,假設你有一個List<SomeInterface> listOfThingsThatImplementsSomeInterface地方:
private SomeInterface getThisType(Class<?> myType) {
for (SomeInterface current: listOfThingsThatImplementsSomeInterface) {
if (myType.isInstance(current)) {
return current;
}
}
return null; //nothing found
}
uj5u.com熱心網友回復:
該方法應將類作為引數。假設你的介面是T,你可以接收一個型別的引數Class<? extends T>
然后遍歷串列并檢查具有相同類的元素
這是一個應該有效的示例:
private static <T> Optional<T> getThisType(
List<T> listOfThingsThatImplementsInterfaceT,
Class<? extends T> klass) {
return listOfThingsThatImplementsInterfaceT.stream()
.filter(klass::isInstance)
.findFirst();
}
使用串列進行測驗Serializable
List<Serializable> listOfThingsThatImplementsSerializable = Arrays.asList(
new Integer(1), new Float(2), new Double(3)
);
System.out.println(
getThisType(listOfThingsThatImplementsSerializable, Float.class)
);
尋找Float回報Optional[2.0]
uj5u.com熱心網友回復:
假設我們有所需的實作Class實體,(如果類是,請參閱@f1sh 答案Class<?>),可以使用 Stream 來完成,
private <T extends SomeInterface> Optional<T> getThisType(Class<T> type) {
return this.someInterfaceList.stream().filter(type::isInstance)
.map(type::cast).findFirst();
}
匹配型別的關鍵是使用Class#isInstance.
需要注意的幾點:
- 使方法回傳型別通用,因此不需要強制轉換。
Class#cast可用于轉換 Stream 的回傳型別。- 用于
Optional在沒有元素匹配時處理大小寫。
完整示例
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class FilterByClass {
List<SomeInterface> someInterfaceList = new ArrayList<>();
public static void main(String[] args) {
FilterByClass filterByClass = new FilterByClass();
filterByClass.someInterfaceList = List.of(new SomeInterfaceImplA(), new SomeInterfaceImplB(), new SomeInterfaceImplC());
System.out.println(filterByClass.getThisType(SomeInterfaceImplA.class).get().name());
System.out.println(filterByClass.getThisType(SomeInterfaceImplB.class).get().name());
System.out.println(filterByClass.getThisType(SomeInterfaceImplC.class).get().name());
}
private <T extends SomeInterface> Optional<T> getThisType(Class<T> type) {
return this.someInterfaceList.stream().filter(type::isInstance)
.map(type::cast).findFirst();
}
public interface SomeInterface {
default String name() {
return this.getClass().getSimpleName();
}
}
public static class SomeInterfaceImplA implements SomeInterface { }
public static class SomeInterfaceImplB implements SomeInterface { }
public static class SomeInterfaceImplC implements SomeInterface { }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456088.html
