Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 3457
493
Android 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);
 
Saloon Keeper
Posts: 4355
163
  • 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: 26463
81
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: 72423
315
  • 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: 26463
81
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: 72423
315
  • 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.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic