Create array of object from an array of int

Viewed 110

I'm not understanding something, obviously. I've got a primitive array of ints, and I need to convert that into an array of my own type. Here's what I've tried.

public class StupidInt {
   @JsonProperty("ID")
   private int id;

   public StupidInt(int id) { this.id = id; }

   public int getId() { return this.id; }
}

public static void main(String []args){
   int[] ints = {1,2,4,67};
   StupidInt[] myInts = IntStream.of(ints).map(StupidInt::new).collect(Collectors.toList());
}

As you might expect, myInts line has a problem and that problem is "cannot convert StupidInt to int". But I'm not sure what combination of map or foreach or whatever intermediate and terminal methods to use to convert that array of ints to an array of my object. What is the correct way to do this conversion?

2 Answers

You need to use mapToObj to create a new Stream of objects and toArray instead of collect to obtain the result as an array.

StupidInt[] myInts = IntStream.of(ints).mapToObj(StupidInt::new).toArray(StupidInt[]::new);

The answer from @hev1 is simple and the best answer for a terminal method for what you are trying to achieve.

I provide here two different options if you need a different solution. The first is just a for intermediate method to create the primitive array; and the second option is a wholesome different approach to the problem and does not create a primitive array but a "list" of the type Object that reflect the primitive type and will also work with any type or object:

Solution 1:

int[] ints = {1,2,4,67};
StupidInt[] myInts = new StupidInt[ints.length];
for (int i = 0; i < myInts.length; i++) {
    myInts[i] = new StupidInt(ints[i]);
}

Solution 2:

public class StupidIntList {

    private ArrayList<Integer> ids;

    public StupidIntList() { 
        this.ids = new ArrayList<Integer>();
    }

    public void add(int id) {
        this.ids.add(id);
    }

    public int get(int pos) { 
        return this.ids.get(pos); 
    }

    public boolean findId(int i_d) {
        for (Integer id : ids) {
            if(id == i_d)
                return true;
        }
        return false;
    }

    public String toString() {
        String res = "[";
        for (Integer id : ids) {
            res += id+",";
        }
        return res.substring(0, res.lastIndexOf(","))+"]";
    }

    public static void main(String []args){
        int[] ints = {1,2,4,67};
        StupidIntList myInts = new StupidIntList();
        for (int i = 0; i < ints.length; i++) {
            myInts.add(ints[i]);
        }
        System.out.println(myInts);
    }
}

In this case to add a new int you use the add method and to get one int you will use the get(position) method or the find method to look if it exists and get the position.

Or you could simply use the ArrayList class or if you want I can share with you a list structure that allows what you need to accomplish, has most methods you can think of, implements comparable, can be ordered, implements iterable and is very efficient.

Related