Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Storing - anything more efficient

  • 16-10-2009 06:44AM
    #1
    Registered Users, Registered Users 2 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