Thursday, December 9, 2010

How to make list.toArray() return a specific class


debugStringsArray = new String[] {
"productId", "browseNode", "brand", "merchant", "productSource", "priceMetricDAL", "calculatedCPC",
"mexpMerchantIsActive", "yield", "mexpCatId", "relevance", "boost", "ctr", "tags"
};
if (newGrid) {
// boy it's a PITA to append to an already-initialized array
List debugStringsList = new ArrayList();
for (String debugString : debugStringsArray) {
debugStringsList.add(debugString);
}
debugStringsList.add("productTitle");
debugStringsList.add("imageFile");

// here is how you get around the fact that ordinary list.toArray() returns Object[]
debugStringsArray = new String[debugStringsList.size()];
debugStringsArray = debugStringsList.toArray(debugStringsArray);
}

// ETA: It's actually not that much of a PITA:
ArrayUtils.add(Object[] array, Object element) // convenience method

Wednesday, December 8, 2010

Do not forget: Stay out of debt

To create a fresh copy (not pointing at the source items) use:

Collections.copy(destList, srcList);

Tuesday, November 30, 2010

LinkedHashMap

If you have a HashMap in a particular order and want to make sure it gets maintained, use LinkedHashMap.


// LinkedHashMap remembers the order in which the items are added to the list,
// which preserves the sorting coming from the db
Map<String, List<TopSellerDO>> topSellersByCategoryMap = new LinkedHashMap<String, List<TopSellerDO>>();

Tuesday, November 2, 2010

JSTL code to iterate over a Map

Helpful snippet of JSTL code to iterate over a
Map<String, List<SomeDO>>

<c:set var="topSellersByCategoryMap" value="${results.topSellersByCategoryMap}"/>

<c:forEach var="entry" items="${topSellersByCategoryMap}">
  Name: ${entry.key}
  Value: ${entry.value}

    <c:forEach var="listitem" items="${entry.value}">
      Item: ${listitem.merchant.domain}
    </c:forEach>
</c:forEach>

Thursday, July 22, 2010

Yeats poem

This was quoted in an email from Congressman Alan Grayson and I forgot how much I love it. Putting it here to hold onto for another day.

TURNING and turning in the widening gyre
The falcon cannot hear the falconer;
Things fall apart; the centre cannot hold;
Mere anarchy is loosed upon the world,
The blood-dimmed tide is loosed, and everywhere
The ceremony of innocence is drowned;
The best lack all conviction, while the worst
Are full of passionate intensity.

William Butler Yeats, "The Second Coming"