3

I am in the luxourious position that I have the freedom to choose whether to implement the following method as an array of Strings:

public void addColumns(String[] columns)
{
    for(String column : columns)
    {
        addColumn(column);
    }
}

Or as a Collection of strings:

public void addColumns(List<String> columns)
{
    for(String column : columns)
    {
        addColumn(column);
    }
}

What is the best option to implement this? I'm using Java.

1
  • It depends on what else you need to do with your list/array. If you just want to do what is in your example code, i suggest using an array because it consumes less memory. Commented Jul 25, 2012 at 10:14

4 Answers 4

3

1. I will prefer you to use Collection when you are working with Java.

2. Java, and the processor have become fast enough that you won't notice any performance difference between an Array or Collection.

3. Collection gives you a lot of flexibility,and choices to select from List,Set , Maps etc..... whichever suits your need.

4. List<String> will be the way to go in my opinion.

Sign up to request clarification or add additional context in comments.

Comments

2

It entirely depends on the usage.

If you want to keep it light weight then use String[].

If you are making insertion deletion sorting and other operations or may use them in future then go for List<String>.

1 Comment

String[] requires a lot less typing to instantiate. You pretty much have to create a string array to create a List<String> anyway. So if you're not doing list operations it's the light weight way to go.
2

Both a string[] and a List<string> allow the method to mutate them.

You only actually need to use an Iterable<string> to achieve what you do in your example.

I'd use Iterable<string> because it expresses the minimum that you need (you can iterate over it). This also gives the added benefit that you can pass either a string[] or a List<string> into the method.

Using the most restricted type you can communicates intent of what the method will do.

9 Comments

Actually, I'd argue that Collection<String> is a better fit for his purposes, because what he needs is precisely a "collection of string". It's a better description of the parameter imho.
For me, that advertises to the callee that they should copy the references into another container before calling that method. What if the first line of addColumns was columns.clear()?. With Iterable you have no such worries.
I'm not sure I understand you correctly... The use of Collection advertises that? Iterators can remove objects from the underlying collection too, so it won't be unmodifiable anyway.
This question is tagged as Java. Your suggestion works in C#, but not in Java.
Unfortunately, that doesn't work in Java. Arrays do not implement Iterable.
|
0

First of all think how you are going to call your method. Do you already have the String array, or you'll have to build it? How do you plan to build it? Choose the solution that will make your client code do less work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.