1

I've looked at multiple different answer and questions but none seem to be giving the result I would like. my issue is that I have a .txt file with 426458 states (disregarding the rules) of noughts and crosses with the format shown below.

[E][E][E]
[E][E][E]
[E][E][E]

[X][X][X]
[O][O][O]
[E][E][E]

I load the file using this:

Scanner sc = new Scanner(new File("src/AllPlayerOneStates.txt"));
    sc.useDelimiter("\r");
    List<String> lines = new ArrayList<String>();
    while (sc.hasNextLine()) {
      lines.add(sc.next());
    }

This creates a list with every element being a state then I would like to create a new list without duplicate states.

I've tried using StringBuilder to simply output all the unique states but it gives me a number much less than expected aswell as looping through each element and comparing it to all the rest but I may have done it wrong as it told me the amount of unique states was larger than 426458. I'm relatively new to programming.

3
  • "This creates an array ..." The code you show does not use arrays at all. Commented Mar 1, 2017 at 20:03
  • sorry about that, I thought they were a similar concept Commented Mar 1, 2017 at 20:12
  • They are similar, in some ways. But "array" is a specific thing, and your code doesn't use that specific thing. Words matter, especially words with exact definitions in the JLS. Commented Mar 1, 2017 at 20:14

2 Answers 2

3

This creates an array with every element being a state then I would like to create a new array without duplicate states.

Try the following way:

Set<String> set = new HashSet<String>(lines);
List<String> newList = new ArrayList<String>(set);

Here newList is your new List of states that doesn't contain duplicates.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Set instead of List and it will remove the duplicates itself, e.g.:

Set<String> lines = new HashSet<String>();
while (sc.hasNextLine()) {
  lines.add(sc.next());
}

Once this while loop finishes, lines.size() will give you number of unique rules.

If you want to preserve the order, you can use LinkedHashSet instead of HashSet.

4 Comments

it must be the way im formatting my text file as it gives me the same result of only 28 unique states when there should be much more
try printing out each element as it gets added into the set, that might give you more idea on what's being added..
You will only get the right result if equals and hashCode correctly compare states. Does one line of input really correspond to a single state?
this is what the hash list looks like after being read into a file pastebin.com/4rEACmqS im not entirely sure what is going wrong or why the 3rd or 4th element is not in the same format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.