best way to find matches (rookie alert)

J

Jeff Kish

I'm a rookie.

I have mostly simplistic java experience and deeper c++ experience.
Working with java j2sdk1.4.1_06 I believe.
I don't know what the equivalent of templates is for java.

I'm trying to figure out the best way to determine if a problem occurs, and I
assume it would be by keeping a pair of lists and doing some finds after they
are populated.

Ths situation is I have a method that calls some api I have access to to get
rows from a set of data rows like this:

for (x = 0; x < dataSet.size; x++)
{
Data entry = dataSet.getEntry(x);
String theID = entry.getData("theID"); //dups allowed for "theID"
String theField1 = entry.getData("theField1");
String theField2 = entry.getData("theField2");

}

I would like to build a vector/list/whatever (I think) to keep track of all
the Data entries that have one of two characteristics:
either:
Strings from theField1 and theField2 are both null OR
the Strings from one or both are not null

After I've examined the list I plan on throwning an exception for a business
rule validation - the business rule says:
if any Data entry identified by "theID" has both fields null and there
is any other Data entry with the same "theID" that has either one or
both not null, an exception is thrown.

I was thinking that something like this is reasonable:
Vector String atLeastOneFieldNotNull;
Vector String bothFieldsNull;
for(...)
{
if both field strings null
{
bothFieldsNull.push_back(new String entry.getData("theID"));
}
else
{
atLeastOneFieldNotNull.push_back(new String
entry.getData("theID"));
}
}

boolean foundBadSituation = false;
int nIndex = 0;
int nSize = bothFieldsNull.size();
int nSize2 = atLeastOneFieldNotNull.size();
if ((nSize < 0) && (nSize2 > 0))
{
for (nIndex = 0; nIndex < nSize2; nIndex++)
{
if (atLeastOneFieldNotNull.find(bothFieldsNull[nIndex])
{
throw exception;
}
}
}

comments on the approach, and the "right" java way to do this are both
appreciated.
Thanks

Jeff Kish
 
J

Jeff Kish

I'm a rookie.

I have mostly simplistic java experience and deeper c++ experience.
Working with java j2sdk1.4.1_06 I believe.
I don't know what the equivalent of templates is for java.

I'm trying to figure out the best way to determine if a problem occurs, and I
assume it would be by keeping a pair of lists and doing some finds after they
are populated.

Ths situation is I have a method that calls some api I have access to to get
rows from a set of data rows like this:

for (x = 0; x < dataSet.size; x++)
{
Data entry = dataSet.getEntry(x);
String theID = entry.getData("theID"); //dups allowed for "theID"
String theField1 = entry.getData("theField1");
<snip>
mm I forgot to mention that actually their are a pair of fields that identify
the Data entry, namely "theID" and also "theID2" which work in conjunction
with each other.
Thanks
Jeff Kish
 
D

David Hilsee

Jeff Kish said:
I'm a rookie.

I have mostly simplistic java experience and deeper c++ experience.
Working with java j2sdk1.4.1_06 I believe.
I don't know what the equivalent of templates is for java.

I'm trying to figure out the best way to determine if a problem occurs, and I
assume it would be by keeping a pair of lists and doing some finds after they
are populated.

Ths situation is I have a method that calls some api I have access to to get
rows from a set of data rows like this:

for (x = 0; x < dataSet.size; x++)
{
Data entry = dataSet.getEntry(x);
String theID = entry.getData("theID"); //dups allowed for "theID"
String theField1 = entry.getData("theField1");
String theField2 = entry.getData("theField2");

}

I would like to build a vector/list/whatever (I think) to keep track of all
the Data entries that have one of two characteristics:
either:
Strings from theField1 and theField2 are both null OR
the Strings from one or both are not null

This would apply to all Data entries. The logic is cleared up by what you
provided below, but what you just just stated translates into the following.

if ( theField1 == null && theField2 == null || // both null
theField1 == null && theField2 != null || // from one not null
theField1 != null && theField2 == null || // from one not null
theField1 != null && theField2 != null // both not null
){
// keep track of the data entry. This will always be reached.
}

I understand what you mean by looking at the code and the following
paragraph. I just wanted to point out that the description above was a
little confusing, and that might be why you're not getting many responses.
After I've examined the list I plan on throwning an exception for a business
rule validation - the business rule says:
if any Data entry identified by "theID" has both fields null and there
is any other Data entry with the same "theID" that has either one or
both not null, an exception is thrown.

I was thinking that something like this is reasonable:
Vector String atLeastOneFieldNotNull;
Vector String bothFieldsNull;
for(...)
{
if both field strings null
{
bothFieldsNull.push_back(new String entry.getData("theID"));
}
else
{
atLeastOneFieldNotNull.push_back(new String
entry.getData("theID"));
}
}

boolean foundBadSituation = false;
int nIndex = 0;
int nSize = bothFieldsNull.size();
int nSize2 = atLeastOneFieldNotNull.size();
if ((nSize < 0) && (nSize2 > 0))
{
for (nIndex = 0; nIndex < nSize2; nIndex++)
{
if (atLeastOneFieldNotNull.find(bothFieldsNull[nIndex])
{
throw exception;
}
}
}

comments on the approach, and the "right" java way to do this are both
appreciated.

First, I just want to point out that it is rare that you see new
String(<something>) in Java code. It might be used when converting from a
byte[] or a char[] to a String, but that doesn't occur very often. Strings
are immutable, so it rarely makes sense to invoke the String(String s)
constructor. It looks like that is what your code is doing, so you might
want to chage is so it doesn't do that.

I would probably use some container that derives from java.util.Set
(provides similar functionality to std::set in C++) of Strings (ids).
Iterate over the dataSet once, and when you find an entry with both fields
null, add an entry to the set (set.add(id)). Now, iterate over it one more
time, and when you find an entry with at least one field not null, check the
value in the set to see if you marked that ID as having a "both fields null"
entry (if(set.contains(id))). If set.contains(id) returns true, then you
should throw an exception.

In case you're curious, the C++ equivalent container would be a
std::set<std::string>. This solution is probably better than your vector
solution because Vector.find() is usually slower than Set.contains().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top