Solved

Type safe collection subtract

Posted on 2011-09-16
6
1,334 Views
Last Modified: 2012-05-12
Collection<String> result = CollectionUtils.subtract(List1, List2);
I get the following warning for above code:
Type safety: The expression of type Collection needs unchecked conversion to conform to Collection<String>

How can I make this line of code type safe?
What would be the correct syntax to call an object's static function, and give that object a specific template type?

FYI:
List1 and List2 are of type Set<String>
Set<String> List1 = new HashSet<String>();
Set<String> List2 = new HashSet<String>();
0
Comment
Question by:Axter
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36548549
They might be suggesting the below but i'd need to see api docs for 'CollectionUtils'
Collection<String> result = (Collection<String>)CollectionUtils.subtract(List1, List2);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36548564
For your purposes, you might as well do the following probably
Set<String> result = (Set<String>)CollectionUtils.subtract(List1, List2);

Open in new window

0
 
LVL 4

Accepted Solution

by:
stachenov earned 500 total points
ID: 36548625
The problem here is that you're trying to call an old-style non-generic method passing it a new-style generic collection. For compatibility reasons it is allowed, but since type checking is impossible, it results in a warning. If there were a type-safe way to write that, it would probably be an error rather than a warning.

Still, instead of suppressing that warning everywhere, you may wish to isolate that problem inside a separate wrapper method in one of your classes, like this:
 
@SuppressWarnings("unchecked")
    public static <T> Collection<T> mySubtract(Collection<T> a, Collection<T> b) {
        return subtract(a, b);
    }

Open in new window

Now, you may call the new mySubtract anywhere you want without having to suppress that annoying warning in any place but this function.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 30

Author Comment

by:Axter
ID: 36550185
CEHJ,
That neither one worked to remove the warning.
0
 
LVL 30

Author Closing Comment

by:Axter
ID: 36550202
Thanks.
The following version worked form me.
      @SuppressWarnings("unchecked")
      public static <T> Collection<T> subtract(Collection<T> a, Collection<T> b)
      {
            return CollectionUtils.subtract(a, b);
      }

And I changed the calling code to the following:
Collection<String> result = subtract(List1, List2);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36550245
Personally i wouldn't worry about warnings. In a sense, it's better not to hide them - it simply makes your code (or theirs) seem stronger than it really is, which is misleading
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now