简体   繁体   中英

how to convert list of model objects to list of object arrays java

How to convert a list of model objects to list of object arrays java

Like example List<Model> newlist = new ArrayList<Model>(); This is my which I want to convert into List

How will I do it?

To convert array to list you can use

List<Object> list = Arrays.asList(ObjectOne, ObjectTwo)

or to convert list to array

Object[] array = list.toArray();

Ok, let's stick to the requirements. Seems to make not much sense, but there it is:

List<Object[]> transformed = list.stream().map(i -> new Object[]{i}).collect(Collectors.toList());

And a java 5 version with 2 possible varieties of creating Object[] from an objcect.

 class C {
    public final Long id;
    public final String value;

    C(Long id, String value) {
        this.id = id;
        this.value = value;
    }
}


public void objectify() {
    List<C> items = Arrays.asList(new C(0L, "a"), new C(1L, "b"));
    List<Object[]> coarse = new ArrayList<>();
    List<Object[]> detailed = new ArrayList<>();
    for (C c: items)
    {
        coarse.add(new Object[]{c});
        detailed.add(new Object[]{c.id, c.value});
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM