?
Solved

Type safe collection subtract

Posted on 2011-09-16
6
Medium Priority
?
1,635 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

650 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