Re: Aspect questions?
Novice wrote:
Lew wrote:
So taking lists as the example:
List<String> options = new ArrayList<>();
I declared the 'options' variable as a 'List<String>', not as an
'ArrayList<String>'. If I discover that the array-backed list wasn't
giving me the performance or other implementation-specific behavior I
need, but a linked list does, I can refactor more easily:
List<String> options = new LinkedList<>();
The rest of the program, depending only on the "list"ness and not the
"array"ness, is unharmed.
(What performance differences are there? Excellent question! Pop quiz
- please answer this post with your answer - where would you find out
about performance characteristics of 'ArrayList<E>' and
'LinkedList<E>'?)
I meant where specifically. You didn't show me where. You talked a lot about
how you'd go about finding where, but you didn't actually follow your own advice.
Summary:
Interface: what to do
Class: how to do it
I need to understand what you've said a bit better before I try the pop
quiz ;-)
It makes perfect sense to me to figure out what a program needs to do
before you worry about how it gets done. You've got to design a house - how
many bedrooms, how many bathrooms, what style of house (ranch or bungalow
or apartment building), etc. etc. - before you choose the exact fixtures.
But when you talked about designing interfaces, I assumed you meant NEW interfaces.
No. I wasn't talking about designing interfaces. I was talking about writing a
program.
Your examples seem to be about selecting which of the existing
interfaces - which kind of lists - you wanted. But those kinds already
Yes. There's a reason for that. It's because I was talking about selecting
which existing interfaces you want to use.
exist so you're really just choosing them, not inventing new ones to do new
Right. Precisely. Just so.
things. It would be helpful to me if you also talked about new interfaces
and how those come about at design time.
Give me an example of some unit of functionality you'd like to design, in
broad terms, and I'll do just that.
By the way, I completely see your point in the examples. I have already
benefited from the same by using Collections. I use more Sets than Lists
Why do you use more sets than lists?
and I will often start with a HashSet but if I start using it and find it
doesn't have things in the order I wanted, it's a trivial thing to change
it to another ordering by choosing a TreeSet or LinkedHashSet. That is
REALLY convenient.
Now that I think about it, I suppose I know enough to tackle the pop quiz
after all.
I have the feeling that I'm supposed to answer with something like "the
API" or maybe based on something you said in one of your posts. Well, I'm
"You're supposed to answer"? What _is_ the answer? "The API" or "something you
said" isn't an answer. That's like saying, "Where in the city will I find the
post office" and you say, "There's a map somewhere". I am still left unable to
mail a letter.
going to "over-answer" the question and say this:
Sorry, that's actually under-answering.
I know that the API or the various Java Tutorials (like the one on
Collections) often gives clues about performance and tells you explicitly,
What clues? Where? Show me. I asked a specific question about specific classes.
for example, that a HashSet normally performs best and a TreeSet worst with
a LinkedHashSet typically performing almost as well as a HashSet. So that's
obviously a good place to start.
"normally"? "performs"? "best"?
You described some useful strategies, but let's see what happens when you
apply them to 'ArrayList<E>' and 'LinkedList<E>'.
And since you brought them up, the three 'Set' implementations you mention.
The questions above are to stimulate thought. The questions that follow are
for you to answer here.
Without asking anyone else, but by research (which you should cite):
What are the performance differences (if any! - question every assumption in a
question) between:
- 'ArrayList<E>' and 'LinkedList<E>'?
- 'HashSet<E>', 'TreeSet<E>' and 'LinkedHashSet<E>'?
What are the differences between:
- 'List<E>' and 'Set<E>'?
For your claim that "HashSet normally performs best", define:
- "normally"
- "performs"
- "best"
Since you get to define these terms, there might not be exactly a right
answer. Instead, justify your definitions to whatever extent you feel they
need it. Be careful - justifications like "the standard definition" do require
citation of which standard.
Have fun.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg