aspose file tools*
The moose likes Java in General and the fly likes Data Structures in java : Examples Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Data Structures in java : Examples" Watch "Data Structures in java : Examples" New topic
Author

Data Structures in java : Examples

Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Hello all,

I need to create a data structure in java which can act as a buffer and temporarily store a few rows of data ( Example 5-10 rows )

This data will be stored and processed and then emptied.

What can be used in such case ?

Thanks,
Aditya


SCJP 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14108
    
  16

This sounds like you simply need a List or an array.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Hi jesper,

Even I though about using a Vector , ArrayList but I want it to be like one single structure. Example : One Arraylist will have one row , so I need to have 5-10 such rows in a structure similar to a database table.

Thanks,
Aditya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You need a class which holds those data, then set up an array of that class. You can alter the size of the array by creating a new array and copying elements as the number retained changes.
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Thanks Campbell,

So do you mean that for every record I should create an object of that class eg new MyClass(parameter1,parameter2,parameter3,...,parameterN) and store this in ArrayList <MyClass> or Array.

So I shall need getter and setter methods for accessing those values and once my job is done for that particular row , I can discard those object instances.

This is because ,I need to parse a file with around 20000000 lines/rows. And the data for processing would be around 5-10 line/rows.

Thanks,
Aditya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You said 5-10 rows earlier. Yes, that sort of data structure is what I meant.

For that many instances, you will probably need to put them into SQL, otherwise you will overwhelm the amount of heap space available.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Make all the data final in your instances, and forget about set methods.
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
So that means for processing a data of 5-10 rows I can use a class with those many final class variables.

Ok , I shall try executing that

Thanks,
Aditya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
It would appear in a database as 5-10 columns. If you wish to use that class to record data, make it into an immutable class. Making all the variables final is only part of that.
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Thanks Campbell,
One query

I have the following application.

A CSV file having around 8 to 10 comma separated fields. This file is approximately 6GB big with 20000000 rows.

As a part of processing this file, I need to store around 5-10 rows in a buffer/data structure.

For this buffer, I have created the following


Class Buffer
{
private String a;
private String b;
private String c;
Private string d;

public Buffer(a,b,c,d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}



Class process
{
method refine(a,b,c,d) ...................to process the data
{
new Buffer(a,b,c,d)
processing logic
}


get methods for the parameters

}



Class A
{

public static void main(args[])
{
while()
parse a CSV file having approximately 20000000 rows

collect the data and for each row and send it to the method in the class process
Process y = new Process();
y.refine();

}
}


1: My question is that creating a new object for the process class for every row isn't advisable right ?

2: If I create an reference variable of class Process as a class variable, I cannot use it inside main to create an object.

How should I implement this ?


Thanks,
Aditya


Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Adi please use code tags to make it more readable.


My Blog SCJP 5 SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Structures in java : Examples