code: private Collection<Geometry> unionAdjacentPolygons(Collection<Geometry> list) {
          UnaryUnionOp op = new UnaryUnionOp(list);
          Geometry result = op.union();
          if (result.getNumGeometries() < list.size()) {
              list.clear();
              for (int i = 0; i < result.getNumGeometries(); i++) {
                  list.add(result.getGeometryN(i));
              }
          }
          return list;
      }
docs: Union adjacent polygons to make an invalid MultiPolygon valid