Two Methods to Remove Duplicates in an ArrayList

本文介绍两种去除ArrayList中重复元素的方法:一种不保持原有顺序,利用HashSet的特性快速去除重复;另一种保持顺序,通过迭代检查并添加未重复元素到新列表。
Two Methods to Remove Duplicates in an ArrayList
Here are two methods that allow you to remove duplicates in an ArrayList. removeDuplicate does not maintain the order where as removeDuplicateWithOrder maintains the order with some performance overhead.

1.The removeDuplicate Method:
/** List order not maintained **/
public static void removeDuplicate(ArrayList arlList)

   HashSet h = new HashSet(arlList); 
   arlList.clear(); 
   arlList.addAll(h);
}

 

2.The removeDuplicateWithOrder Method: 
/** List order maintained **/
public static void removeDuplicateWithOrder(ArrayList arlList)

   Set set = new HashSet(); 
   List newList = new ArrayList(); 
   for (Iterator iter = arlList.iterator(); iter.hasNext(); ) 
   { 
      Object element = iter.next(); 
      if (set.add(element)) newList.add(element); 
   } 
   arlList.clear(); 
   arlList.addAll(newList);
 
Java task 2: Library service and books [15 marks total] Task 2a [5 marks] Create a class called Book to represent the properties of a book that the hero can read to learn a new skill. The properties should include: A title (represented as a String); An author (represented as a String); A number of pages (represented as an integer); and A skill to be learned (represented using the Skill enumeration) Your Book class should have a constructor that sets all fields, and getter methods for all properties. You should also override the equals method so that two books are considered equal if the title and author are the same. You do not need to do any error checking on the book properties and do not need to implement setters. Also add a method to the Hero class with the following signature and behaviour: public void readBook (Book book) This method should update the hero’s current skill to the skill provided by the book and print a message to System.out “<HeroName> has read '<Title>' and now knows: <Skill>” Task 2b [2 marks] Define a class Library that stores a collection of Books. The collection should be initially empty. Add a method to Library with the following signature and behaviour: public void addBook (Book book) This method should add the indicated book to the collection. You do not need to check for duplicates, it is fine if multiple copies of a book are in the collection. Task 2c [4 marks] Add a method to your Library class with the following signature and behaviour. public boolean removeBook (Book book) This method should look for a book in the collection that matches the input parameter (using equals()). If a matching book is found, remove it from the collection and return true. If no matching book is found, do nothing and return false. Task 2d [4 marks] Add one more method to your Library class with the following signature and behaviour. public boolean assignBestSkillBook(Hero hero, Skill desiredSkill) If the hero already knows the desired skill,
最新发布
12-10
在不同场景下有不同的去除重复项方法: ### Excel 中去除重复项 在工作中对原始记录清单进行整理时,若要剔除某些重复项(重复项通常指某些记录在各个字段中都有相同的内容,纵向称为字段,横向称为记录),可使用 Excel 去除重复项的常用手法来处理[^1][^2]。虽然具体方法未在引用中给出,但一般在 Excel 中可以通过数据菜单下的“删除重复项”功能来实现,选中数据区域后,点击该功能,Excel 会自动识别并删除完全相同的记录。 ### MySQL 中去除重复项 虽然引用中未详细说明 MySQL 去除重复项的具体步骤,但通常可以使用 SQL 语句来完成。例如,假设有一个表 `table_name`,有字段 `col1`, `col2`, ... ,要删除重复记录,可以先创建一个临时表,将不重复的记录插入临时表,再删除原表,最后将临时表重命名为原表名。示例代码如下: ```sql -- 创建临时表 CREATE TEMPORARY TABLE temp_table AS SELECT DISTINCT * FROM table_name; -- 删除原表 DROP TABLE table_name; -- 重命名临时表为原表名 ALTER TABLE temp_table RENAME TO table_name; ``` ### Java 中去除重复项 在 Java 程序中,若要在不使用 `Set` 的情况下从 `ArrayList` 中删除重复项,可以使用 Java 8 的 Stream API。以下是示例代码: ```java import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); System.out.println(numbersList); List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList()); System.out.println(listWithoutDuplicates); } } ``` 上述代码通过 `stream().distinct()` 方法去除了 `ArrayList` 中的重复元素[^3]。 ### Python 中去除重复项 在 Python 中去除重复项有多种方法,但需要注意这些方法在处理元素较大的列表时可能会降低处理效率,因此需要根据实际情况选择合适的方法。同时,对于元素类型为对象的列表,需要重载对象的比较方法,才能正确地去重[^4]。以下是几种常见的去除重复项的方法: #### 使用集合(Set) ```python original_list = [1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8] unique_list = list(set(original_list)) print(unique_list) ``` #### 遍历列表 ```python original_list = [1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8] unique_list = [] for item in original_list: if item not in unique_list: unique_list.append(item) print(unique_list) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值