First off, here is the code I will be referencing:

package com.airamerica.dataConversion;

import com.airamerica.Airports;
import com.airamerica.Person;
import com.airamerica.Products.Products;
import com.airamerica.Products.Tickets;

public class FindObject {

    public static Airports findAirport(String airportCode, Airports [] airportsArray){
        Airports airport = null;

            for(int j = 0; j < airportsArray.length; j++) {
                if (airportCode.equals(airportsArray[j].getCode())){
                    airport = airportsArray[j];
        return airport;


    public static Tickets findTicket(String productCode, Products[] productArray){
        Tickets product= null;
        String matchCode = null;

            for(int j = 0; j < productArray.length; j++) {
                matchCode = productArray[j].getCode();

                if (matchCode.equals(productCode)){
                    product = (Tickets) productArray[j];
        return product;


    public static Person findPerson(String personCode, Person[] personArray){
        Person person= null;
        String matchCode = null;

            for(int j = 0; j < personArray.length; j++) {
                matchCode = personArray[j].getCode();

                if (matchCode.equals(personCode)){
                    person = personArray[j];
        return person;


With this code a partner and I are finding different objects in our object arrays that we can then you to get information from that object for reporting and other data manipulations. In this FindObject class we have 3 methods that each search by a different code unique to the type of object they are searching through. As an example if we take our Airports object and use it within the findAirport method we can pass in the code we want to find as well as the array of Airports objects created in a different class file. The method will sift through the Array of Airport Objects (airportsArray) and use the .getCode method in the Airport object to find the specific code associated with that object, if it equals the code passed we return that object, easy enough. The question we have is if we can use some sort of generic method to simplify these 3 almost identical (logically identical) methods into one method. We talked about polymorphism in class as well as generic methods and talked to our instructor and we are now left with no ideas. Is this code as efficient as it gets? Can it be better? If so what is something we could do. To be clear, we are not looking for a solution, we don't want code that we could just copy and paste, what we do want is resources we can read that will point us in the right direction, if you do provide code please keep it generic because this is something we both really need and want to learn and we all know giving someone the answer isn't the best way to do that.

The .getCode method is different for each method, they used to be named .getAirportCode, .getPersonCode and .getProductCode but we changed them all to .getCode because one of our ideas was if the method call was the same we could possible make one method for all 3 but we again weren't sure if it'd work or not and after researching and talking to TA's and our professor I don't think it will influence the solution any.

1 Year
Discussion Span
Last Post by overwraith

Changing all the methods names to a single pattern/name is actually a good idea. If you do that, and you have access to the classes of Airports, Products, and Persons you can modify them to all inherit from an interface, and therefore do what you want to do, make a more generic method for accessing them. The following code is actually C#, and I typed it in notepad, so no promises that it actually compiles, is just an example.

  Warning, the following is C#, and has not been tested, I am firing from the hip here... 
  What you want to do is possible. 

  If you possibly can, changing the methods of all associated classes is preferable because
  that means you can access all three from this method provided they all inherit from a 
  common interface. 

//don't know if you have a better name for this one
public interface ICodeExpressable{
    public string getCode();

public static T findObject<T>(String code, T[] arr) where T : ICodeExpressable {
    T result = null;
    String ctCode = null; //your match code, "current" is more readable to me

    //I prefer using 'i' as an 'iterator'
    for(int i = 0; i < arr.length; i++){
        ctCode = arr[i].getCode();

        if(ctCode == code){
            result = arr[i];

    return result;

Interfaces allow you to create generic code in this manner. This doesn't even really have to be 'generic', you can simply replace T with ICodeExpressable like so:
ICodeExpressable result = null;

Interfaces allow you to enforce a 'contract' in implementing classes which allows your code to know for a fact that the code implemeting that interface does in fact have the methods specified in the contract.

I think even if you cannot modify the classes of the associated code, you could possibly use an adapter class which is a design pattern. Don't quote me though, I just started reading a design pattern book.
Click Here

I was thinking about talking about lambadas, since Java 8 now supports them, but I don't think they would be able to solve the issue of the getCodes methods all being named differently, and not inheriting from a common interface. I don't know. If you used lambadas you would probably have to implement three seperate methods for three seperate classes (please correct me if I am wrong, but gently...).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.