aspose file tools*
The moose likes Java in General and the fly likes Basic data structure and java.util package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Basic data structure and java.util package " Watch "Basic data structure and java.util package " New topic
Author

Basic data structure and java.util package

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14429
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Thibault Dangr�aux
Greenhorn

Joined: Dec 12, 2005
Posts: 19
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic data structure and java.util package