简体   繁体   中英

ArrayList or Hashset

ArrayList queryParms = new ArrayList();
StringBuffer sql = new StringBuffer();
sql.append(
"SELECT 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, eg

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), ");
......

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