Set<Long> supervisionIds = new HashSet<>(); for(supervisionPlan temp : list){ supervisionIds.add(temp.getBidSupervisionId()); }
把存的ID值循環成數組輸出,用了Set集合的toArray(T[] a)方法
public String setToarray(Object[] in) { StringBuffer str = new StringBuffer(); for (int i = 0; i < in.length; i++) { if (in[i] != null) { str .append("'" + in[i] + "',"); } } str .insert(0,"("); str .deleteCharAt(str.length() - 1); str .append(")"); return str .toString(); }
因為toArray()返回的是Object類型的數組,所以setToarray()方法傳入一個Object[] 類型數組,
然后用StringButter在for循環中拼接
setToarray(supervisionIds.toArray(new Long[]{}));
最后輸出:('值1','值2','值3'.....)
