Hashmap笔记

8 篇文章 0 订阅
import java.io.*;
import java.util.HashMap;

/**
 * Created by henson on 18-4-1.
 */
public class Label {
    public void label(String path, String outpath){
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outpath), "UTF-8"));
            String aline = br.readLine();
            String[] line;
            int row_num=1;
            while (aline != null) {
                line = aline.trim().split("\\s+");
                for (int i = 0; i < line.length; i++) {
                    bw.write(line[i] +" "+ row_num+"\n");
                }
                aline = br.readLine();
                row_num++;
            }
            bw.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


//    private HashMap<Integer, Integer> exchange = new HashMap<Integer, Integer>();
//
//    public void index_num(String index, String node_label, String community, String sample_num ) {
//        try {
//            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(index), "UTF-8"));
//            BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream(node_label), "UTF-8"));
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(community), "UTF-8"));
//            String aline1 = br1.readLine();
//            String aline2 = br2.readLine();
//            String[] line1;
//            String[] line2;
//            Integer a1, b1,a2,b2;
//            while (aline1 != null) {
//                line1 = aline1.split(" ");
//                a1 = Integer.valueOf(line1[0]);
//                b1 = Integer.valueOf(line1[1]);
//                while (aline2!=null){
//                    line2=aline2.split(" ");
//                    a2=Integer.valueOf(line2[0]);
//                    b2=Integer.valueOf(line2[1]);
//                    if (a1==a2){
//                        bw.write(""+b2+"\n");
//                    }
//                }
//                aline1 = br1.readLine();
//            }
//            br1.close();
//            br2.close();
//            bw.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
// 定向思维,old school 太慢了

    public static void main(String[] args){action();}

    private static void action(){
        //int sample_num=1;
        for (int sample_num =1;sample_num<23;sample_num++){

            String label = "/home/henson/Desktop/ICC/sample"+sample_num+"_SHII/label.csv";;          //label文件,给节点编社区号
            String node_label = "/home/henson/Desktop/ICC/sample"+sample_num+"_SHII/node_label.txt";        //生成节点对应的社区编号文件
            //String index = "/home/henson/Desktop/ICC/index_"+sample_num+".txt";                            //通过index查找节点
            Label test=new Label();
            test.label(label,node_label);
            //test.index_num(index,node_label,community,sample_num);
        }
    }
}
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by henson on 18-4-2.
 */

import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Community {
    private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    private Map<Integer,Integer> sortMap = new TreeMap<Integer, Integer>();
    public void createMap1(String path) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            String aline = br.readLine();
            String[] line;
            Integer a, b;
            while (aline != null) {
                line = aline.split(" ");
                a = Integer.valueOf(line[0]);
                b = Integer.valueOf(line[1]);
                map.put(a, b);
                aline = br.readLine();
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void change(String path) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            String aline = br.readLine();
            String[] line;
            Integer a, b;
            while (aline != null) {
                line = aline.split(" ");
                a = Integer.valueOf(line[0]);
                b = Integer.valueOf(line[1]);
                a=map.get(a);   //获取 map 当前key为a对应的value值
                sortMap.put(a, b);
                aline = br.readLine();
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Map<Integer,Integer> sortMapByKey(Map<Integer, Integer> sortMap) {
        if (sortMap == null || sortMap.isEmpty()) {
            return null;
        }

        Map<Integer, Integer> map = new TreeMap<Integer, Integer>(
                new MapKeyComparator());

        map.putAll(sortMap);

        return map;

    }

    public void out(Map<Integer,Integer> sortMap, String outpath) {
        Map<Integer, Integer> resultMap = sortMapByKey(sortMap);    //按Key进行排序
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outpath), "UTF-8"));
            for (Map.Entry<Integer, Integer> entry : resultMap.entrySet()) {    //将resultMap里的每一个键值对key,value取出来封装成一个Entry对象在存到一个Set里面
                bw.write(""+entry.getValue());
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Map<Integer, Integer> getSortMap() {
        return sortMap;
    }

    public static void main(String[] args) {
        action();
    }

    private static void action() {
        //String num_sample="1";
        for (int num_sample =1;num_sample<23;num_sample++){
            String index = "/home/henson/Desktop/ICC/index_"+num_sample+".txt";
            String com = "/home/henson/Desktop/ICC/sample"+num_sample+"_SHII/node_label.txt";
            String sort = "/home/henson/Desktop/ICC/sample"+num_sample+"_SHII/coummunity.txt";
            Community comm = new Community();
            comm.createMap1(index);
            comm.change(com);
            Map<Integer, Integer> map = comm.getSortMap();
            comm.out(map, sort);
        }


    }
}

class MapKeyComparator1 implements Comparator<Integer> {  //继承比较器

    @Override
    public int compare(Integer a, Integer b) {

        return a.compareTo(b);  //默认为升序排列   降序排列为:return b.compareTo(a);
    }
}

最近用hashmap整理数据,简直不能太方便
后知后觉的傻气

论整理思路文档的重要性,在整理过程中才能知道,思路有多少bug。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值