廣告

2015年2月28日 星期六

[Java] Use recursive way to print complex data structure type until primitive data type.

As title , the ALHM(hash map in array list ) or HMAL(arraylist in hash map)
is my favorite data structure ,
but sometime if there are many content in that,
you may not see clearly from your print,
So i provide a simple way so print data clearly.

is also support ALHM(HMAL(ALHM)), nest data structure .

your data may like this:

[
  {
    key1:[item1,item2,.....],
    key2:[item1,item2,.....],
    key3:[item1,item2,.....],
    key4:[item1,item2,.....],
    key5:[item1,item2,.....],
    key6:[item1,item2,.....],
    key7:[item1,item2,.....]
  },
  
  {
    key1:[item1,item2,.....],
    key2:[item1,item2,.....],
    key3:[item1,item2,.....],
    key4:[item1,item2,.....],
    key5:[item1,item2,.....],
    key6:[item1,item2,.....],
    key7:[item1,item2,.....]
  },
  
  {
    key1:[item1,item2,.....],
    key2:[item1,item2,.....],
    key3:[item1,item2,.....],
    key4:[item1,item2,.....],
    key5:[item1,item2,.....],
    key6:[item1,item2,.....],
    key7:[item1,item2,.....]
  },
  
]  

here is code.

ViewDat.java

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.acer.util.DataUtil;


public class ViewDat {

 public boolean viewDat(String fileName) {

  File file = new File(fileName);
  if (file.exists()) {
   Object object = DataUtil.DeSerialization(fileName);
   if (object instanceof Map) {
    System.out.println(fileName + " is Map");
    getMapContent((Map)object);
   } else if (object instanceof List) {
    System.out.println(fileName + " is List");
    getListContent((List)object);
   } else {
    System.out.println(fileName + " only support map and list");
   }
  } else {
   System.out.println(fileName + " is not exist");
  }

  return true;
 }
 
 public void getMapContent(Map map ){
  
  Iterator keyIt = map.keySet().iterator();
     
  while (keyIt.hasNext()) {

   String key = keyIt.next();
   
   Object object = map.get(key);

   if (null!= object && ! DataUtil.isPrimitiveType(object.getClass())) {
    System.out.println("==="+key+"===");
   }
   
   recursive2Primitive(object,key);
  }
 }
 
 public void getListContent(List list){
  
  for(int i = 0 ; i < list.size() ; i ++  ){

   Object object = list.get(i);
   System.out.println("===================");
   recursive2Primitive(object,null);
   
   
  }
  
 }
 
 public void recursive2Primitive(Object object, String key){
  
  if (null != object && object instanceof Map) {

   getMapContent((Map) object);

  } else if (null != object && object instanceof List) {

   getListContent((List) object);

  } else if (null != object && DataUtil.isPrimitiveType(object.getClass())) {

   System.out.println(key+" : "+object.toString());

  } else if(null != object){

   System.out.println("not support this type, content is "
     + object.toString());

  }else{
   System.out.println("=============================== ");
   System.out.println("content is null, can't print ");
   System.out.println("=============================== ");
  }
  
 }

}

沒有留言:

張貼留言