Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This is part of a homework assignment, a search engine. I am trying to make a class that removes duplicate values from string input (input will be taken from user, using a random String now for test) and stores the tokens/words into an ArrayList to compare them against a String[] of Stop Words (words to remove). I am using List and LinkedHashSet because I want to preserve the order of the words and to remove duplicates. It does remove the duplicate words and preserves order but I can't get it to store the words into an ArrayList, any ideas?

import java.util.Arrays;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;

public class RemoveDuplicateWord {

    public static void main(String[] args) {
        String str = "the search engine that could search";
        removeDupWord(str);
    }

    public static void removeDupWord(String str) {
        List<String> list = Arrays.asList(str.split(" "));
        Set<String> lhs = new LinkedHashSet<String>(list);

        for(String s : lhs) {
            System.out.print(s+" ");
        }
    }
}
share|improve this question
    
If you just need a copy of the data in an ArrayList, then ArrayList<String> tokens = new ArrayList<>(lhs); will do what you want. – teppic yesterday
    
2 comments to help you out : is there a point of having an ArrayList in the end (as compared to either a generic List or your current LinkedHashSet ? If not, then, you may have nothing to do. Secondly, when you had a List and wanted to build a LinkedHashSet, how did you do ? Couldn't you do the same for converting a LinkedHashSet back to a ArrayList ? – GPI yesterday
up vote 0 down vote accepted

I think this is what you want as i understood what u need :/ hope it works

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;
public class RemoveDuplicateWord {
   public static void main(String[] args) {

        String str = "the search engine that could search";
        removeDupWord(str);
    }
public static void removeDupWord(String str) {
    List<String> list = Arrays.asList(str.split(" "));
    Set<String> lhs = new LinkedHashSet<String>(list);
    ArrayList<String> words = new ArrayList<String>();
    for(String s : lhs) {
        words.add(s);
    }
    for(int i=0;i<words.size();i++) {
        System.out.print(words.get(i) + " ");
    }
}
}
share|improve this answer
public static void removeDupWords(String str) {
      List<String> list = Arrays.asList(str.split(" "));
      Set<String> check = new HashSet<String>();
      String output = "";
      for(int i =0; i< list.size(); i++) {
          if(!check.contains(list.get(i)))
              output = output.concat(list.get(i) + " ");
          check.add(list.get(i));
      }
     System.out.println(output);
}
share|improve this answer

just declare another arrayList and add each string in it while iterating your Set.

  public static void removeDupWord(String str) {
            List<String> list = Arrays.asList(str.split(" "));
            Set<String> lhs = new LinkedHashSet<String>(list);
           List<String> result= new ArrayList<String>();
            for(String s : lhs) {
                System.out.print(s+" ");
                result.add(s);
            }
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.