0

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;
    }

}
Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • 2
    `categoriesModelList.stream().map(CategoriesModel::getName).toArray(String[]::new)`. – Joachim Sauer Mar 19 '22 at 11:27
  • 4
    Note that any solution of this can only *hide* the loop inside some other code, but there's just fundamentally no way to achieve this without iterating over each element. – Joachim Sauer Mar 19 '22 at 11:28
  • @JoachimSauer Thanks for your reply, My android app is targeting `API level 21` and above but the `stream` method is added in `API level 24`. Please look at this question [Is it possible to use the Java 8 Stream API on Android API < 24?](https://stackoverflow.com/questions/39515035/is-it-possible-to-use-the-java-8-stream-api-on-android-api-24) Is there any alternative way to replace the `stream` method? – Taha Sami Mar 19 '22 at 11:37
  • 1
    Read the accepted answer to the question you linked to all the way through: you can now use streams with any API level, thanks to desuggaring. – Joachim Sauer Mar 19 '22 at 21:36

3 Answers3

1

You could use a stream:

String[] namesList = categoriesModelList.stream()
    .map(CategoriesModel::getName)
    .toArray(String[]::new);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for your answer, My android app is targeting `API level 21` and above but the `stream` method is added in `API level 24`. Please look at this question [Is it possible to use the Java 8 Stream API on Android API < 24?](https://stackoverflow.com/questions/39515035/is-it-possible-to-use-the-java-8-stream-api-on-android-api-24) Is there any alternative way to replace the `stream` method? – Taha Sami Mar 19 '22 at 11:36
  • If you can't use streams, then you may be stuck with loops, which actually isn't that bad at all. – Tim Biegeleisen Mar 19 '22 at 11:42
  • In this situation, I'll use the `stream` method for devices that targets `API level 24` and above and I'll use a loop for devices that targets `21, 22, and 23`. All answers are correct but I'll select your answer as the correct answer depending on the post date. – Taha Sami Mar 19 '22 at 11:53
1

anyway you have to use loop operation.
but stream is more elegant.

String[] namesList = categoriesModelList.stream()
                .map(CategoriesModel::getName)
                .toArray(String[]::new);
Joonseo Lee
  • 458
  • 2
  • 6
  • 12
  • Thanks for your answer, My android app is targeting `API level 21` and above but the `stream` method is added in `API level 24`. Please look at this question [Is it possible to use the Java 8 Stream API on Android API < 24?](https://stackoverflow.com/questions/39515035/is-it-possible-to-use-the-java-8-stream-api-on-android-api-24) Is there any alternative way to replace the `stream` method? – Taha Sami Mar 19 '22 at 11:36
1

Use java.util.stream for it:

String[] names = categoriesModelList.stream()
     .map(category -> category.getName())
     .collect(Collectors.toList())
     .toArray(new String[]{});

Another variant avoid stream():

List<String> namesList = new LinkedList<>();
categoriesModelList.forEach(category -> namesList.add(category.getName()));
String[] names = namesList.toArray(new String[]{});

But this is just a workaround

tohhant
  • 103
  • 10
  • Why do category -> category.getName() when it can be simplified to a double colon? – MorganS42 Mar 19 '22 at 11:31
  • It is for better understanding for the new person at Streams and static references – tohhant Mar 19 '22 at 11:32
  • 1
    @MorganS42 the difference is essentially irrelevant. On the other hand, you could have picked up on `collect(toList()).toArray(...)`, which should just be `toArray(...)`. – Andy Turner Mar 19 '22 at 11:33
  • Thanks for your answer, My android app is targeting `API level 21` and above but the `stream` method is added in `API level 24`. Please look at this question [Is it possible to use the Java 8 Stream API on Android API < 24?](https://stackoverflow.com/questions/39515035/is-it-possible-to-use-the-java-8-stream-api-on-android-api-24) Is there any alternative way to replace the `stream` method? – Taha Sami Mar 19 '22 at 11:36
  • 1
    It's more resource cunsomptional, but as I said, new person at static references and lambdas can be confused – tohhant Mar 19 '22 at 11:39
  • 1
    @SC291 I added a little bit uggly, but a solution without `stream()` – tohhant Mar 19 '22 at 11:57
  • @tohhant Look at this method [setItems](https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder#setItems(java.lang.CharSequence[],%20android.content.DialogInterface.OnClickListener)) in android documentation, It accepts an only array as a parameter. Thank you for your time and +1 for your effort :) – Taha Sami Mar 19 '22 at 12:04