public static List<Geometry> makeBatches(Collection<Geometry> geometries, boolean useLods) {
        ArrayList<Geometry> retVal = new ArrayList<>();
        HashMap<Material, List<Geometry>> matToGeom = new HashMap<>();

        for (Geometry geom : geometries) {
            List<Geometry> outList = matToGeom.get(geom.getMaterial());
            if (outList == null) {
                //trying to compare materials with the contentEquals method 
                for (Material mat : matToGeom.keySet()) {
                    if (geom.getMaterial().contentEquals(mat)){
                        outList = matToGeom.get(mat);
                    }
                }
            }
            if (outList == null) {
                outList = new ArrayList<Geometry>();
                matToGeom.put(geom.getMaterial(), outList);
            }
            outList.add(geom);
        }

        int batchNum = 0;
        for (Map.Entry<Material, List<Geometry>> entry : matToGeom.entrySet()) {
            Material mat = entry.getKey();
            List<Geometry> geomsForMat = entry.getValue();
            Mesh mesh = new Mesh();
            mergeGeometries(geomsForMat, mesh);
            // lods
            if (useLods) {
                makeLods(geomsForMat, mesh);
            }
            mesh.updateCounts();
           
            Geometry out = new Geometry("batch[" + (batchNum++) + "]", mesh);
            out.setMaterial(mat);
            out.updateModelBound();
            retVal.add(out);
        }

        return retVal;
    }
Batches a collection of Geometries so that all with the same material get combined.
@param geometries The Geometries to combine
@param useLods true&rarr;generate levels of detail, false&rarr;don't
generate them
@return A List of newly created Geometries, each with a  distinct material
jme3-core/src/tools/java/jme3tools/optimize/GeometryBatchFactory.java