Optimize headMap and tailMap.