Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Remove duplicates from ArrayList without API and print onto cvs file

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Thanked 0 Times in 0 Posts

    Default Remove duplicates from ArrayList without API and print onto cvs file

    Hi, I'm trying to remove duplicates from an ArrayList made from tokens (from cvs file) using Scanner method. The file output from my code does not remove duplicates!

    import java.io.*;
    import java.util.*;
    public class removeDuplicates {
        public static void main (String[] args) throws IOException {
            String line = "";
            StringBuilder addToFile  = new StringBuilder();
                Scanner myFile = new Scanner(new File("data.csv"));
                File output = new File("correctedData.cvs");
                while (file.hasNext()){
                    line = file.nextLine();
                    String[] tokens = line.split(",");
                    List<String> List = new ArrayList<>();
                    List<String> newList = new ArrayList<>();
                    for (String x : List){
                        if (!newList.contains(x)) {
                PrintWriter fileSave = new PrintWriter(output);
            } catch (FileNotFoundException e) {



    Duplicates not removed...
    Last edited by nosleep123; October 11th, 2020 at 12:25 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Remove duplicates from ArrayList without API and print onto cvs file

    Can you describe the steps the code needs to take to remove the duplicates? You need to have that algorithm before trying to write the code.

    Note: Having comments in the code can make it easier for anyone reading the code to understand what the code is trying to do. Please add comments to the code that describe what it is doing and why it is doing it.
    For example why does it create a new list in every iteration of the loop?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to remove items of an arraylist
    By crossit in forum Java Theory & Questions
    Replies: 7
    Last Post: December 18th, 2012, 01:14 PM
  2. Replies: 4
    Last Post: December 11th, 2012, 12:12 AM
  3. [SOLVED] Looping through ArrayList to remove elements
    By itispj in forum Collections and Generics
    Replies: 3
    Last Post: October 14th, 2011, 12:42 AM
  4. Duplicates in Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2011, 06:23 PM
  5. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM

Tags for this Thread