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

How can I combine these two methods into one?

WBOY
Release: 2024-02-22 13:28:06
forward
1117 people have browsed it

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");
Copy after login

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");
Copy after login

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);
      }
   }
}
Copy after login

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);
      }
   }
}
Copy after login

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);
      }
   }
}
Copy after login

I tried using generics

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

But this:

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

    comparator.comparing(t::getfromdat);
    Copy after login
  • 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);
    Copy after login
  • 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);
    }
}
Copy after login

The call is as follows:

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

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

public interface dated {
  localdate getdate();
} 

public class classb implements dated {
  ...
}

public class classc implements dated {
  ...
}
Copy after login

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);
      }
   }
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template