Home > Mobile >  java how to split an object to multiple objects inside an array efficiently
java how to split an object to multiple objects inside an array efficiently

Time:06-10

I am new to Java so please excuse the way I explain/word this problem I have been given.

Problem: You bought a few bunches of fruit over the weekend. Create a java function that splits a bunch into singular objects inside an array.

example input: [{name: "grapes", quantity: 2}]

expected output: [{name: "grapes", quantity: 1}, {name: "grapes", quantity: 1}]

ANOTHER EXAMPLE INPUT AND EXPECTED OUTPUT:

 example input:
 [{name: "currants", quantity: 1},
 {name: "grapes", quantity: 2},
 {name: "bananas", quantity: 2}]

 expected output:
 [{name: "currants", quantity: 1},
 {name: "grapes", quantity: 1},
 {name: "grapes", quantity: 1},
 {name: "bananas", quantity: 1},
 {name: "bananas", quantity: 1}]

CodePudding user response:

Because i dont know what you exactly want to do, i wrote quickly a frew lines of Code, like a "proof of concept".

Sure this is maybe not exactly what you want, or what you supposed to to. But according to the information you give, this could be a soloution for you.

public class FruitArry {

public static void main (String [] args){
    ShoppedFruits grapes = new ShoppedFruits("grapes",2);
    System.out.println(Arrays.toString(getFruitsAsArray(grapes)));

}

public static String[] getFruitsAsArray(ShoppedFruits shoppedFruits){
    String[] returnArray = new String[shoppedFruits.quantity];
    for(int i = 0 ; i < shoppedFruits.quantity; i  ){
        returnArray[i] = shoppedFruits.toString();
    }
    return returnArray;
}

public static class ShoppedFruits {
    String name;
    int quantity;

    public ShoppedFruits(String name, int quantity) {
        this.name = name;
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "name:"  name  " quantity: 1";
    }
}
}

Cosole output:

[name:grapes quantity: 1, name:grapes quantity: 1]

CodePudding user response:

Assuming the class Fruit has the appropriate fields with a full-args constructor and getters:

final List<Fruit> fruits = List.of(
        new Fruit("currants", 1),
        new Fruit("grapes", 2),
        new Fruit("bananas", 2)
);

You can yield an infinite stream of the "single item quantities" (1, 1, 1...) limited by the actual count of the fruit type limit(fruit.getQuantity()) and mapping the "single item quantity" as 1 into the new instance using mapToObj. The whole structure shall be flatmapped using flatMap as you get Stream<Fruit> from a single Fruit:

final List<Fruit> expanded = fruits.stream()
        .flatMap(fruit -> IntStream.generate(() -> 1)
                .mapToObj(i -> new Fruit(fruit.getName(), i))
                .limit(fruit.getQuantity()))
        .collect(Collectors.toList());

Alternatively limit the stream on creation using IntStream.range:

final List<Fruit> expanded = fruits.stream()
        .flatMap(fruit -> IntStream.range(0, fruit.getQuantity())
            .mapToObj(i -> new Fruit(fruit.getName(), 1)))
        .collect(Collectors.toList());

Both of the solutions result in this:

expanded.forEach(System.out::println);
Fruit[name='currants', quantity=1]
Fruit[name='grapes', quantity=1]
Fruit[name='grapes', quantity=1]
Fruit[name='bananas', quantity=1]
Fruit[name='bananas', quantity=1]
  • Related