简体   繁体   中英

what is a best data structure to store tables having K-V columns

I am using Apache POI to extract tables from word document. These tables have first column of key, second of value and it could be any number of such paired columns. Currently, I am iterating each table, each row, each cell to store all cells sequentially row wise in List<String>

Ultimate goal is to store results in CSV with 2 columns Attribute name and Attribute value and KV columns should be iterated in top-bottom fashion and then proceed to next 2 columns if exists.

I am iterating through List<String> ( i being index counter) and using below pseudo code to do so:

  • Using booleans keyB and valueB and initializing keyB to true and valueB to false .
  • If(keyB) then store current string as Attribute name and do i++ . Mark keyB to false and valueB to true .
  • if(valueB) then store current String as Attribute value and do i=i+<NO_OF_COLUMNS> -1 to get to position of key present at next row and mark keyB to true .
  • I am also storing topKeyInd which is initialized to first row's key index(0th element of List )
  • when we are at last row's value cell then I've used condition
    if(i+<NO_OF_COLUMNS> -1)> List.size()) then we have to start from next pair of KV columns if exists. So, i = topKeyInd +2 to get to 3rd column first key cell and update topKeyInd accordingly.
  • Continue above process until we get to last element of List .

It seems to be very complex, is there a better way to handle it?

I'm using ConcurrentHashMap ( ex: ConcurrentHashMap) because is fast and threadsafe :)

for your example :

public static ConcurrentHashMap<String, String> hashMap = new concurrentHashMap<>();

for( int i = 0 ; i< list.size(); i+=2){
    hashMap.put(list.get(i), list.get(i+1));
}

Here you have some example for simple map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM