Table of Contents
Question content
Workaround
Home Java How can I combine these two methods into one?

How can I combine these two methods into one?

Feb 22, 2024 pm 01:28 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)