去除List中的重複值ide
已經從某處引用或者得到了List<String>類型的值,須要去除重複spa
方法一:效率高element
List<String> listShare = new LinkedList<String>(); listShare.add("aa"); listShare.add("bb"); listShare.add("bb"); listShare.add("aa"); listShare.add("cc"); ArrayList<String> newst= new ArrayList<String>(new LinkedHashSet<String>(listShare));//不改變以前順序 for(String ll : newst){ System.out.println(ll); }
方法二:效率低it
List<String> listShare = new LinkedList<String>(); listShare.add("aa"); listShare.add("bb"); listShare.add("bb"); listShare.add("aa"); listShare.add("cc"); Set<Object> set = new HashSet<Object>(); List<Object> newList = new ArrayList<Object>(); for (Iterator<String> iter = listShare.iterator(); iter.hasNext();) { Object element = iter.next(); if (set.add(element)) newList.add(element); } for(Object ll : newList){ System.out.println(ll); }