开发者

ArrayList or Hashset

开发者 https://www.devze.com 2023-01-17 22:39 出处:网络
ArrayList queryParms = new ArrayList(); StringBuffer sql = new StringBuffer(); sql.append( \"S开发者_StackOverflowELECT A.FLDREC_NUM, \" +
ArrayList queryParms = new ArrayList();
StringBuffer sql = new StringBuffer();
sql.append(
"S开发者_StackOverflowELECT A.FLDREC_NUM, " +
"@CHARDATE(A.FLDDATE), " +
"T.FLDDESCR, @DEC(A.FLDLEVEL,3) " +
" FROM @SCHEMAALCOHOL A LEFT OUTER JOIN @SCHEMADRUGCAUS T " +
" ON A.FLDREASON = T.FLDCODE " +
" WHERE A.FLDEMPLOYEE = ? " +
" ORDER BY A.FLDDATE DESC"
);
queryParms.add(new Long(empRec));
  1. Can i use HashSet instead of ArrayList above and does it make any sense in terms of performance?.

  2. What does the query do, do we need to append the query in StringBuffer. Why can't i directly add to ArrayList?


In general, you would want to use an ArrayList for query parameters - because the order matters. When you've only got a single parameter (as you have here) it's not an issue - but I'd definitely use a list for consistency.

As for using StringBuffer - you haven't shown what you're doing with sql here. If you're not appending anything else, you could use a simple String... but you wouldn't be adding it to the ArrayList, as it's the query itself, not a query parameter.

Finally, I'd recommend using generics, e.g.

ArrayList<Long> queryParameters = new ArrayList<Long>();


As for performance, consider:

  1. Using StringBuilder(instead of StringBuffer) which is not synchronized and is not an issue, since you're creating a new StringBuilder() for each request. If it's the same statement used for each request, it should be a final String, globally declared.

  2. As suggested by Jon, use ArrayList with Generics. The queryParams is used as a part of QueryService implementation used to query DB. As it appears, it's a pre-compiled sql query and order of query params is important at run-time. HashSet doesn't guarantee order.


more efficient:

sql.append("SELECT A.FLDREC_NUM, ");
sql.append("@CHARDATE(A.FLDDATE), ");
......
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号