Naruto Sempai
Naruto Sempai

Reputation: 7181

Generalizing parameters in a java method

So I have 2 methods:

private void updateJobType(, Boolean addTarget, JobType target) {
 if (addTarget == Boolean.TRUE) {
        jobTypes.add(target);
   } else {
        jobTypes.remove(target);
  }
 }

private void updateSearchStatus( Boolean addTarget, JobStatus target) {
  if (addTarget == Boolean.TRUE) {
     searchStatus.add(target);
  } else {
     searchStatus.remove(target);
  }

Which I would like to generalize into something like:

private void updateList(Boolean addItem, Object item, List<Object> list) {
       if (addItem == Boolean.TRUE) {
          list.add(item);
        } else {
          list.remove(item);
        }
 }

but when I call:

updateList(Boolean.True, JobType.FULL_TIME, jobTypes);

I get an error:

updateList() in JobSearch cannot be applied to:

Expected Parameters: "java.util.list"

Actual Arguments: jobTypes (java...domain.job.JobType>)

Is this not possible conceptually in Java? What am I missing? I've read Q&A that seemed similar but none answered this question completely for me. Thanks in advance. Cheers!

Upvotes: 0

Views: 2385

Answers (1)

A.H.
A.H.

Reputation: 66243

This one should do the trick:

private <T> void updateList(Boolean addItem, T item, java.util.List<T> list) { //...

Upvotes: 2

Related Questions