Solved

Type safe collection subtract

Posted on 2011-09-16
6
1,482 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

688 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