Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hashset?  RSS feed

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi am aware of hashset ie like adding values and other basic operation?


how can you write a hashset say where it takes 3 stringbuffer or string values and adds only unique values into itself?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like:

?
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't fully understand the question.
One of the properties of a Set is that it never contains any duplicates.
Because a HashSet is a set it will never contain duplicates.
To give a relevant example

After the loop, tokens will contain two strings "ABC" and "DEF", but HashSet does not garantee any order, so the order is unknown.

Another Set is the TreeSet, which only works for objects that are Comparable (like Strings). It will sort the elements in their natural order.
For the example

now tokens again will contain the two strings "ABC" and "DEF", and always in this order.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that HashSet uses hash codes and the equals() method to check for duplicates. So this means that it will work for strings, but it may not work for string buffers -- assuming that you expect two string buffers that contain the same values as being equal.

Henry
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on one of Prasanna's earlier posts, he probably wants the HashSet to only contain the values that are unique.
ie. if you tried to add "ABC", "ABC", "DEF", the set should only contain "DEF".
[ October 31, 2007: Message edited by: Joanne Neal ]
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
but it may not work for string buffers -- assuming that you expect two string buffers that contain the same values as being equal.

Actually, it WILL not work for string buffers.
StringBuffer does not override the equals and hashCode methods, but inherits them from Object instead. Therefore, it will use == for the equality check, and the system hash code.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
Based on one of Prasanna's earlier posts, he probably wants the HashSet to only contain the values that are unique.
ie. if you tried to add "ABC", "ABC", "DEF", the set should only contain "DEF".


Okay, only because I am somewhat bored... BTW, this is *not* complete. Not all the Set methods have been implemented.



And you can test it like so...



Henry
[ October 31, 2007: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!