-1

I want to remove frequent patterns from an arraylist. I tried this using hashset but output appears the same.This is the sample set of my arraylist which is residing on a file mytext.txt...How to remove the repeated sequences either before adding into file or after adding into file?

This is the content of my newlist......when i print the contents of newList the following output gets printed without repeated entries...But after writing to a file it shows repeated sequences

    HashSet<String> hash1 = new HashSet<String>();
    hash1.addAll(newList2);
    newList2.clear();
    newList2.addAll(hash1);

    System.out.println(hash1);

output

[arrested, snatching, chain, for, A, escaped, on, from, Friday, man]
[arrested, snatching, chain, for, A, escaped, on, from, Friday, man]
[arrested, snatching, chain, for, A, escaped, on, from, Friday, man]
chopss
  • 771
  • 9
  • 19
  • can you please provide more code for newList2 ? – Keval Trivedi Jun 10 '14 at 09:58
  • See here, for a few suggestions: http://stackoverflow.com/questions/12242083/remove-duplicates-from-list-using-guava – Norbert Radyk Jun 10 '14 at 10:01
  • Do NOT use ArrayList if you do NOT want repetitive entries. Use Set instead. – e.doroskevic Jun 10 '14 at 10:06
  • @E.Doroskevic: It really depends on the use case and values uniqueness is often only one of many data characteristics you might require, i.e. should you require both unique and sorted values sets might not be the best solution. – Norbert Radyk Jun 10 '14 at 10:14
  • @NorbertRadyk I agree. I base my comment on the content of the question. -"How to remove the repeated sequences either before adding into file or after adding into file?" Therefore I recommended to use Set interface to address the given situation. -Regards – e.doroskevic Jun 10 '14 at 10:19

2 Answers2

1

You can try like this..

List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS" ,"Windows mobile");

HashSet<String> listToSet = new HashSet<String>(duplicateList);

List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);

System.out.println(listWithoutDuplicates);
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
learner
  • 365
  • 1
  • 3
  • 16
  • //ArrayList with duplicates String List duplicateList = (List) Arrays.asList("Android" , "Android", "iOS", "Windows mobile"); //Converting ArrayList to HashSet to remove duplicates HashSet listToSet = new HashSet(duplicateList); //Creating Arraylist without duplicate values List listWithoutDuplicates = new ArrayList(listToSet); System.out.println(listWithoutDuplicates); – learner Jun 10 '14 at 10:16
  • 1
    ya it's working thanks...that was due to some mistake in my looping – chopss Jun 19 '14 at 04:40
1

Perhaps you can give us more info on how do you get newlist. One solution I could think of, with the information that you gave us is : add every line in a separate ArrayList, and add those ArrayLists in a HashSet. You would have :
HashSet<ArrayList<string>>.

user3722371
  • 145
  • 6