N.N.
N.N.

Reputation: 15

How to get an Arraylist<String> after using List and LinkedHashSet to remove duplicate values from a string in Java

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+" ");
        }
    }
}

Upvotes: 0

Views: 289

Answers (3)

user2532400
user2532400

Reputation: 11

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);
}

Upvotes: 1

Andr&#233; Abboud
Andr&#233; Abboud

Reputation: 2030

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) + " ");
    }
}
}

Upvotes: 0

scav
scav

Reputation: 36

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);
            }
        }

Upvotes: 0

Related Questions