3:11 pm - Saturday November 23, 6458

Remove Duplicate Entries from an array using asList()

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to Remove Duplicate Entries from an array using asList() method.

The easiest way to remove duplicate entries from the given array is, create TreeSet object andadd array entries to the TreeSet.

Since the set doesnot support duplicate entries, you will get only unique elements left with TreeSet.

Example

import java.util.Arrays;

import java.util.List;

import java.util.TreeSet;

public class MyArrayDuplicates

{

   public static void main(String a[])

{

       String[] strArr = {"one","two","three","four","four","five"};

       List<String> tmpList = Arrays.asList(strArr);

       TreeSet<String> unique = new TreeSet<String>(tmpList);

       System.out.println(unique);

   }

}

Output    

           [five, four, one, three, two]

BACK

Filed in: Java Examples

Comments are closed.