• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Union of Inputted Values

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an enum class called IndicatorT that looks like this:


and now I'm trying to implement a class called AttributeT which looks like


and the purpose of the AttributeT method should be to set name to be 'attribName' and set s to be equal to the union of all the inputted indicators however that line is giving me an error that says "Cannot infer type arguments for HashSet<>". I looked around for similar errors on the internet but I haven't been able to solve this problem. Any advice is appreciated!
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are already receiving a Set as an argument, you could simple pass that in to the HashSet constructor:
    this.s = new HashSet<>(indicators);
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line:

is not a Set<IndicatorT>. Check your IDE for the real type.

Why not simply do:

But what is generic about your class AttributeT<T>?Where is this <T> used?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need a Set of enum values, then it's strongly recommended that you use an EnumSet. According to Guide to EnumSet:

That tutorial wrote:As a rule of thumb, EnumSet should always be preferred over any other Set implementation when we are storing enum values.



If you do that, then line 11 becomes, well as the tutorial says:

we can create an EnumSet by copying all the elements from another EnumSet

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you need a copy of that Set?
You can create the Set with this method, which has the advantage that your Set will contain every element in the original enum.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why would you need a copy of that Set?



A defensive copy?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. Does EnumSet have a method to create an immutable instance? It doesn't seem to.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic