Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to clone names from the list to the specific array without creating a loop?

I have 35 categories inside this list List<CategoriesModel> categoriesModelList;, and I want to clone all names and put them inside this array String[] namesList, I’m currently cloning names using this code but I want to ask is there any method can clone names from list to the specific array without creating a loop?

String[] namesList = new String[categoriesModelList.size()];
for (int i = 0; i < categoriesModelList.size(); i++)
    namesList[i] = categoriesModelList.get(i).getName();

CategoriesModel.java

public class CategoriesModel {

    private int id;
    private String name;
    private boolean supportSubcategories;

    public CategoriesModel(int id, String name, boolean supportSubcategories) {
        this.id = id;
        this.name = name;
        this.supportSubcategories = supportSubcategories;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public boolean isSupportSubcategories() {
        return supportSubcategories;
    }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use a stream:

String[] namesList = categoriesModelList.stream()
    .map(CategoriesModel::getName)
    .toArray(String[]::new);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading