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。