• Post Reply Bookmark Topic Watch Topic
  • New Topic

java For loop  RSS feed

 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code


according to this code for loop is run once and do this State.setStateg("Approved");
   (ex:if first element of the List is equal to Approved then state is set to Approved.Iwant to go through all the elements)

I want to check all the data in checkedSt List and if all data is equal to Approved then state is change to Approved.
if All data is Denied then state is change to Rejected.
if data in the List are diffrent not to change.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use some counters to count the number of Approved and the number of Denied.   Then use the values to determine the actions.
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an you show me that?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we don't hand out answers here.  We expect you to try, post your new code, and we'll comment or make suggestions. Sometimes, we just ask leading questions.

So...how would YOU know if all the elements are approved?  I don't mean what would the java code be, I mean how would you, chamini prashakthi, do this if all you had was pencil and paper?
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
fred rosenberger and Norman Radder.
I got the point of Norman Radder and i resolved my probem.Thankyou verymuch for that..
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chamini prashakthi wrote: and i resolved my probem
Well done Would you mind to post a solution so we could all enjoy?
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knowing nothing about the code around, can't be that 50% are approve and 50% are rejected?

[edit] it seems you stated that above, didn't notice, sorry.
I want to check all the data in checkedSt List and if all data is equal to Approved then state is change to Approved.
if All data is Denied then state is change to Rejected.
if data in the List are diffrent not to change.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it "Rejected" or "Denied"?
You seem to have both there, but that could be intentional.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chamini prashakthi wrote:
There is something I think you ought to change there. You have a List<PromotionApproval> but in line 5 you are casting the elements. I am sure that cast is unnecessary. Remember casts are potentially harmful and you shou‍ld avoid them as much as possible. If the compiler is happy with line 1 and raises warnings about line 5, that is a sign the cast needs to go.
I would also have thought you can iterate that List with a for‑each loop (officially called enhanced for) which you can read about in the Java™ Tutorials. You would appear to have a strategy which marks the approvals on the basis of text in the objects. I am not convinced that text is the best way to record such information. What about an enumerated type?Are you approving or rejecting something if the decision in unanimous? What if there is a majority for rejection? What are you going to do?

Now how do we execute all that with a Stream? This is, after all, 2017 and Java8 has been out for nearly three years. It is really easy if you are going to demand a unanimous decision, because you can use the all match method.I know you can partition or group a Stream into a Map, but have forgotten how to count the different categories.
All Lists Collections have a stream() method which returns a Stream iterating the contents of that Collection. That would be a Stream<PromotionApproval> and you can extract the stateg variable from it (a Stream<String>) using a method reference. Then you can simply tell whether everything is equal to "Approved" with the allMatch() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!