Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data Structures in java : Examples

 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like you simply need a List or an array.
 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make all the data final in your instances, and forget about set methods.
 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adi please use code tags to make it more readable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic