Solved

Type safe collection subtract

Posted on 2011-09-16
6
1,422 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 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
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 52
JDBC performance 8 29
Java array 21 86
Delete .class files at every compile 4 21
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

733 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