java 对list的操作

  
  
  1. public class ListUtils {
  2. /**
  3. * 判断list中存储的对象列表中是否存在一个某属性与指定值相匹配的元素
  4. *
  5. * @param objList 对象列表
  6. * @param propertyName 指定的属性
  7. * @param destObj 要匹配的值
  8. * @return boolean
  9. */
  10. public static boolean contains(List objList, String propertyName, Object destObj) {
  11. boolean exist = false;
  12. Iterator it = objList.iterator();
  13. while (it.hasNext()) {
  14. Object bean = it.next();
  15. try {
  16. String getMethodStr = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
  17. Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
  18. Object propertyValue = getMethod.invoke(bean, null);
  19. if (propertyValue.equals(destObj)) {
  20. exist = true;
  21. break;
  22. }
  23. } catch (SecurityException e1) {
  24. e1.printStackTrace();
  25. } catch (NoSuchMethodException e1) {
  26. e1.printStackTrace();
  27. } catch (IllegalArgumentException e) {
  28. e.printStackTrace();
  29. } catch (IllegalAccessException e) {
  30. e.printStackTrace();
  31. } catch (InvocationTargetException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. return exist;
  36. }
  37. /**
  38. * 判断list中存储的对象列表中是否存在一个某组属性与指定一组值相匹配的元素
  39. *
  40. * @param objList 对象列表
  41. * @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
  42. * 指定的属性数组
  43. * @param destObjs 要匹配的值数组
  44. * @return boolean
  45. */
  46. public static boolean contains(List objList, String[] propertyNames, Object[] destObjs) {
  47. boolean exist = false;
  48. Iterator it = objList.iterator();
  49. while (it.hasNext()) {
  50. try {
  51. Object bean = it.next();
  52. int i = 0;
  53. for (i = 0; i < propertyNames.length; i++) {
  54. String getMethodStr = "get" + Character.toUpperCase(propertyNames[i].charAt(0)) + propertyNames[i].substring(1);
  55. Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
  56. Object propertyValue = getMethod.invoke(bean, null);
  57. if (!propertyValue.equals(destObjs[i])) {
  58. break;
  59. }
  60. }
  61. if (i == propertyNames.length) {
  62. exist = true;
  63. break;
  64. }
  65. } catch (SecurityException e1) {
  66. e1.printStackTrace();
  67. } catch (NoSuchMethodException e1) {
  68. e1.printStackTrace();
  69. } catch (IllegalArgumentException e) {
  70. e.printStackTrace();
  71. } catch (IllegalAccessException e) {
  72. e.printStackTrace();
  73. } catch (InvocationTargetException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. return exist;
  78. }
  79. /**
  80. * 获取list存储的对象列表中与某组属性与指定一组值相匹配的元素
  81. *
  82. * @param objList 对象列表
  83. * @param propertyName (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
  84. * 指定的属性
  85. * @param destObj 要匹配的值
  86. * @return Object
  87. */
  88. public static Object getMatchedBean(List objList, String propertyName, Object destObj) {
  89. Object matchBean = null;
  90. Iterator it = objList.iterator();
  91. while (it.hasNext()) {
  92. try {
  93. Object bean = it.next();
  94. String getMethodStr = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
  95. Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
  96. Object propertyValue = getMethod.invoke(bean, null);
  97. if (propertyValue.equals(destObj)) {
  98. matchBean = bean;
  99. break;
  100. }
  101. } catch (SecurityException e1) {
  102. e1.printStackTrace();
  103. } catch (NoSuchMethodException e1) {
  104. e1.printStackTrace();
  105. } catch (IllegalArgumentException e) {
  106. e.printStackTrace();
  107. } catch (IllegalAccessException e) {
  108. e.printStackTrace();
  109. } catch (InvocationTargetException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. return matchBean;
  114. }
  115. /**
  116. * 获取list存储的对象列表中与某组属性与指定一组值相匹配的元素
  117. *
  118. * @param objList 对象列表
  119. * @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
  120. * 指定的属性数组
  121. * @param destObjs 要匹配的值数组
  122. * @return Object
  123. */
  124. public static Object getMatchedBean(List objList, String[] propertyNames, Object[] destObjs) {
  125. Object matchBean = null;
  126. Iterator it = objList.iterator();
  127. while (it.hasNext()) {
  128. try {
  129. Object bean = it.next();
  130. int i = 0;
  131. for (i = 0; i < propertyNames.length; i++) {
  132. String getMethodStr = "get" + Character.toUpperCase(propertyNames[i].charAt(0)) + propertyNames[i].substring(1);
  133. Method getMethod = bean.getClass().getDeclaredMethod(getMethodStr, null);
  134. Object propertyValue = getMethod.invoke(bean, null);
  135. if (!propertyValue.equals(destObjs[i])) {
  136. break;
  137. }
  138. }
  139. if (i == propertyNames.length) {
  140. matchBean = bean;
  141. break;
  142. }
  143. } catch (SecurityException e1) {
  144. e1.printStackTrace();
  145. } catch (NoSuchMethodException e1) {
  146. e1.printStackTrace();
  147. } catch (IllegalArgumentException e) {
  148. e.printStackTrace();
  149. } catch (IllegalAccessException e) {
  150. e.printStackTrace();
  151. } catch (InvocationTargetException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. return matchBean;
  156. }
  157. /**
  158. * 移除list对象列表中与某组属性与指定一组值相匹配的元素
  159. *
  160. * @param objList 对象列表
  161. * @param propertyName (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
  162. * 指定的属性
  163. * @param destObj 要匹配的值
  164. */
  165. public static void removeMatchedBean(List objList, String propertyName, Object destObj) {
  166. Iterator it = objList.iterator();
  167. while (it.hasNext()) {
  168. try {
  169. Object bean = it.next();
  170. PropertyDescriptor pd = new PropertyDescriptor(propertyName, bean.getClass());
  171. Method readMethod = pd.getReadMethod();
  172. Object propertyValue = readMethod.invoke(bean, null);
  173. if (propertyValue.equals(destObj)) {
  174. it.remove();
  175. }
  176. } catch (SecurityException e1) {
  177. e1.printStackTrace();
  178. } catch (IllegalArgumentException e) {
  179. e.printStackTrace();
  180. } catch (IllegalAccessException e) {
  181. e.printStackTrace();
  182. } catch (InvocationTargetException e) {
  183. e.printStackTrace();
  184. } catch (IntrospectionException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }
  189. /**
  190. * 移除list对象列表中与某组属性与指定一组值相匹配的元素
  191. *
  192. * @param objList 对象列表
  193. * @param propertyNames (该属性不可以用hibernate model中其他对象的名称,因为是代理的所以用equals无法比对)
  194. * 指定的属性数组
  195. * @param destObjs 要匹配的值数组
  196. */
  197. public static void removeMatchedBean(List objList, String[] propertyNames, Object[] destObjs) {
  198. Iterator it = objList.iterator();
  199. while (it.hasNext()) {
  200. try {
  201. Object bean = it.next();
  202. int i = 0;
  203. for (i = 0; i < propertyNames.length; i++) {
  204. PropertyDescriptor pd = new PropertyDescriptor(propertyNames[i], bean.getClass());
  205. Method readMethod = pd.getReadMethod();
  206. Object propertyValue = readMethod.invoke(bean, null);
  207. if (!propertyValue.equals(destObjs[i])) {
  208. break;
  209. }
  210. }
  211. if (i == propertyNames.length) {
  212. it.remove();
  213. }
  214. } catch (SecurityException e1) {
  215. e1.printStackTrace();
  216. } catch (IllegalArgumentException e) {
  217. e.printStackTrace();
  218. } catch (IllegalAccessException e) {
  219. e.printStackTrace();
  220. } catch (InvocationTargetException e) {
  221. e.printStackTrace();
  222. } catch (IntrospectionException e) {
  223. e.printStackTrace();
  224. }
  225. }
  226. }
  227. /**
  228. * 判断list是否是空的
  229. *
  230. * @param list 要判断的目标list
  231. * @return boolean
  232. */
  233. public static boolean isNotBlankList(List list) {
  234. boolean isNotBlank = true;
  235. if (list != null && list.size() > 0) {
  236. isNotBlank = true;
  237. } else {
  238. isNotBlank = false;
  239. }
  240. return isNotBlank;
  241. }
  242. /**
  243. * 将数组转换成List
  244. *
  245. * @param strs
  246. * @return
  247. */
  248. public static List<String> toStringList(String[] strs) {
  249. List<String> returnLst = new ArrayList<String>(strs.length);
  250. for (String objstr : strs) {
  251. returnLst.add(objstr);
  252. }
  253. return returnLst;
  254. }
  255. /**
  256. * 将数组转换成List
  257. *
  258. * @param strs :以,分隔
  259. * @return
  260. */
  261. public static List<String> toStringList(String str) {
  262. List<String> returnLst = new ArrayList<String>();
  263. if (StringUtils.isBlank(str)) {
  264. return returnLst;
  265. }
  266. String[] strs = str.split(",");
  267. for (String objstr : strs) {
  268. if (StringUtils.isNotBlank(objstr)) {
  269. returnLst.add(objstr);
  270. }
  271. }
  272. return returnLst;
  273. }
  274. /**
  275. * 将字符串转换成Set
  276. *
  277. * @param strs :以,分隔
  278. * @return
  279. */
  280. public static Set<String> toStringSet(String str) {
  281. Set<String> returnSet = new HashSet<String>();
  282. if (StringUtils.isBlank(str)) {
  283. return returnSet;
  284. }
  285. String[] strs = str.split(",");
  286. for (String objstr : strs) {
  287. if (StringUtils.isNotBlank(objstr)) {
  288. returnSet.add(objstr);
  289. }
  290. }
  291. return returnSet;
  292. }
  293. public static <T> Set<T> listToSet(List<T> listt){
  294. Set<T> returnSet = new HashSet<T>();
  295. if(null!=listt&&!listt.isEmpty()){
  296. for(T t: listt){
  297. returnSet.add(t);
  298. }
  299. }
  300. return returnSet;
  301. }
  302. /**
  303. * @param ids
  304. * @return
  305. * @author:Administrator
  306. */
  307. public static List<Long> toLongList(String[] strs) {
  308. List<Long> returnLst = new ArrayList<Long>(strs.length);
  309. for (String objstr : strs) {
  310. returnLst.add(Long.parseLong(objstr));
  311. }
  312. return returnLst;
  313. }
  314. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值