Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Java
»
Beginning Java
Java 1.5 for loop
clakare bright
,
Greenhorn
Jan 17, 2009 03:42:26
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
import java.util.ArrayList; import java.util.HashMap; /** * */ /** * @author xxx * */ public class ReportGen { public void generateReport(){ HashMap<String, String> shopMap = new HashMap<String, String>(); HashMap<String, Integer> itemMap = new HashMap<String, Integer>(); HashMap<String, Integer> userMap = new HashMap<String, Integer>(); HashMap<String, Integer> sendMap = new HashMap<String, Integer>(); HashMap<String, Integer> mailMap = new HashMap<String, Integer>(); ArrayList<RecoMailBean> list = ValueBuilder.createValueList(); for(RecoMailBean bean : list){ int initVal = 1; String shopUrl = bean.getShopURL(); if(!shopMap.containsKey(shopUrl)){ shopMap.put(shopUrl, shopUrl); itemMap.put(shopUrl, initVal); userMap.put(shopUrl, bean.getUserCt()); sendMap.put(shopUrl, bean.getSendCt()); mailMap.put(shopUrl, bean.getMailCt()); }else{ shopMap.put(shopUrl, shopUrl); int itemCt = itemMap.get(shopUrl); itemMap.put(shopUrl, itemCt+initVal); int usrCt = userMap.get(shopUrl); userMap.put(shopUrl, usrCt+bean.getUserCt()); int sendCt = sendMap.get(shopUrl); sendMap.put(shopUrl, sendCt+bean.getSendCt()); int mailCt = mailMap.get(shopUrl); mailMap.put(shopUrl, mailCt+bean.getMailCt()); } } //Total shop report summary int itemCt=0; int usrCt=0; int sendCt=0; int mailCt=0; for(String shopURL :shopMap.keySet()){ itemCt = itemCt + itemMap.get(shopURL); usrCt = usrCt + userMap.get(shopURL); sendCt = sendCt + sendMap.get(shopURL); mailCt = mailCt + mailMap.get(shopURL); } RecoMailBean shopSumBean = new RecoMailBean(); shopSumBean.setShopCt(shopMap.size()); shopSumBean.setItemUrlCt(itemCt); shopSumBean.setUserCt(usrCt); shopSumBean.setSendCt(sendCt); shopSumBean.setMailCt(mailCt); ArrayList<RecoMailBean> shopSumList = new ArrayList<RecoMailBean>(); shopSumList.add(shopSumBean); System.out.println("shopSumBean : "+shopSumBean); //Call to Generate string message function of the shopSumList // and return msg.set the msg to RecoMailBean of the // setShopURL(msg) shopSumBean = new RecoMailBean(); shopSumBean.setShopURL("shopSumBean consolidated msg from String Msg" + "generate function"); //Shop summary report ArrayList<RecoMailBean> shopList = new ArrayList<RecoMailBean>(); shopList.add(0, shopSumBean); for(String shopURL :shopMap.keySet()){ RecoMailBean shopBean = new RecoMailBean(); shopBean.setShopURL(shopMap.get(shopURL)); shopBean.setItemUrlCt(itemMap.get(shopURL)); shopBean.setUserCt(userMap.get(shopURL)); shopBean.setSendCt(sendMap.get(shopURL)); shopBean.setMailCt(mailMap.get(shopURL)); System.out.print(" shopUrl : "+shopMap.get(shopURL)); System.out.print(" itemCt : "+itemMap.get(shopURL)); System.out.print(" userCt : "+userMap.get(shopURL)); System.out.print(" sendCt : "+sendMap.get(shopURL)); System.out.println(" mailCt : "+mailMap.get(shopURL)); shopList.add(shopBean); } System.out.println("shopList : "+shopList); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ReportGen gen = new ReportGen(); gen.generateReport(); } }
Maneesh Godbole
,
Bartender
staff
Jan 17, 2009 06:04:18
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
You had a question??
In future, please use
code tags
.
I have edited your original post for now. As you can see it is much more readable and easier to understand.
Jesper de Jong
,
Java Cowboy
staff
Jan 17, 2009 08:54:33
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
If you want to know more about the enhanced for loop syntax of
Java
5, see this:
The For-Each Loop
Ulf Dittmer
,
Rancher
Jan 17, 2009 10:58:50
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
That's odd. I saw that post earlier today, and there
was
a question. I think something about the performance of the loop being bad when iterating a 1000 times.
Campbell Ritchie
,
Marshal
staff
Jan 18, 2009 06:48:04
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Please don't anybody edit their posts like that. It makes the remainder of the
thread
unintelligible.
Post by:
autobot
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
This thread has been viewed 1045 times.
Similar Threads
SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections
Simple ArrayList conundrum?
Question about Random Number Generation...
Mixing Generics and Legacy code question
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:59:47
.