The first:
Realization of ideas: the use of two for looping through the set of all elements, and then determines whether the same element, if any, is removed. This approach is most of the first thought is the most simple implementation.
Among them, this way we can guarantee List collection of original order unchanged.
/** * Notes: two for loops to re-implement List * @param list * @return */ public static List repeatListWayOne(List<String> list){ for(int i = 0;i < list.size();i++){ for(int j = i+1;j < list.size();j++){ if(list.get(i).equals(list.get(j))){ list.remove(j); } } } return list; }
The second:
Realization of ideas: we know HashSet implements Set interface does not allow duplicate elements. Can be based on this idea, the set of all elements into the List HashSet object, and then clear all the elements of the List collection,
Finally HashSet object is added to the List of all the elements in the collection, so as to ensure non-duplication of elements. And there is a HashSet constructor elements may be added directly during initialization. Which, HashSet not guarantee the order unchanged, so this method can not guarantee the List collection of original order unchanged.
/** * Notes: Use HashSet to re-implement List * @param list * @Return * / public static List repeatListWayTwo (List <String> list) { // initialize HashSet object, the object list and the element assigned to the object HashSet HashSet SET = new new HashSet (list); // the set of all elements in List Empty list .clear (); // add objects to the HashSet List collection list.addAll (the SET); return List; }
Third:
Realization of ideas: TreeSet collection also implements the Set interface, it is an orderly, and no repeated elements of the collection. Similarly, we can go re-thinking based on the above two ways. Wherein, List can be re-set to the original order and are consistent.
/** * Notes: Use TreeSet implement deduplication List * @param list * @Return * / public static List repeatListWayThird (List <String> list) { // initialize TreeSet object list and the element assigned to the target TreeSet TreeSet SET = new new TreeSet (list); // the set of all elements in List Empty list .clear (); // add TreeSet objects to the List collection list.addAll (the SET); return List; }
Fourth:
Realization of ideas: the use of loop through the List collection contains method, first create a new List collection, then loop through the original List collection to determine whether the new collection contains a collection of old, and if so, is not added to the new collection, otherwise add.
Finally, the collection of old emptied, the new collection of elements assigned to the old collection.
/** * Notes: using List contains a set of de-emphasis method to loop through * @param list * @Return * / public static List repeatListWayFourth (List <String> List) { // Create a new List collection, storage elements for the weight to List <String> newList = new new the ArrayList <String> (); // Loop through old set of elements for ( int I = 0; I <list.size (); I ++ ) { // determines whether the new set includes, if there is not included, then the new stored set of Boolean isContains = newList.contains (List. GET (I)); IF (! isContains) { newList.add(list.get(i)); } } // the set of all elements in List empty list.clear (); // add a new element to the collection List collection list.addAll (newList); return List; }
Summary: Second way is the best way to re-code the most simple and fastest total time.