This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic data structure and java.util package

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In many cases we need to use data structures like linked list, double linked list, circular linked list, etc. I know if you write code in C language, you have to define your own "struture" for them like

typedef struct Node {
int i;
Node *next;
} MyNode;

and you have to implement every operations (like "delete", "insert", etc) by yourself in the functions.

In Java, is it true that java.util package has already done ALL of these for us ? And all we need to do is to instantiate a "LinkedList" and call the API's built in functions to perform the operations.

If this is true, then I am wondering that when college students learn data structure using java, are they going to skip the implementation part because it is alreday available in API ?? In C that's a very important practice part. But if Java API has already implemented all these operations for various data structure, then programmers do not need to worry about it any more, is that right ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the standard C++ library has had a linked list class for many years, as have the libaries for lots of other languages. For those languages where the standard library doesn't provide collections classes like this, there are virtually always third-party libraries, both open source and commercial, that implement them.

It's always important to learn the theory, whether implementations already exist or not. But once you know the theory, the smartest thing to do, in Java or in any other language, is to build on the work of others. Don't reinvent the wheel, as it's exceedingly unlikely that your wheel will be as nice as wheels other people have worked on before you. If your university education has left you with the impression that professional programmers do or should write their own novel linked list classes, instead of using standard libraries, then you've been seriously misled.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ernest. Just because a library for basic data structure exists does not preclude students from writing their own implementations to learn how it is done. In most college courses, the professor will probably specify that you are not allowed to use any built-in or third-party libraries. In a professional setting, there are often cases where the library's data structures are insufficient to solve a particular problem so you need to write something from scratch that is appropriate for a particular situation. However, in general, once you understand the theory, you should make the most effort to reuse the libraries that already exist for these well understood tasks.

Layne
 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, should I put this way --- In Java we basically have API (classes, interface and operation methods) for most of the commonly used data strutures already. So, if someone asks you to do some relatively common data structure manipulation, you should be able to simply use a API class and a API method to do it, right ?

I know the Map, LIst, Set are very cool. How about Trees ? does JAva have nice API for binary tree, red-black tree, etc ?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking a University course in Java now, and our text didn't skip over the linked list implementation. Of course it had us implement a simple singly linked list, but it had all the functionality you mentioned. Understanding the implementation is key to deciding which data structure to implement depending on how they are used. That said, I still grumbled a little when I got to the end of the chapter and realized we had just re-invented the wheel, and a crumbly old wagon-wheel at that, when there are free Pirelli's sitting around for the taking! (Just to run that metaphor into the ground).
[ January 18, 2006: Message edited by: Garrett Rowe ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Artemesia Lakener:
so, should I put this way --- In Java we basically have API (classes, interface and operation methods) for most of the commonly used data strutures already. So, if someone asks you to do some relatively common data structure manipulation, you should be able to simply use a API class and a API method to do it, right ?

I know the Map, LIst, Set are very cool. How about Trees ? does JAva have nice API for binary tree, red-black tree, etc ?


Ofcourse everybody uses the classes in the standard API. In the real world you always have to deal with deadlines as a software developer, and it would not be smart to waste your time writing your own collection classes when there are already good, well-tested implementations available in the standard API.

There are no binary tree and red-black tree in the standard API. If you want to know what's in the standard API, look at the API documentation (link in my signature).

If you need something specific, for example a red-black tree, and you don't want to spend your time writing it yourself, try searching the Internet for "java red-black tree". For almost anything there's an open source and free library available in the Java world.
 
Thibault Dangr´┐Żaux
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also a problem with some teachers, though I expect it to have diminished in the recent years... Some teachers come from a procedural programming background. So, they fail to understand the specifics of OOP and think that using OOP is all about encoding good old algorithmics into classes. I have had bunch of teachers like this. The telltale sign was when most of the code ended up in the main()...
Actually, OOP is all about code reuse, API knowledge, event oriented programming, component oriented programming, design pattern, object interaction and a *little* algorithmics.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll vote that it is excellent exercise for programming newbies to implement some of these basics by hand. Since Java is about my thousandth language I didn't do linked lists and the whole catalog this time, but remembering the versions I did in other languages helps a lot when it's time to choose a collection.

I have to admit I have duplicated some of the classes in the standard libraries because I didn't quite get how to search the API docs and couldn't find the solutions that were already there. Still a good exercise. That's my excuse and I'm stickin to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic