0

I need to create 3 lists based on these three objects:

{"cat","owl","fish","mouse","elephant"}
{"cat","owl","fish","mouse","elephant"}
{"cat","owl","fish","mouse","elephant"}

How do I save space so that I don't specify"mouse", "elephant" 3 times? The objects are iterated through and I don't know in advance what the object contains.

// pseudocode

public static List<String> getValuesAsList(myCoolObjects)
    for(MyCoolObject myCoolObject: MyCoolObjects){
    List<String> listContainingObjectValues = new ArrayList<>();
       listContainingObjectvalues.add(myCoolObject.getValue1());
       listContainingObjectvalues.add(myCoolObject.getValue2());
       listContainingObjectvalues.add(myCoolObject.getValue3());
    }
    return listContaingObjectValues;

So that the result would not be like this:

List 1 ["cat","mouse","elephant"]
List 2 ["owl","mouse","elephant"]
List 3 ["fish","mouse","elephant"]

The desired result would be this:

List 1 ["cat","mouse","elephant"]
List 2 ["owl",*points to 'List 1.[1]', *points to 'List 1.[2]'*]
List 3 ["fish",*points to 'List 1.[1]', *points to 'List 1.[2]'*]

Is this something that Java already does?

sander
  • 1,426
  • 4
  • 19
  • 46
  • `String` already is a reference type, so unless you create explicit copies it will already do that – UnholySheep Oct 10 '20 at 21:54
  • 2
    See also: https://stackoverflow.com/questions/10578984/what-is-java-string-interning – Alex Shesterov Oct 10 '20 at 21:56
  • Do you want to use matrix [] or List ()? – andersen Oct 10 '20 at 21:56
  • What I'm basically asking is that if you have those 3 objects.. and you create 3 lists from it, will java automatically share the same memory address for similar strings or not? – sander Oct 10 '20 at 22:28
  • How long are your lists? If your working with greater than 100,000 items, I'd consider this sort of thing. "Premature optimization is the root of all evil." -- Knuth. – NomadMaker Oct 11 '20 at 00:40

1 Answers1

0

Assuming that you have a list describing the first column and a list containing values for the rest of the columns, a list of lists may be generated:

List<String> firstCol = Arrays.asList("cat", "owl", "fish");
List<String> otherCols = Arrays.asList("mouse", "elephant");
    
List<List<String>> lists = firstCol.stream()
                                   .map(col -> Stream.concat(Stream.of(col), otherCols.stream())
                                                     .collect(Collectors.toList())
                                   )
                                   .collect(Collectors.toList());

lists.stream().forEach(System.out::println);

output:

[cat, mouse, elephant]
[owl, mouse, elephant]
[fish, mouse, elephant]
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • See edited answer. The objects are already given to me, I can not affect to them. – sander Oct 10 '20 at 22:33
  • Don't understand. In the desired output you removed elements 1 and 2 in the first list, elements 0 and 2 in the second, and 0 and 1 in the third, and keep the elements in columns 3, 4. Then you claim that lists 2 and 3 should "refer" the contents of list1. So lists 2 and 3 should never contain their own data? – Nowhere Man Oct 10 '20 at 22:45