Home  >  Article  >  How can I combine these two methods into one?

How can I combine these two methods into one?

WBOY
WBOYforward
2024-02-22 13:28:061070browse

php editor Apple will answer your question in Java: How to combine two methods into one? In Java programming, sometimes we need to merge two methods into a more efficient method to improve the readability and execution efficiency of the code. By merging methods, duplicate code can be reduced, program structure simplified, and code reusability improved. Next, we will introduce specific operating steps and precautions to help you quickly implement method merging and optimize Java program design.

Question content

I have two very similar methods. So I want to reduce it to a method but I don't know how.

Use like this:

mapclassbtoclassa("a list o classb objs", "a list of classa objs");
mapclassctoclassa("a list o classc objs", "a list of classa objs");

I want to change to:

mapclasstoclassa("a list o classb objs", "a list of classa objs");
mapclasstoclassa("a list o classc objs", "a list of classa objs");

Method 1b => a

public void mapclassbtoclassa(list<classb> b, list<classa> a){
   comparator<classb> sortfunc = comparator.comparing(classb::getdate);
   collections.sort(b, sortfunc);
   for (classa aobj : a){
      localdate datea = aobj.getdate();
      classb bobjtouse = null;
      for (classb bobj : b){
         localdate dateb = bobj.getdate();
         if (dateb.isafter(datea)){
            break;
         }
         else {
            bobjtouse = bobj; 
         } 
      }
      if (bobjtouse != null){
         aobj.setclassb(bobjtouse);
      }
   }
}

Method 2c => a

public void mapclassctoclassa(list<classc> c, list<classa> a){
   comparator<classc> sortfunc = comparator.comparing(classc::getdate);
   collections.sort(c, sortfunc);
   for (classa aobj : a){
      localdate datea = aobj.getdate();
      classc cobjtouse = null;
      for (classc cobj : c){
         localdate datec = cobj.getdate();
         if (datec.isafter(datea)){
            break;
         }
         else {
            cobjtouse = cobj; 
         } 
      }
      if (cobjtouse != null){
         aobj.setclassc(cobjtouse);
      }
   }
}

I want to do something similar:

public void mapclasstoclassa(list< **xx** > t, list<classa> a){
   comparator< **xx** > sortfunc = comparator.comparing( **xx** ::getdate);
   collections.sort( t , sortfunc);
   for (classa aobj : a){
      localdate datea = aobj.getdate();
      **xx** objtouse = null;
      for (**xx** obj : t){
         localdate datet = obj.getdate();
         if (datet.isafter(datea)){
            break;
         }
         else {
            objtouse = bobj; 
         } 
      }
      if (objtouse != null){
         aobj.setclassxxx(objtouse);
      }
   }
}

I tried using generics

public <t> void maptoarende(list<t> t, list<classa> a){
        ...
           t objtouse = null;

But this:

  • I can't execute the comparator because this doesn't work

    comparator.comparing(t::getfromdat);
  • Instead of a call to obj.getdate(), here I found a solution using reflection, but people advise against it. But it works. what do I do?

    Method m = obj.getClass().getMethod("getDate");
    LocalDate dateT = (LocalDate) m.invoke(obj);
  • I don’t know how to call aobj.setclassxxx() because the method names of classb and classc are different

I've read about lambdas but am having some difficulty understanding how to use them.

Workaround

If you cannot add a common interface to these classes, you can add some functions to the method parameters:

public <t> void mapclasstoclassa(list<t> t, list<classa> a,
                                 function<t, localdate> dategetter,
                                 biconsumer<classa, t> setter) {

    comparator<t> sortfunc = comparator.comparing(dategetter);
    collections.sort(t, sortfunc);
    for (classa aobj : a) {
        localdate datea = aobj.getdate();
        t ttouse = null;
        for (t tobj : t){
            localdate datet = dategetter.apply(tobj);
            if (datet.isafter(datea)){
                break;
            }
            ttouse = tobj; 
        }
        setter.accept(aobj, tobjtouse);
    }
}

The call is as follows:

mapclasstoclassa(b, a, classb::getdate, classa::setclassb);
mapclasstoclassa(c, a, classc::getdate, classa::setclassc);

That’s what interfaces are for, isn’t it?

public interface dated {
  localdate getdate();
} 

public class classb implements dated {
  ...
}

public class classc implements dated {
  ...
}

For setters, you can use instanceof.

public void mapClassBToClassA(List<? extends Dated> b, List<ClassA> a){
   Comparator<Dated> sortFunc = Comparator.comparing(Dated::getDate);
   Collections.sort(b, sortFunc);
   for (ClassA aObj : a){
      LocalDate dateA = aObj.getDate();
      Dated datedToUse = null;
      for (Dated bObj : b){
         LocalDate dateB = bObj.getDate();
         if (dateB.isAfter(dateA)){
            break;
         }
         else {
            datedToUse = bObj; 
         } 
      }
      if (datedToUse instanceOf ClassB bObjToUse) {
         aObj.setClassB(bObjToUse);
      }
      else if (datedToUse instanceOf ClassC cObjToUse) {
         aObj.setClassC(cObjToUse);
      }
   }
}

The above is the detailed content of How can I combine these two methods into one?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete