I have the following code:
public void myiteration(List <MyObject> mylist) {
if (mylist != null) {
for ( MyObject mo : mylist) {
... }
}
As you can see I have to check at first whether the passed list is null or not.
If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.
Ok, it works as shown before.
However I would appreciate to be able to treat an null value as an empty list.
I want to omit the leading, wrapping "if" check.
Is this possible somehow?
Maybe with a workaround like:
for (MyObject mo : mylist; (mylist = NOT(null)))
Robin
|
|
0
|
|
|
|
Reply
|
rob
|
1/25/2011 10:07:05 AM |
|
On 25/01/2011 11:07, Robin Wenger wrote:
> I have the following code:
>
> public void myiteration(List<MyObject> mylist) {
>
> if (mylist != null) {
> for ( MyObject mo : mylist) {
> ... }
> }
>
> As you can see I have to check at first whether the passed list is null or not.
> If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.
>
> Ok, it works as shown before.
>
> However I would appreciate to be able to treat an null value as an empty list.
> I want to omit the leading, wrapping "if" check.
>
> Is this possible somehow?
>
> Maybe with a workaround like:
>
> for (MyObject mo : mylist; (mylist = NOT(null)))
Well you /can/ make something like
if(mylist == null) {
mylist = Collections.emptyList();
}
before the loop. Same difference really.
I think you should really make a best effort of not sending null Lists
as parameter of the method in the first place, though. That way, the
method can correctly throw a NullPointerException when that happens, as
it must not happen.
--
Mayeul
|
|
0
|
|
|
|
Reply
|
Mayeul
|
1/25/2011 10:35:39 AM
|
|
On 25.1.2011 12:07, Robin Wenger wrote:
> I have the following code:
>
> public void myiteration(List <MyObject> mylist) {
>
> if (mylist != null) {
> for ( MyObject mo : mylist) {
> ... }
> }
>
> As you can see I have to check at first whether the passed list is null or not.
> If the is null and have no "if" check on the null value then the prog would crash at the "for" loop.
>
> Ok, it works as shown before.
>
> However I would appreciate to be able to treat an null value as an empty list.
> I want to omit the leading, wrapping "if" check.
>
> Is this possible somehow?
>
> Maybe with a workaround like:
>
> for (MyObject mo : mylist; (mylist = NOT(null)))
>
> Robin
>
Lazy arent ya ;)
myList = myList == null ? new ArrayList<MyObject>() : mylist ;
for (MyObject obj : myList)
{
Something you just have to write...Be it if or something other...
--
Q: How many lawyers does it take to change a light bulb?
A: You won't find a lawyer who can change a light bulb. Now, if
you're looking for a lawyer to screw a light bulb...
|
|
0
|
|
|
|
Reply
|
Donkey
|
1/25/2011 11:59:24 AM
|
|
On 01/25/2011 05:07 AM, Robin Wenger wrote:
> Maybe with a workaround like:
>
> for (MyObject mo : mylist; (mylist = NOT(null)))
If you want to minimize extra lines:
public static <T> List<T> denullify(List<T> list) {
return list == null ? Collections.emptyList<T>() : list;
}
....
for (MyObject mo : denullify(mylist))
Alternatively, you could put that conditional in the for statement
directly, but it might be long for your tabulation requirements.
Also, what is up with people posting to cljp and cljh, with followups
set to only cljh? It makes it confusing to see no responses...
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
|
|
0
|
|
|
|
Reply
|
Joshua
|
1/25/2011 1:00:41 PM
|
|
Robin Wenger wrote:
>> Maybe with a workaround like:
>>
>> for (MyObject mo : mylist; (mylist =3D NOT(null)))
>
This is not an improvement over suggestions like Joshua's.
Joshua Cranmer wrote:
> If you want to minimize extra lines:
>
> public static <T> List<T> denullify(List<T> list) {
> =A0 =A0return list =3D=3D null ? Collections.emptyList<T>() : list;
>
> }
>
> ...
>
> for (MyObject mo : denullify(mylist))
>
> Alternatively, you could put that conditional in the for statement
> directly, but it might be long for your tabulation requirements.
>
Instead of relying on the language to enforce invariants for you, you
can use the language to help enforce invariants for you with the
'assert' keyword.
Nothing removes from the programmer the responsibility to enforce
invariants. One way or t'other you have to handle the 'null' case.
Often effective is to prevent the null case. Then you can have code
like:
assert things !=3D null;
for ( Thing thing : things )
{
...
}
This is better on a ton of levels.
> Also, what is up with people posting to cljp and cljh, with followups
> set to only cljh? It makes it confusing to see no responses...
>
Yeah, OP and others, don't do that! It's not like people aren't
asking you over and over and over to stop or anything. You're asking
for help but flouting the etiquette - not very smart diplomacy nor
good manners, wouldn't you agree?
Do you wish to antagonize those from whom you request help? Do you
wish to be rude?
This is Usenet - you can be rude directly without playing header
games.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
1/25/2011 5:32:00 PM
|
|
Robin Wenger <rob@wenger.net> wrote:
>I have the following code:
>
>public void myiteration(List <MyObject> mylist) {
>
> if (mylist != null) {
> for ( MyObject mo : mylist) {
> ... }
> }
>As you can see I have to check at first whether the passed list is null or not.
What is your contract with the caller? There are a few cases where you'd like
null to be considered an empty list, but most of the time it indicates an
error somewhere up the chain.
You're safer with:
if (mylist == null) {
throw new IllegalArgumentException("must specify list to iterate.");
}
or, at a code/package/conceptual boundary where you've documented that null
and empty are the same thing for convenience,
if (mylist == null) {
mylist = Collections.emptyList();
}
and then feel free to pass mylist into your more private library classes,
which can treat null as the error it is.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
|
|
0
|
|
|
|
Reply
|
dagon
|
1/25/2011 8:51:49 PM
|
|
On 25/01/2011 18:32, Lew allegedly wrote:
> Robin Wenger wrote:
<snip />
>> for (MyObject mo : denullify(mylist))
>>
>> Alternatively, you could put that conditional in the for statement
>> directly, but it might be long for your tabulation requirements.
>>
>
> Instead of relying on the language to enforce invariants for you, you
> can use the language to help enforce invariants for you with the
> 'assert' keyword.
>
> Nothing removes from the programmer the responsibility to enforce
> invariants. One way or t'other you have to handle the 'null' case.
> Often effective is to prevent the null case. Then you can have code
> like:
>
> assert things != null;
> for ( Thing thing : things )
> {
> ...
> }
>
> This is better on a ton of levels.
Better yet: I assume you got that instance from a method which returned
a Collection. A method returning a Collection ought never to return null
unless it is necessary and is clearly indicated in its contract (e.g.
Javadoc).
The latter only makes sense if a null return value and an empty
collection represent different method results. If that's the case, then
you absolutely /have to/ handle the null case.
Otherwise, modify the method in question never to return null, but
java.util.Collections#emptySomething instead. If you can't modify it,
create a wrapper in your code for it.
Do not expect Java to provide you with syntactic sugar for things like
this. Or actually, dread the day might come when Java will provide you
syntactic sugar for things like this.
There are many cases where the Java programming language forces you to
handle things manually. In hardly *any* of these cases does it *not*
make sense.
>
>> Also, what is up with people posting to cljp and cljh, with followups
>> set to only cljh? It makes it confusing to see no responses...
>>
>
> Yeah, OP and others, don't do that! It's not like people aren't
> asking you over and over and over to stop or anything. You're asking
> for help but flouting the etiquette - not very smart diplomacy nor
> good manners, wouldn't you agree?
> Do you wish to antagonize those from whom you request help? Do you
> wish to be rude?
>
> This is Usenet - you can be rude directly without playing header
> games.
Bloody fucking yeah.
df.
|
|
0
|
|
|
|
Reply
|
Daniele
|
1/25/2011 11:40:06 PM
|
|
|
6 Replies
243 Views
(page loaded in 0.181 seconds)
Similiar Articles: Neatest way to get the end pointer? - comp.lang.c... At no stage is pend dereferenced; and the loop ... execl isn't defined by standard C, of course; let ... such as: char empty_string[1]; empty_string[0] = NULL ... Iterating over a String - comp.lang.java.helpIt has bugged me that the for:each syntax would not let ... > > The indexing loop lets you look back and forward, which ... set[1], inside java, with codepoints you treat it as ... How to check whether malloc has allocated memory properly in case ...> If malloc has to return a non-NULL value for an empty allocation ... to define the behavior consistently and let ... did unambiguously specify that a null count DO loop was an ... delete columns csv file - comp.lang.awkTo clarify, the values 0.0e0 represent null ... now I see that your "" to force awk to treat ... Initially set to the empty string, out="", then in the loop there's more ... error LNK2019: unresolved external symbol _main referenced in ...... am using Visual Studio 2008 C++, starting with an empty ... lParam); } // If something was not done, let ... WM_QUIT ); // Terminates message loop ... GAWK: A fix for "missing file is a fatal error" - comp.lang.awk ...I think it's still a Good Thing to let an ... of deleting the file from the argument list, so there's no need to open /dev/null ... It does that today if the input file is empty ... MERGE statement consuming all available CPU - comp.databases.ibm ...It seems, somehow, that DB2 comes to an infinite loop. ... Heap size: 452 Database Heap size: 16384 Lock List ... terminate db2stop db2start Run the workload and let ... Some text processing questions - comp.lang.vhdl... language for doing conditionals, loops ... how would I write, is_empty I would ... will crash the simulator if L is null. On the other hand, it *is* OK to put an empty ... input & output in assembly - comp.lang.asm.x86@=$100 loop: bsr.l getc ; get char from ... case" of backspace when the buffer is actually empty ... to the Model-T is acceptable for a beginner, but let's ... Could anyone give me the spice-mode.el - comp.emacsHi, All I am new to *NIX and I am thinking of writing spice code under Emacs. However, I have no idea of Emacs Lisp. Hence, I could not write a packa... Detecting "Null" database fields - Velocity Reviews - Computer ...While an empty string value means that the value is set to empty let say u ... Let "for" loop treat "null" as empty List ? Robin Wenger: Java: 3: 01-25-2011 10:40 PM Loops#!/bin/bash # list-glob.sh: Generating [list] in a for-loop, using ... one two three" } for word in $(generate_list) # Let ... Helou's code thusly: OLDIFS=$IFS IFS='' # Null ... 7/22/2012 2:23:37 PM
|