GeoTools : CreateAFeatureCollection

We need to add several features to a layer of a map for instance. It is then convenient to use the FeatureCollection mechanism for doing that.

                //feature attributes creation 
		AttributeType[] types = new AttributeType[1];
		types[0] = AttributeTypeFactory.newAttributeType("centre", Point.class);

                // feature type creation
		FeatureType pointType = FeatureTypeFactory.newFeatureType(types,

		//feature instance creation
                // we could use also the WKT writers instead 
		GeometryFactory geomFac = new GeometryFactory(); 
		Point point1 = geomFac.createPoint(new Coordinate((0.0), 0.0));
		Point point2 = geomFac.createPoint(new Coordinate((10.0), 10.0));
                Feature feature1 = pointType.create(new Object[] { point1 });
                Feature feature2 = pointType.create(new Object[] { point2 })

                //feature collection creation
                FeatureCollection fc = FeatureCollections.newCollection();

See also : How to create a simple FeatureCollection from Scratch