Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:04:14+00:00 2026-06-03T19:04:14+00:00

I have a method <T> void f (Collection<T> c, T t) Alas, sometimes I

  • 0

I have a method

<T> void f (Collection<T> c, T t)

Alas, sometimes I need to pass as the first arg not Collection but Collection<P> where P is a precursor for T, i.e., there is a P getter method returning a T.
So how do I modify my method to work with both Collection<P> and Collection<T> (in the latter case the getter is the identity)?

E.g.,

<T> boolean f (Collection<T> c, T t) {
  for (T x : c) if (x.equals(t)) return true;
  return false;
}
class Z { String a, b; }

I want to be able to use f to search Collection<Z> by either a or b:

f(Collection<Z> c1 ???searching by a???, "foo")
f(Collection<Z> c2 ???searching by b???, "bar")

I have no control over the implementation of T and P; in particular, I cannot make P inherit from T (and I don’t want to: as in Z above, I might want to search by different fields at different times).

I do not want to create an intermediate Collection<T> by mapping.

I want something like the common :key Lisp keyword argument.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:04:15+00:00Added an answer on June 3, 2026 at 7:04 pm

    How about using something like a interface for the predicate to check on equality. An example would be:

    interface Predicate<T> {
         public boolean test(T element);
    }
    

    and then use the following function:

     public <T> boolean f(Collection<T> c, Predicate<T> p) {
         for (T x : c) if (p.test(x)) return true;
         return false;
     }
    

    Then you could call it the following ways:

    Collection<T> ts;
    f(ts, new Predicate<T> () { 
                public boolean test(T element) { 
                          return element.equals("foo"); 
                }
     });
    
    Collection<P> ps;
    f(ps, new Predicate<P> () { 
                public boolean test(P element) { 
                          return element.getT().equals("foo"); 
                }
    });
    

    While this syntax is quite verbose it should fit your question. It will however get prettier in Java 8 when lambda functions will arrive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
So I have this method: protected void collectSelectedItems(ListSelectionModel lsm, Collection<? super MyItemClass> result) {
I have a method -(void)myMethod:(MyObject*)obj And I am detaching a new thread [NSThread detachNewThreadSelector:@selector(myMethod)
I currently have this method: -(void)seperateGuides { DLog(GetGuideListCount: %i, [[appDelegate getGuideList] count]); columnOneArray =
I have this method: public void testJSNI2(){ String x = test; } I can
I have the following method: public void PutFile(string ID, Stream content) { try {
I have this method: internal static void AssignNewArrayItem(ref Xml.CLASSNAME[] Items, Xml.CLASSNAME Item) { if
I have a private variable in my class: BOOL isEnabled; and a method: -(void)
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have a method which I would like to invoke asynchronously: void Foo() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.