• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a HashSet that contains arrays of Strings  RSS feed

 
Ivan Bosnjak
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm having trouble reading a hashSet I created. This is the way it is defined and used:




after that I would like to print this data and I'm trying with this code:



And of course it doesn0t work, I get something like:

[Ljava.lang.String;@87c268,

How do I print my hashSet?

Thanks in advance!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this isn't a very useful data structure: HashSet relies on the objects you insert to override equals() and hashCode() in a meaningful way, but arrays don't do this. If you have two String arrays containing the exact same Strings in the exact same order, the HashSet<String> will not eliminate one of them. This is because the equals() method of any array returns false() if called on any other array, regardless of contents.

To print the contents, you're going to have to render each individual item somehow. You might do this with another loop inside the first, or you could use java.util.Arrays.asList() to turn the String[] into a List<String>, which will, if printed, display the Strings in the array.
 
Rob Spoor
Sheriff
Posts: 21094
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You are adding the exact same instance of the string array to the set. Because it is a set, there will always be only one element.
2) See Ernest's comment; this also solves issue 1.
3) You can use Arrays.toString(...) to nicely print an array.
 
Ivan Bosnjak
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
1) You are adding the exact same instance of the string array to the set. Because it is a set, there will always be only one element.
2) See Ernest's comment; this also solves issue 1.
3) You can use Arrays.toString(...) to nicely print an array.



Ok, I was in a hurry yesterday so I'll try to explain my problem again.

I'm trying to read some data from text files(no problems with this), specifically first name and last name. I want to put this data in a sort of collection that cannot contain duplicates, and that's why I chose hashSet. So if I come across a same first name and last name more than once I don't want it to be added in the collection. Afterwards I need to print this data.
Can you give me a simple solution to do this?

Thanks!
 
Rob Spoor
Sheriff
Posts: 21094
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet uses equals() to determine whether or not an object is already present. You just need an object who's equals method will return true if the object to compare to has the same elements.
If you create a new String[] array in each loop, then wrap it in a list by calling Arrays.asList(), this should do what you need. The list returned by Arrays.asList() has an equals method that performs the following checks:
- return false if the object to compare with is not a list
- iterate through both lists and check if they have the same elements in the same order

Seeing as both will be the case this should work just fine.

Because you will add lists you won't need to do any fancy stuff to get a nice printed version; the list will print itself as [element0, element1].
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!