反射,java,迭代集合
Reflection , java , iterate on collection
我正在编写一个将迭代某些集合的函数,我不知道我得到的是什么集合,所以我写到现在,PrintFieldsOfClass 将得到一些对象或对象集合,我会必须打印 class 的字段,我已经为一些 class 工作,想要添加集合支持。
void PrintFieldsOfClass(Object obj){
if(obj ==null){
return;
}
Class<?> mainClass = obj.getClass(); // create instance of the class
Class<?>[] interfaces =null;
Object collection = null;
int isCollection = 0; //1 - for List , 2 - For Set , 3 - Map
if(mainClass !=null ){
interfaces = mainClass.getInterfaces();
}
try {
if(interfaces!=null ){
for(Class<?> interface1 : interfaces ){
if(interface1.getName().toString().equals("java.util.List")) {
isCollection = 1;
collection = new ArrayList<Object>();
}
else if(interface1.getName().toString().equals("java.util.Set")){
isCollection=2;
collection = new HashSet<Object>();
}
}
}
`
知道我得到的集合后,如何将对象转换为该集合?我如何迭代它?
提前致谢!
你有没有尝试过类似的东西:
void printFieldsOfClass(Object obj) {
if (obj == null) {
return;
}
// Deal with collections...
if (obj instanceof Collection) {
for (Object o : ((Collection<Object>) obj)) {
printFieldsOfClass(o);
}
return;
}
// Otherwise it is a 'simple' object...
Class<?> mainClass = obj.getClass(); // create instance of the class
Class<?>[] interfaces =null;
Object collection = null;
int isCollection = 0; //1 - for List , 2 - For Set , 3 - Map
if(mainClass !=null ){
interfaces = mainClass.getInterfaces();
}
try {
if(interfaces!=null ){
for(Class<?> interface1 : interfaces ){
if(interface1.getName().toString().equals("java.util.List")) {
isCollection = 1;
collection = new ArrayList<Object>();
}
else if(interface1.getName().toString().equals("java.util.Set")){
isCollection=2;
collection = new HashSet<Object>();
}
}
}
我正在编写一个将迭代某些集合的函数,我不知道我得到的是什么集合,所以我写到现在,PrintFieldsOfClass 将得到一些对象或对象集合,我会必须打印 class 的字段,我已经为一些 class 工作,想要添加集合支持。
void PrintFieldsOfClass(Object obj){
if(obj ==null){
return;
}
Class<?> mainClass = obj.getClass(); // create instance of the class
Class<?>[] interfaces =null;
Object collection = null;
int isCollection = 0; //1 - for List , 2 - For Set , 3 - Map
if(mainClass !=null ){
interfaces = mainClass.getInterfaces();
}
try {
if(interfaces!=null ){
for(Class<?> interface1 : interfaces ){
if(interface1.getName().toString().equals("java.util.List")) {
isCollection = 1;
collection = new ArrayList<Object>();
}
else if(interface1.getName().toString().equals("java.util.Set")){
isCollection=2;
collection = new HashSet<Object>();
}
}
}
`
知道我得到的集合后,如何将对象转换为该集合?我如何迭代它? 提前致谢!
你有没有尝试过类似的东西:
void printFieldsOfClass(Object obj) {
if (obj == null) {
return;
}
// Deal with collections...
if (obj instanceof Collection) {
for (Object o : ((Collection<Object>) obj)) {
printFieldsOfClass(o);
}
return;
}
// Otherwise it is a 'simple' object...
Class<?> mainClass = obj.getClass(); // create instance of the class
Class<?>[] interfaces =null;
Object collection = null;
int isCollection = 0; //1 - for List , 2 - For Set , 3 - Map
if(mainClass !=null ){
interfaces = mainClass.getInterfaces();
}
try {
if(interfaces!=null ){
for(Class<?> interface1 : interfaces ){
if(interface1.getName().toString().equals("java.util.List")) {
isCollection = 1;
collection = new ArrayList<Object>();
}
else if(interface1.getName().toString().equals("java.util.Set")){
isCollection=2;
collection = new HashSet<Object>();
}
}
}