Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Type safe collection subtract

Posted on 2011-09-16
6
Medium Priority
?
1,693 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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