2
\$\begingroup\$

This is the most basic code to join two tables in a database. For tables it uses a simple list. Solved this using brute force, sorting and using hashtable.

final class SportsMan {

    private final String name;
    private final int rank;
    private final String sport;

    public SportsMan (String name, int rank, String sport)  {
        this.name = name;
        this.rank = rank;
        this.sport = sport;
    }

    public String getName() {
        return name;
    }

    public int getRank() {
        return rank;
    }

    public String getSport() {
        return sport;
    }
}


final class Sport {

    private final String sport;
    private final int numberOfPlayers;

    public Sport(String sport, int numberOfPlayers) {
        this.sport = sport;
        this.numberOfPlayers = numberOfPlayers;
    }

    public String getSport()  {
        return sport;
    }

    public int getNumberOfPlayers() {
        return numberOfPlayers;
    }
}


final class Result {

    private final String name;
    private final int rank;
    private final String sport;
    private final int numberOfPlayers;

    public Result (String name, int rank, String sport, int numberOfPlayers) {
        this.name = name;
        this.rank = rank;
        this.sport = sport;
        this.numberOfPlayers = numberOfPlayers;
    }

    public String getName() {
        return name;
    }

    public int getRank() {
        return rank;
    }

    public String getSport() {
        return sport;
    }

    public int getNumberOfPlayers() {
        return numberOfPlayers;
    }

}

public final class Joins {

    private Joins () {}

    public static List<Result> innerJoinBruteForce(List<SportsMan> sportsMen, List<Sport> sportList) {
        final List<Result> resultSet = new ArrayList<Result>();
        for (SportsMan sportsMan : sportsMen) {
            for (Sport sport : sportList) {
                if (sportsMan.getSport().equals(sport.getSport())) {
                    resultSet.add(new Result(sportsMan.getName(), sportsMan.getRank(), sport.getSport(), sport.getNumberOfPlayers()));
                }
            }
        }
        return resultSet;
    }


    public class SportsMenComparator implements Comparator<SportsMan> {
        @Override
        public int compare(SportsMan s1, SportsMan s2) {
            return s1.getSport().compareTo(s2.getSport());
        }
     }

    public class SportComparator implements Comparator<Sport> {
        @Override
        public int compare(Sport sport1, Sport sport2) {
            return sport1.getSport().compareTo(sport2.getSport());
        }
    }

    public static List<Result> innerJoinSort(List<SportsMan> sportsMansList, List<Sport> sportList) {
        List<Result> result = new ArrayList<Result>();

        Collections.sort(sportsMansList, new Joins().new SportsMenComparator());
        Collections.sort(sportList, new Joins().new SportComparator());

        int sportsManCtr = 0;
        int sportsCtr = 0; 

        while (sportsManCtr < sportsMansList.size() && sportsCtr < sportList.size()) {
            SportsMan sportsMan =  sportsMansList.get(sportsManCtr); 
            Sport sport = sportList.get(sportsCtr);
            if (sportsMan.getSport().compareTo(sport.getSport()) > 0) {
                sportsCtr++;
            } else if (sportsMan.getSport().compareTo(sport.getSport()) < 0) {
                sportsManCtr++;
            } else {
                result.add(new Result(sportsMan.getName(), sportsMan.getRank(), sport.getSport(), sport.getNumberOfPlayers()));
                sportsManCtr++;
                sportsCtr++;
            }
        }

        return result;
    }


    public static  List<Result> innerJoinHash(List<SportsMan> sportsManList, List<Sport> sportList) {
        List<Result> result = null;

        Map hashMap;
        if (sportsManList.size() < sportList.size()) {
            hashMap = new HashMap();
            for (SportsMan sportsman :  sportsManList) {
                hashMap.put(sportsman.getSport(), sportsman);
            }
            for (Sport sport : sportList) {
                if (hashMap.containsKey(sport.getSport())) {
                    SportsMan sportsMan = (SportsMan) hashMap.get(sport.getSport());
                    result.add(new Result(sportsMan.getName(), sportsMan.getRank(), sportsMan.getSport(), sportsMan.getRank()));
                }
            }

        } else {
            hashMap = new HashMap();
            for (Sport sport :  sportList) {
                hashMap.put(sport.getSport(), sport);
            }
            for (SportsMan sportsMan : sportsManList) {
                if (hashMap.containsKey(sportsMan.getSport())) {
                    Sport sport = (Sport) hashMap.get(sportsMan.getSport());
                    result.add(new Result(sportsMan.getName(), sportsMan.getRank(), sportsMan.getSport(), sportsMan.getRank()));
                }
            }
        }

        return result;
    }



    public static void main(String[] args) {

       List<SportsMan> sportsMenList = new ArrayList<SportsMan>();
       sportsMenList.add(new SportsMan("sachin", 1, "cricket"));
       sportsMenList.add(new SportsMan("nadal", 1, "tennis"));
       sportsMenList.add(new SportsMan("tiger", 1, "golf"));
       sportsMenList.add(new SportsMan("pele", 1, "soccer"));


       List<Sport> sportList = new ArrayList<Sport>();
       sportList.add(new Sport("cricket", 11));
       sportList.add(new Sport("tennis", 2));
       sportList.add(new Sport("golf", 1));


       List<Result> resultSet1 = innerJoinBruteForce(sportsMenList, sportList);
       for (Result result : resultSet1) {
           System.out.print(result.getSport() + " : ");
       }


       List<Result> resultSet2 = innerJoinSort(sportsMenList, sportList);
       for (Result result : resultSet2) {
           System.out.print(result.getSport() + " : ");
       }

       List<Result> resultSet3 = innerJoinSort(sportsMenList, sportList);
       for (Result result : resultSet3) {
           System.out.print(result.getSport() + " : ");
       }

    }
}
\$\endgroup\$
5
\$\begingroup\$

Right now you aren't using generics:

Map hashMap; // no generics :o
Map<String, Object> hashMap; // generics :D

The variable result can only be null in the function innerJoinHash():

result.add(new Result(sportsMan.getName(), sportsMan.getRank(), sportsMan.getSport(), sportsMan.getRank()));

This is because you are initializing result to null:

List<Result> result = null;

You should initiate the list right away:

List<Result> result = new ArrayList<Result>();

The value of the local variable sport is not used, and therefore can be removed:

Sport sport = (Sport) hashMap.get(sportsMan.getSport());
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.