java使用嵌套三目表达式进行嵌套HashMap赋值

//XuXuZhuFuShopBuyOutLets.java

import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.SimpleDateFormat;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileOutputStream;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

public class XuXuZhuFuShopBuyOutLets{
    private static ArrayList<String> createDateList(String Date1, String Date2) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        Date date1 = sdf.parse(Date1);
        Date date2 = sdf.parse(Date2);
        ArrayList<String> datelist = new ArrayList<String>();
        cal.setTime(date1);
        while (cal.getTime().getTime()<=date2.getTime()){
            datelist.add(sdf.format(cal.getTime()));
            cal.add(Calendar.DATE,1);
            //System.out.println(sdf.format(cal.getTime()));
        }   //for(Object xx: datelist){
        //for(Object xx: datelist){
        //    System.out.println((String)xx);
        //}
        return datelist;
    }
    private static JSONObject getFartherSerMapDict(File FileMap) throws Exception{
        JSONObject sermapdict = new JSONObject();
        String temp = null;
        BufferedReader br = new BufferedReader(new FileReader(FileMap));
        while((temp=br.readLine())!=null){
            sermapdict = JSONObject.fromObject(temp);
        }
        //System.out.println(sermapdict.toString());
        return sermapdict;
    }
    @SuppressWarnings("unchecked")
    private static HashMap<String, HashSet> getSerDauDict(File FileAct, JSONObject SerMapDict) throws Exception{
       HashMap<String, HashSet> serdaudict = new HashMap<String, HashSet>();
       String temp = null;
       BufferedReader br = new BufferedReader(new FileReader(FileAct));
       while((temp=br.readLine())!=null){
           String[] line = temp.split("\t");
           String uid = line[1];
           String server = (String)SerMapDict.get(line[2]);
           if (serdaudict.containsKey(server)){
               serdaudict.get(server).add(uid);
           }else{
               HashSet<String> hs = new HashSet<String>();
               hs.add(uid);
               serdaudict.put(server, hs);
           }
       }
       //for (HashSet xx: serdaudict.values()){
       //    System.out.println(xx.size());
       //}
       return serdaudict;
    }
    @SuppressWarnings("unchecked")
    private static HashMap<String, HashMap> getShopOutletsDict(File FileSpend, JSONObject SerMapDict) throws Exception{
        HashMap<String, HashMap> shopoutletsdict = new HashMap<String, HashMap>();
        String temp = null;
        BufferedReader br = new BufferedReader(new FileReader(FileSpend));
        while((temp=br.readLine())!=null){
            String[] line = temp.split("\t");
            String uid = line[1];
            String server = (String)SerMapDict.get(uid.substring(0,uid.length()-7));
            String goodstype = line[7];
            //HashMap<String, Object> args = new HashMap<String, Object>(line[13]);
            JSONObject args = JSONObject.fromObject(line[13]);  //注意此处将字典形式的字符串,转变为JSONObject。
            if (goodstype.equals("shop.outlets_buy")){
                String shopid = ((JSONArray)args.get("shop_id")).getString(0);
               
                if (shopoutletsdict.get(server)!=null){
                    //(shopoutletsdict.get(server).get(shopid)==null)?shopoutletsdict.get(server).put(shopid, 1):shopoutletsdict.get(server).get(shopid)=shopoutletsdict.get(server).get(shopid)+1;
                    //if(shopoutletsdict.get(server).containsKey(shopid)){
                    //    shopoutletsdict.get(server).put(shopid, (int)shopoutletsdict.get(server).get(shopid)+1);
                    //}else{
                    //    shopoutletsdict.get(server).put(shopid,1);
                    //}
                     shopoutletsdict.get(server).put(shopid, (shopoutletsdict.get(server).get(shopid)==null)?1:(int)shopoutletsdict.get(server).get(shopid)+1); //使用嵌套三目表达式其实现功能与上面注释的部分相同
                }else{
                    shopoutletsdict.put(server, new HashMap<String, Integer>());
                    shopoutletsdict.get(server).put(shopid,1);
                }
            }
        }
        return shopoutletsdict;
    }
    public static void main(String[] args) throws Exception{
        String date1 = "20160315";
        String date2 = "20160321";
        ArrayList datelist = createDateList(date1, date2);
        String path = "/home/data/superhero_vietnam/";
        String acthead =  path + "active_user/dw_superhero_activeuser_";
        String spendhead = path + "spendlog/spendlog_";
        String sermaphead = path + "viso_config/father_server_map_";
        for (Object day : datelist){
            File fileact = new File(acthead + (String)day);
            File filespend = new File(spendhead + (String)day);
            File filesermap = new File(sermaphead + (String)day);
            JSONObject sermapdict = getFartherSerMapDict(filesermap);
            HashMap<String, HashSet> serdaudict = getSerDauDict(fileact, sermapdict);
            HashMap<String, HashMap> shopoutletsdict = getShopOutletsDict(filespend, sermapdict);
            Iterator it = serdaudict.keySet().iterator();
            while(it.hasNext()){
                String ser = (String) it.next();
                int dau = serdaudict.get(ser).size();
                System.out.print(day.toString()+"\t"+ser+"\t"+dau);
                for (String str:new String[]{"1","2","3","4","5","6"}){
                    //int person = 0;
                    //if (shopoutletsdict.containsKey(ser)){
                    //    if (shopoutletsdict.get(ser).get(str)==null){
                    //        person = 0;
                    //    }else{
                    //        person = (int)shopoutletsdict.get(ser).get(str);
                    //    }
                    //}
                    int person = (shopoutletsdict.get(ser)==null)?0:((shopoutletsdict.get(ser).get(str)==null)?0:(int)shopoutletsdict.get(ser).get(str)); //使用嵌套三目表达式,其实现功能与上面注释的相同。
                    System.out.print("\t"+person);
                }
                System.out.println();
            }
        }
    }
}
































转载于:https://my.oschina.net/lCQ3FC3/blog/645998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值