code: public static double squaredDistance(SparseArray x, SparseArray y) {
          Iterator<SparseArray.Entry> it1 = x.iterator();
          Iterator<SparseArray.Entry> it2 = y.iterator();
          SparseArray.Entry e1 = it1.hasNext() ? it1.next() : null;
          SparseArray.Entry e2 = it2.hasNext() ? it2.next() : null;
  
          double sum = 0.0;
          while (e1 != null && e2 != null) {
              if (e1.i == e2.i) {
                  sum += Math.sqr(e1.x - e2.x);
                  e1 = it1.hasNext() ? it1.next() : null;
                  e2 = it2.hasNext() ? it2.next() : null;
              } else if (e1.i > e2.i) {
                  sum += Math.sqr(e2.x);
                  e2 = it2.hasNext() ? it2.next() : null;
              } else {
                  sum += Math.sqr(e1.x);
                  e1 = it1.hasNext() ? it1.next() : null;
              }
          }
          
          while (it1.hasNext()) {
              sum += Math.sqr(it1.next().x);
          }
  
          while (it2.hasNext()) {
              sum += Math.sqr(it2.next().x);
          }
          
          return sum;
      }
docs: The Euclidean distance.