private List < Row > join ( List < Row > leftRows , String leftColumn , String rightTable , List < Row > rightRows , String rightColumn ) { List < Row > result = new LinkedList < Row > ( ) ; RowComparator leftComparator = new RowComparator ( new String [ ] { leftColumn } ) ; RowComparator rightComparator = new RowComparator ( new String [ ] { rightColumn } ) ; Collections . sort ( leftRows , leftComparator ) ; Collections . sort ( rightRows , rightComparator ) ; ListIterator < Row > rightIterator = rightRows . listIterator ( ) ; Row rightRow = rightIterator . hasNext ( ) ? rightIterator . next ( ) : null ; for ( Row leftRow : leftRows ) { Integer leftValue = leftRow . getInteger ( leftColumn ) ; boolean match = false ; while ( rightRow != null ) { Integer rightValue = rightRow . getInteger ( rightColumn ) ; int comparison = leftValue . compareTo ( rightValue ) ; if ( comparison == 0 ) { match = true ; break ; } if ( comparison < 0 ) { if ( rightIterator . hasPrevious ( ) ) { rightRow = rightIterator . previous ( ) ; } break ; } rightRow = rightIterator . next ( ) ; } if ( match && rightRow != null ) { Map < String , Object > newMap = new HashMap < String , Object > ( ( ( MapRow ) leftRow ) . getMap ( ) ) ; for ( Entry < String , Object > entry : ( ( MapRow ) rightRow ) . getMap ( ) . entrySet ( ) ) { String key = entry . getKey ( ) ; if ( newMap . containsKey ( key ) ) { key = rightTable + "." + key ; } newMap . put ( key , entry . getValue ( ) ) ; } result . add ( new MapRow ( newMap ) ) ; } } return result ; }
Very basic implementation of an inner join between two result sets .