Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java: Generics: Generalized Collection

Posted on 2011-05-11
11
Medium Priority
?
305 Views
Last Modified: 2012-05-11
Have a class that wraps a List object, providing some additional functionality. Sometimes I need a class with a List<String>, sometimes with a List<Integer> Would like to generalize the type of List to avoid having multiple classes or multiple List variables.  Is there a way to define a single List variable within that class so that a factory class could return that class with it having created a List<String> in one case, and a List<Integer> in another case?  And, have an add() method in the wrapper class that could be called with a String when its List was List<String>, but the same method could be called with an Integer when the List was created as List<Integer>?  I've tried things like List<T> and List<?>, but never seem to quite get it to compile.  I understand generics when which using fixed designations like <String> and <Integer>, but using <T> and <?> still baffles me.  
0
Comment
Question by:whandley
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 35744019
>>but using <T> and <?> still baffles me

when declaring, you can't use undefined general type, you need to specify which type the list is going to contain

List<String> strList = new YourList<String>() ; should work
0
 

Author Comment

by:whandley
ID: 35744031
Savant:  Bounded doesn't help me, Integer is not a type of String nor vice versa.  Those aren't my real types, but mine aren't related either.

Genius:  Sorry, you missed the point of my question.
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 92

Expert Comment

by:objects
ID: 35744040
Well if you want to store Integers *and* Strings in the same collection then generics aren't really going to help you. Thats not what they are intended for.
0
 
LVL 92

Expert Comment

by:objects
ID: 35744147
though your question seems to imply that you're list will have *either* Integer or String which you would achieve with t bounded type parameters. Perhaps you could clarify your question a little.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 35744416
You're on the right track with List<T>. Check out the following...

 
import java.util.ArrayList;
import java.util.List;

public class GenericListWrapper<T>
{
    private List<T> wrappedList;

    public GenericListWrapper()
    {
        this.wrappedList = new ArrayList<T>();
    }

    
    public GenericListWrapper(List<T> wrappedList)
    {
        this.wrappedList = wrappedList;
    }

    public List<T> getWrappedList()
    {
        return wrappedList;
    }
    
    public void add(T element)
    {
        wrappedList.add(element);
    }
}

Open in new window


 
public class GenericListWrapperMain
{
    public static void main(String[] args)
    {
        GenericListWrapper<String> wrapper = new GenericListWrapper<String>();
        
        wrapper.add("a String");
    }
}

Open in new window


Notice that on line 4 of GenericListWrapper, T is declared as the placeholder for the eventual type of the generic class. After that you can use T to represent the type anywhere in that class. In particular, note line 24 where T is used, not between <  > to represent the type of argument to the add method.

Then in GenericListWrapperMain, I just use the GenericListWrapper class like you do now with List's, Map's, etc.

Hope this helps...
0
 
LVL 92

Expert Comment

by:objects
ID: 35744471
Thats what I suggested above but whandley said it didn't help. Bit unclear exactly what is needed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35744489
As mccarl says, you would take the type T with which you initialise the class which contains the List, so it becomes List<T>. The factory would of course have to be told which type you wanted, so some method call including the type would be necessary when asking for an instance
0
 
LVL 36

Expert Comment

by:mccarl
ID: 35744500
@objects: I think pointing out info about "bounded" type arguments in particular, may be what threw whandley off what you were trying to suggest. But who knows? ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 35744802
yes its weird, the Box example on that page looked like just what is needed.
0
 

Author Closing Comment

by:whandley
ID: 35745781
Thanks, exactly what I needed.  And thanks for taking the time to explain how this works...so necessary for one to make progress with this, and sadly, so often omitted in solutions.
0

Featured Post

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.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.
Suggested Courses
Course of the Month20 days, 22 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