• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

autoboxing is not working for arrays

 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought primitive types are supposed to be autoboxed
error: The method addAll(Collection<? extends Character>) in the type Set<Character> is not applicable for the arguments (char[])
this works but i would rather do it on one line without a for-loop, anyone know a better way?
 
Sheriff
Posts: 17125
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char[] is itself an array type, not a primitive type, so autoboxing doesn't apply.  You could probably stream str.toCharArray() and map it to a collection of Character.
 
S Fox
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't know how to use streams and collectors very good, can't figure out how to do that
 
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also new to Streams and collectors. I did a little looking around, and found that you can use String.chars() to get a stream of integer values corresponding to each character. Hitting the Stack Overflow thread Java collections convert a string to a list of characters showed me how to map them to Character objects. You can then collect them.

This should give you a Set<Character> for your key set:

I didn't have time to look deeper, but I hope that helps for now.
 
Marshal
Posts: 76452
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you trying to add anything to the key set of a Map? You shouldn't change that set's state. It may even be implemented in a read-only fashion. The ConcurrentHashMap#newKeySet(int) method seems to create a new Set, but it isn't a general‑purpose Set. I would suggest you familiarise yourself with general‑purpose Sets before you try using special‑purpose Sets. Two Java™ Tutorial links:- 1 2. If you go through that “trail”, you will find descriptions of Streams used on Collections. This is one way you might use a Stream:-Note that the whole expression is intended to create a Set<Character>, so the result is declared with that type. The Stream starts from the String literal, using its chars() method; this returns an IntStream because there is no such thing as a CharStream. So the IntStream processes ints produced by ero‑extending each char.
You can't change an IntStream to a CharStream, but you an change it to a Stream<T> with its mapToObj() method, which takes a IntFunction as its parameter. I could have written the IntFunction<Charater> as an anonymous class:-You need the cast because Character#valueOf() doesn't take an int as its parameter.
...but IntFunction is a Functional Interface, so that anonymous class can be simplified. The compiler “knows” you want an IntFunction, so we can delete some of the code:-We also know which method we are going to implement (it only has one method), so we can delete the method name etc.:-Who needs return or {} braces or a semicolon?We know it's an int, so (int...) goes, and we use the -> (arrow token) to join the two parts together, and voilà!After line 2, we have a Stream<Character> and that has a collect() method, which takes a Collector as its parameter. Fortunately you don't have to write your own Collector because the Collectors class has methods to supply a ready‑made Collector, like this method which provides just what you want to create an ordinary Set. Done
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that mapToObj() is a badly‑spelt name for a method, but not as bad as my spelling of letterSet. How many people remember LetraSet®? I do, so I couldn't resist the temptation to introduce that bad spelling 
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic