Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Storing - anything more efficient

Options
  • 16-10-2009 6:44am
    #1
    Registered Users Posts: 6,240 ✭✭✭


    Hey guys

    I'm calling a DB stored Procedure and it will return rows of info like

    SECTION, SUBSECTION, HEADING

    I am meant to display / store the info as follows

    SECTION
    - ALL SUBSECTIONS under the section
    - ALL HEADINGS under this subsection

    so for example
    FINANCE, AUS, HEADER1
    FINANCE, AUS, HEADER2
    FINANCE, IE, HEADER1
    NEWS, AUS, HEADER!
    OTHER, AUS, HEADER!

    will be displayed as follows
    FINANCE
    --AUS
    ----HEADER1
    ----HEADER2
    --IE
    ----HEADER1
    NEWS
    --AUS
    ----HEADER1
    OTHER
    --AUS
    ----HEADER1

    Now these could be thousands of articles so efficient storing is vital

    This is how I have done it and want to have some feedback if there is a better way
    I store the Section in a Map - where key is value of section and the value is a map of subsection

    Subsection is also a map - key is subsection value, and value is list of headlines

    here is the code .. rowSet is just a CachedRowSet
    rowSet.beforeFirst();
    java.util.Map sectionMap = new HashMap();
    while( rowSet.next() )
    {
    	String section = rowSet.getString("SECTION_NAME"); // get the section
    	String subSection = rowSet.getString("SUB_SECTION_NAME"); //subsection
    	String headline =  rowSet.getString("MESSAGE"); // get the message
    	java.util.Map subSectionMap = new HashMap(); // key = subsection, value = list of headline
    	List headlineList = new ArrayList();
    	if (sectionMap.containsKey(section)) // if we already have a section add to it
    	{
    		subSectionMap = (Map) sectionMap.get(section); // replace the empty map with the section
    		if (subSectionMap.containsKey(subSection)) // if it contains the sub section then add the headline
    		{
    			headlineList = (List) subSectionMap.get(subSection); // replace the empty list with the list from the map
    		}    
    	}
    	headlineList.add(headline); // add the headline
    	subSectionMap.put(subSection, headlineList); // add/replace the new hedaline list
    	sectionMap.put(section, subSectionMap); // add/replace the section map with the subsection
    				
    }
    

    Cheers guys


Advertisement