1

I have objects set up like this, data coming from a service:

DataType1_1 has array [DataType2_1, DataType2_2, DataType2_3]
DataType1_2 has array [DataType2_2, DataType2_3]

I want them to be re-arranged so I have objects like this:

DataType2_1 has array [DataType1_1]
DataType2_2 has array [DataType1_1, DataType1_2]
DataType2_3 has array [DataType1_1, DataType1_2]

Hope that makes sense. How could I do this?

EDIT:

Have Animal objects (dog, cat, mouse) which each have an array of possible fur colors. I need to switch the order, so I have fur colors that each have an array of possible animals that correspond.

Dog has array [Brown, White, Black]
Cat has array [Brown, White, Red]
Mouse has array [White, Black]

Need this:

Brown has array [Dog, Cat]
White has array [Dog, Cat, Mouse]
Red has array [Cat]
Black has array [Dog, Mouse]
bfraz123
  • 113
  • 8

2 Answers2

1

What you are doing is called "array transposing" and yes, it is not Java specific.

But here is an example:

java multi-dimensional array transposing

Basically, loop through Array[x][y] to change to Array[y][x]. You just have to think of it as a 2 dimensional array instead of 2 different 1-dimensional arrays.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • 1
    I don't think that's what I want to do.. not sure, I added more details. I know it was confusing with the ambiguous dataTypes. – bfraz123 Jun 05 '15 at 20:17
  • Yes - if you look at it as an array of animals as "rows" and colors as "columns" you are trying to change it to animals as "columns" and colors as "rows" (and some of them have "0" or "null" values) – Jim Jun 05 '15 at 20:33
0

This might be a push in the right direction. I'm using just Strings, instead of custom class objects, to demonstrate the rearranging of arrays using Streams from Java 8.

public static void main(String[] args) throws Exception {
    String[] Dog = {"Brown", "White", "Black"};
    String[] Cat = {"Brown", "White", "Red"};

    // Join the arrays together to get distinct values
    String[] join = new String[Dog.length + Cat.length];
    System.arraycopy(Dog, 0, join, 0, Dog.length);
    System.arraycopy(Cat, 0, join, Dog.length, Cat.length);
    Object[] distincts = Arrays.stream(join).distinct().toArray();

    // Rearrange data into a Map
    Map<String, List<String>> rearranged = new HashMap();
    for (Object obj : distincts) {
        rearranged.put(obj.toString(), new ArrayList());
        if (Arrays.stream(Dog).anyMatch(dt -> dt.contentEquals(obj.toString()))) {
            rearranged.get(obj.toString()).add("Dog");
        }
        if (Arrays.stream(Cat).anyMatch(dt -> dt.contentEquals(obj.toString()))) {
            rearranged.get(obj.toString()).add("Cat");
        }
    }

    // Print rearranged results
    rearranged.keySet().stream().forEach((key) -> {
        System.out.println(key + " has array " + rearranged.get(key));
    });
}

Results:

Red has array [Cat]
Brown has array [Dog, Cat]
White has array [Dog, Cat]
Black has array [Dog]
Shar1er80
  • 9,001
  • 2
  • 20
  • 29