• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert Set<String> into Set<Integer>  RSS feed

 
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I hit one issue: convert a set of String into set of Integer.



I tried to convert into array



but I receive : ArrayStoreException.

I want also to sort the set, so I must to convert to array.

I use Java 7.





 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can't convert a String instance into an Integer instance by casting -- either implicit or otherwise. This is also true regardless of it being stored in an array or a set.

You need to parse the String into an Integer. The easiest way to do this is probably with the parseInt() method of the Integer class.

Henry
 
Marshal
Posts: 4054
241
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using the Stream library? There's a handy 'map' function on there which is perfect for converting Collections of things into Collections of different things. Henry has already hinted on a good way to do the String to Integer conversion.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:. . . I use Java 7.
Java8 is over three years old; maybe you shou‍ld upgrade to it. Then you can follow Tim's suggestion about Streams.
You said you wanted to sort your set; are you not aware of different kinds of Java® set?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you made any progress with this problem?
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, yet.

I want to use Java 7, because when I have installed the both versions I had the exceptions.

 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that I'm missing something here and I'm not too sure what as this compiles and runs without any errors.

Granted there is are no values in the array, so the for loop does not print any.

How are you putting values into the sets and what values are you putting into them?

Edit: Extra code comments added
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!