• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing an array list and keeping the duplicates  RSS feed

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys.

So i want to take a sentence from a user:

Ex user input :

Time is always On Time no Matter the time.

I want to put it in a array-list an print it all back out in alphabetical order.
I have already printed out once in alphabetical order but it gets rid of the duplicates but i want to keep them the second time i print them out.



this is my code so far:



 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already have ArrayList 'r', why not sort and print that?
Look up the docs for Collections.sort().
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need it to print ou like a string

so if i input

Adam adam Bob Bob bob

it will print the same string back as

Adam Bob Bob adam bob bob.

From what i have looked at that collection.sort() prints everything out like
Adam Bob adam bob
1
2
3
4
5
6
7

1 : Adam
2 : Bob
3 : adam
4 : bob
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to print the same string then why sort it at all?
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cause i need it to be in alphabetical order but i want it to keep the duplicates not get rid of them. so i have it printing the string in alphabetical order with no duplicates now i need it to print alphabetical with duplicates.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is alphabetical: Adam Bob adam bob
Do you mean alphabetical ignoring case?
Then you'd have to make a Comparator and use that as an argument to the sort() method.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.sort() does not remove duplicates from the list it is sorting. If you are seeing that happen, it must be something else you're doing in your code. Neither does it have anything to do with display formatting. If you want all your words to print on the same line, look to your use of System.out
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already printed out once in alphabetical order but it gets rid of the duplicates but i want to keep them the second time i print them out.

A Set will always remove duplicates.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i Know that this will get rid of the duplicates. but if i try to print just the array list not the SortedSet<String> tree. I Want to just print the array list out in alphabetical order. The comparison I'm not really sure how that works I'm reading on it now. And the collection.sort(r) Junilu said it was something wrong with the system.out.print i was using an i have no clue what to use to print it.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code you are using with Collections.sort()?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:i need it to print ...

Adam Bob Bob adam bob bob.

From what i have looked at that collection.sort() prints everything out like
Adam Bob adam bob
...
1 : Adam
2 : Bob
3 : adam
4 : bob

Daniel, just to be clear that you understand what I'm saying, your statement above indicates a misunderstanding on your part about what Collections.sort() does. It has nothing to do with the actual printing out of anything. If you wanted to print out each element of the list on the same line and your program is printing out each element like how you show above, it's not the Collection.sort() you need to fix, it's whatever you're doing after sorting the list that's removing duplicates as well as how you're using System.out.println() to display the list elements. That's what you're doing wrong, not Collections.sort().
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks guys I'm sorry about the last post i wasn't awake long enough to be reading your replies sorry an i did figure it out. I Went with the collections.sort() with a for loop. It worked great. Didn't mean to give you guys a had ache. And Again thanks for all the help.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also any advice on good reads to break down the comparing or  Comparato methods i would like cause i don't really understand how that fully works.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your code? I'm not convinced you need a for-loop.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. A few remarks.

Don't use single letter names for your parameters. Describe what they represent. In this case, it represents a list of words, so call it words.

Never use List.get() inside a loop. Instead, use an iterator or an enhanced for-loop:
In this case however, there's a method in the Java API that already does what you want:
Your method should also be named like a verb and describe what it does. you should accept the least specific type you need for it to be able to do it's job. The end result becomes:
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if you want to ignore case when sorting using the aforementioned StringIgnoreCaseComparator:
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or as of Java 8:
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would the ignore case allow me to covet the cas when i print the out using. tolowerCase() or toUpperCase(). case if i try to with the itorator it won let me convert the case an still like a string if i do change the case it look like it just printed a list. now if i used the for loop like i did earlier i can use thes an it works good.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you also want to change all the words before you print them you can use an enhanced for-loop, or a Stream if you're using Java 8.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Never use List.get() inside a loop. Instead, use an iterator or an enhanced for-loop:

In general, follow this heuristic. It produces clearer code.

However... store this advice as "Prefer to use an iterator or an enhanced for-loop to iterate over a List."  Notice that I changed the perspective and removed the reference to List.get() and the use of "Never" -- avoid ever using the word "Never" ...   because there are cases where you do want to use get(i) over an Iterator and next().

List.get() may be preferable to use if you are concerned with performance. The RandomAccess marker interface states that for a typical instance, a RandomAccess object will perform better by using a for-loop and List.get() than a loop that uses an Iterator. The ArrayList class implements RandomAccess so it should actually perform better using get(i). The documentation does not say anything about the enhanced for loop but since that requires an object that is Iterable, I would assume that get(i) is also more performant than a for-each for a typical instance of RandomAccess.

I would balance out the above by saying that you should prefer clarity and simplicity in your code first. Only change your code for performance reasons if you know for sure you have a performance issue. Don't optimize for performance prematurely and make performance-related decisions based on gut feeling or on a whim. Use a profiler to get a quantitative analysis of your code's performance and only make the change to less clear but more performant code if there will be a significant difference that brings you back within your required performance parameters.

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . The documentation does not say anything about the enhanced for loop but since that requires an object that is Iterable, I would assume that get(i) is also more performant than a for-each for a typical instance of RandomAccess. . . .
Since the Java® Language Specification says that the for‑each loop is rather like syntactic sugar hiding an Iterator, your assumption is probably correct.
Something which people often forget about sorting algorithms: Look at List#sort(Comparator) and what it says about implementation details and also what it says about sorting linked lists.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!