Check if element is unique
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi guys,
I'm trying to readin a txt file (CSV, semicolon separated). Each line will be split in to an array.
Now I want to check the first element which should be an unique ID. But for a reason it does not work.
Output:
Line: 1 First Element of current line: 923739
Line: 2 First Element of current line: 923739
Line: 3 First Element of current line: 924005
Line: 4 First Element of current line: 924007
Line: 5 First Element of current line: 924039
Line: 6 First Element of current line: 924286
Line: 7 First Element of current line: 924327
Line: 8 First Element of current line: 924396
Line: 9 First Element of current line: 924664
Line: 10 First Element of current line: 924718
Line: 11 First Element of current line: 924923
...
I'm trying to readin a txt file (CSV, semicolon separated). Each line will be split in to an array.
Now I want to check the first element which should be an unique ID. But for a reason it does not work.
Output:
Line: 1 First Element of current line: 923739
Line: 2 First Element of current line: 923739
Line: 3 First Element of current line: 924005
Line: 4 First Element of current line: 924007
Line: 5 First Element of current line: 924039
Line: 6 First Element of current line: 924286
Line: 7 First Element of current line: 924327
Line: 8 First Element of current line: 924396
Line: 9 First Element of current line: 924664
Line: 10 First Element of current line: 924718
Line: 11 First Element of current line: 924923
...
posted 8 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Line 52 does not look correct:
Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.
Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.
Robert Sta
Greenhorn
Posts: 5
posted 8 years ago
Hi Jesper,
thank you for your comment.
Well, I changed the line 52 to:
The list should be the collection and the object should be the current the first element of the current line so values[0].
But it still does not work...
In theory I guess that I have to add all available values (read in the whole file) to the list and afterwards the unique check could be done?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Jesper de Jong wrote:Line 52 does not look correct:
Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.
Hi Jesper,
thank you for your comment.
Well, I changed the line 52 to:
The list should be the collection and the object should be the current the first element of the current line so values[0].
But it still does not work...
In theory I guess that I have to add all available values (read in the whole file) to the list and afterwards the unique check could be done?
posted 8 years ago
Please specify: got compilation error? run-time error? incorrect output? (in case of latter what was the output? what were expectations?)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Robert Sta wrote:But it still does not work...
Please specify: got compilation error? run-time error? incorrect output? (in case of latter what was the output? what were expectations?)
Robert Sta
Greenhorn
Posts: 5
posted 8 years ago
Finally I got it!
I changed my code to:
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Robert Sta wrote:
Jesper de Jong wrote:Line 52 does not look correct:
Carefully read the API documentation of Collections.frequency(...) and think about what you're doing on that line.
Hi Jesper,
thank you for your comment.
Well, I changed the line 52 to:
The list should be the collection and the object should be the current the first element of the current line so values[0].
But it still does not work...
In theory I guess that I have to add all available values (read in the whole file) to the list and afterwards the unique check could be done?
Finally I got it!
I changed my code to:
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
My, that is a long method; it should be divided up. I think you can make about six methods out of it.
Declare your buffered reader as a local variable; it doesn't need to be a field. In fact having it as a field is error‑prone because there is a risk of your trying to use a closed Reader.
There is something not quite right about using an int as status. Use an enumerated type instead.
I would have thought that Collections#frequency will run in linear time if applied to a List. Consider instead adding the Strings you are reading to a Set<String> and testing the return value from the add() method. That can run in amortised constant time or, in the best possible situation, plain simple constant time.
If you compare execution times with number of items to read, the whole procedure will probably run in quadratic time with frequency and linear time with Set#add.
Declare your buffered reader as a local variable; it doesn't need to be a field. In fact having it as a field is error‑prone because there is a risk of your trying to use a closed Reader.
There is something not quite right about using an int as status. Use an enumerated type instead.
I would have thought that Collections#frequency will run in linear time if applied to a List. Consider instead adding the Strings you are reading to a Set<String> and testing the return value from the add() method. That can run in amortised constant time or, in the best possible situation, plain simple constant time.
If you compare execution times with number of items to read, the whole procedure will probably run in quadratic time with frequency and linear time with Set#add.
| And inside of my fortune cookie was this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








