code: public static Geometry collectionExtract(Geometry geometry, int dimension) throws ParseException {
          if(geometry == null){
              return null;
          }
          if ((dimension < 1) || (dimension > 3)) {
              throw new IllegalArgumentException(
                      "Dimension out of range (1..3)");
          }
           if (dimension == 1) {
              ArrayList<Point> points = new ArrayList<Point>();
              getPunctualGeometry(points, geometry);
              if (points.isEmpty()) {
                  return geometry.getFactory().buildGeometry(points);
              } else if (points.size() == 1) {
                  return points.get(0);
              } else {
                  return geometry.getFactory().createMultiPoint(points.toArray(new Point[0]));
              }
          } else if (dimension == 2) {
              ArrayList<LineString> lines = new ArrayList<LineString>();
              getLinealGeometry(lines, geometry);
              if (lines.isEmpty()) {
                  return geometry.getFactory().buildGeometry(lines);
              } else if (lines.size() == 1) {
                  return lines.get(0);
              } else {
                  return geometry.getFactory().createMultiLineString(lines.toArray(new LineString[0]));
              }
          } else if (dimension == 3) {
              ArrayList<Polygon> polygones = new ArrayList<Polygon>();
              getArealGeometry(polygones, geometry);
              if (polygones.isEmpty()) {
                  return geometry.getFactory().buildGeometry(polygones);
              } else if (polygones.size() == 1) {
                  return polygones.get(0);
              } else {
                  return geometry.getFactory().createMultiPolygon(polygones.toArray(new Polygon[0]));
              }
          }
          return null;
      }
docs: Given a (multi)geometry, returns a (multi)geometry consisting only of
  elements of the specified type. Sub-geometries that are not the specified
  type are ignored. If there are no sub-geometries of the right type, an
  EMPTY geometry will be returned. Only points, lines and polygons are
  extracted.
  
  @param geometry
  @param dimension
  @return
  @throws org.locationtech.jts.io.ParseException