Java two methods to delete repeated elements in the ArrayList

Source: Internet
Author: User

Java two methods to delete repeated elements in the ArrayList

ArrayList is one of the most common collection types in Java. It allows flexible addition of multiple null elements, repeated elements, and the insertion sequence of elements. During encoding, we often encounter the requirement that duplicate elements must be deleted from the completed ArrayList. This article provides two methods to delete duplicate elements from ArrayList.

Method 1: Use HashSet to delete repeated elements in ArrayList

In this method, we use HashSet to delete repeated elements. As you know, HashSet does not allow repeated elements. We use the HashSet attribute to delete duplicate elements in the existing ArrayList. However, this method has a disadvantage. That is, it will delete the insertion sequence of elements in the ArrayList. This means that the insertion sequence of elements is incorrect after repeated elements are deleted. Let's take a look at the example below.

Import java. util. ArrayList;
Import java. util. HashSet;

Public class MainClass
{
Public static void main (String [] args)
{
// Constructing An ArrayList

ArrayList <String> listWithDuplicateElements = new ArrayList <String> ();

ListWithDuplicateElements. add ("JAVA ");

ListWithDuplicateElements. add ("J2EE ");

ListWithDuplicateElements. add ("JSP ");

ListWithDuplicateElements. add ("SERVLETS ");

ListWithDuplicateElements. add ("JAVA ");

ListWithDuplicateElements. add ("STRUTS ");

ListWithDuplicateElements. add ("JSP ");

// Printing listWithDuplicateElements

System. out. print ("ArrayList With Duplicate Elements :");

System. out. println (listWithDuplicateElements );

// Constructing HashSet using listWithDuplicateElements

HashSet <String> set = new HashSet <String> (listWithDuplicateElements );

// Constructing listWithoutDuplicateElements using set

ArrayList <String> listWithoutDuplicateElements = new ArrayList <String> (set );

// Printing listWithoutDuplicateElements

System. out. print ("ArrayList After Removing Duplicate Elements :");

System. out. println (listWithoutDuplicateElements );
}
}

Output:

ArrayList With Duplicate Elements: [JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements: [JAVA, SERVLETS, JSP, J2EE, STRUTS]

Note the output result. You will find that the elements are shuffled after the repeated elements are deleted. No longer arranged in the insert order. If you want to keep the insertion sequence of elements after deleting duplicate elements, we do not recommend that you use this method. Another method is to ensure that the insertion sequence of elements is not changed after repeated elements are deleted. That is, use javashashset.

Method 2: Use javashashset to delete repeated elements in ArrayList

In this method, we use javashashset to delete repeated elements in the ArrayList. As you know, repeated hashset does not allow repeated elements and maintains the insertion sequence of elements. The two attributes of LinkedHashSet ensure that the element insertion sequence is maintained after the repeated elements in the ArrayList are deleted. See the following example.

Import java. util. ArrayList;
Import java. util. LinkedHashSet;

Public class MainClass
{
Public static void main (String [] args)
{
// Constructing An ArrayList

ArrayList <String> listWithDuplicateElements = new ArrayList <String> ();

ListWithDuplicateElements. add ("JAVA ");

ListWithDuplicateElements. add ("J2EE ");

ListWithDuplicateElements. add ("JSP ");

ListWithDuplicateElements. add ("SERVLETS ");

ListWithDuplicateElements. add ("JAVA ");

ListWithDuplicateElements. add ("STRUTS ");

ListWithDuplicateElements. add ("JSP ");

// Printing listWithDuplicateElements

System. out. print ("ArrayList With Duplicate Elements :");

System. out. println (listWithDuplicateElements );

// Constructing LinkedHashSet using listWithDuplicateElements

LinkedHashSet <String> set = new LinkedHashSet <String> (listWithDuplicateElements );

// Constructing listWithoutDuplicateElements using set

ArrayList <String> listWithoutDuplicateElements = new ArrayList <String> (set );

// Printing listWithoutDuplicateElements

System. out. print ("ArrayList After Removing Duplicate Elements :");

System. out. println (listWithoutDuplicateElements );
}
}

Output:

ArrayList With Duplicate Elements: [JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements: [JAVA, J2EE, JSP, SERVLETS, STRUTS]

Note the output. You can find that the element insertion sequence remains unchanged after the repeated elements in the ArrayList are deleted.

Http://www.codeceo.com/article/java-arraylist-remove-duplicate-ele.html.
How To Remove Duplicate Elements From ArrayList In Java?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.