文件中數據和db中數據進行比較(java實現)

文件中數據和db中數據進行比較(java實現)

以一個key作爲比較關鍵字,兩邊不一樣的數據輸出到一個map中(1,2,4,6)。

文件   db
1    
2    
3   3
    4
5   5
    6

每行內容,定義如下

public class Items {

    private int id;
    private String name;
    private double price;
    private Date createtime;
    private String detail;

    public Items(int id, String name, double price, Date createtime, String detail) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.createtime = createtime;
        this.detail = detail;
    }
// get set方法省略
}

主代碼如下

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class test {

	public static void main(String[] args) {
		compMethod();
	}
	
	private static void compMethod(){
		Map<String, Items> mapfile = new HashMap<String, Items>();
		Map<String, Items> mapdb = new HashMap<String, Items>();
		Items file1 = new Items(0, "2", 0, new Date(), null);
		Items file2 = new Items(0, "1", 0, new Date(), null);
		Items file3 = new Items(0, "5", 0, new Date(), null);
		Items file5 = new Items(0, "3", 0, new Date(), null);
		Items db2 = new Items(0, "3", 0, new Date(), null);
		Items db3 = new Items(0, "6", 0, new Date(), null);
		Items db4 = new Items(0, "5", 0, new Date(), null);
		Items db5 = new Items(0, "4", 0, new Date(), null);
		// 文件的數據
		mapfile.put(file1.getName(), file1);
		mapfile.put(file2.getName(), file2);
		mapfile.put(file3.getName(), file3);
		mapfile.put(file5.getName(), file5);
		// db的數據
		mapdb.put(db2.getName(), db2);
		mapdb.put(db3.getName(), db3);
		mapdb.put(db4.getName(), db4);
		mapdb.put(db5.getName(), db5);
		// 排序
		Map<String, Items> mapfilesort = mapfile.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oleValue, newValue) -> oleValue, LinkedHashMap::new));
//        System.out.println(mapfilesort);
        Map<String, Items> mapdbsort = mapdb.entrySet().stream()
        		.sorted(Map.Entry.comparingByKey())
        		.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
        				(oleValue, newValue) -> oleValue, LinkedHashMap::new));
//        System.out.println(mapdbsort);

        // 文件和db比較不同的數據,存放
        LinkedHashMap<String, Items> resultMap = new LinkedHashMap<String, Items>();
        
        Iterator<Entry<String, Items>> files = mapfilesort.entrySet().iterator();
        Iterator<Entry<String, Items>> dbs = mapdbsort.entrySet().iterator();
        
        // 第一條數據
        String key1 = keyMethod(files);
        String key2 = keyMethod(dbs);
        // 循環至兩邊都沒有數據爲止
        while(key1!=null || key2!=null) {
        	// 文件沒有數據了
        	if(key1==null) {
        		resultMap.put(key2, mapdbsort.get(key2));
        		key2 = keyMethod(dbs);
        		continue;
        	}
        	// db沒有數據了
        	if(key2==null) {
        		resultMap.put(key1, mapfilesort.get(key1));
        		key1 = keyMethod(files);
        		continue;
        	}
        	// 誰小,誰是兩邊不共同擁有的數據
	        if(key1.compareTo(key2)>0) {
	        	resultMap.put(key2, mapdbsort.get(key2));
	        	key2 = keyMethod(dbs);
	        }else if(key1.compareTo(key2)<0) {
	        	resultMap.put(key1, mapfilesort.get(key1));
	        	key1 = keyMethod(files);
	        }else {
	        	// 兩邊共同擁有的數據
	        	key1 = keyMethod(files);
	        	key2 = keyMethod(dbs);
	        }
        }
        
		System.out.println(resultMap);
	}

// 移動迭代器的指針
	private static String keyMethod(Iterator<Entry<String, Items>> iterator){
		String key = null;
		if(iterator.hasNext()) {
			Entry<String, Items> entry = iterator.next();
            key = entry.getKey();
    	}
		return key;
	}
	
// db返回值是set集合的情況下,使用下記方法取得移動指針
//	private static String keyMethod2(ResultSet set){
//		String key = null;
//		try {
//			if(set.next()) {
//			    key = set.getString("name");
//			}
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return key;
//	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章