读书人

除开集合中的重复元素

发布时间: 2013-08-13 16:43:28 作者: rapoo

去除集合中的重复元素

/** ?

?* 通过HashSet踢除重复元素除去List集合中的重复数据 ?

* */ ???

public static List<Integer> removeDuplicate(List<Integer> list)?

{ ???????

?  HashSet<Integer> h = new? HashSet<Integer>(list); ???????

   list.clear(); ???????

   list.addAll(h); ???????

?  return list; ????

} ?

/** ?

* 写个去除数组中重复数据的方法 ?

?* */ ?

public static String[] array_unique(String[] a)

{?? ????

?  // array_unique?? ????

  List<String> list = new LinkedList<String>();?? ????

  for(int i = 0; i < a.length; i++) {?? ????????

  ?if(!list.contains(a[i]))

  {?? ????????????

    list.add(a[i]);?? ????????

?  }?? ????

?}?? ????

return (String[])list.toArray(new String[list.size()]);??

?}?

读书人网 >编程

热点推荐