導航:首頁 > 編程語言 > java數據對比

java數據對比

發布時間:2022-04-29 06:06:00

java比較兩張表相同欄位的數據

雙循環吧
確認A是不重復的,可以比較到相同的時候刪除一個B相同值,減少後面的對比。

⑵ java分別讀取兩個txt文件里的數據進行比較,分別列出這兩個文件中共有的數據,並統計每種數據的數量

我就不給你寫代碼了,給你說下思路。

1.用BufferedReader
分別讀取這2個文件,循環讀取,一次讀取1行
將讀取的數據分別放入2個List中,分別為list1,list2。

定義變數sameCount,用於記錄1,2都有的數據條數。
2.循環list1,取出其中的每一條,並與list2中對比,相同則sameCount++;

至於輸出1,2中各有多少條數據就很簡單了,就是list1.size()和list2.size();

給你一點代碼提示:
BufferedRreader br = new BufferedReader(new FileReader(new File("1.txt"));
String temp = null;
List list1 = new ArrayList();

while((temp = br.readLline() ) != null) {
list1.add(temp);
}

br.close();

這就是讀取1.txt的內容,並放入list1中。
因為你要分別讀取1,2的內容,所以你可以將這段代碼寫一個方法。
參數為文件名和要放入的List。如:readFile(String filename,List list);

對比就不要我說了吧。readFile()這個方法調用2次,分別讀取1,2數據後得到list1,list2。
接下來循環1吧。
定義變數Integer sameCount = 0;
for(String s : list1) {
for (String ss : list2) {
if (s.equals(ss)) {
sameCount++;
}
}
}

代碼給你寫的差不多了,望採納。

⑶ 怎麼用Java對比兩份數據,然後提出相同的數據。(JAVA自學者)

/**
* Title:GetSameData.java
* Description:
* Copyright: Owner 2009
* CreateTime:Sep 10, 2009 10:57:14 AM
* @author Owner
* @data Sep 10, 2009
* @version 1.0
*/
package file;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* Title: GetSameData
* Description:
* Company: LTGames
* @author Owner
* @date Sep 10, 2009
*/
public class GetSameData {

/**
* Map的key為Name, value為包含standard的文件名的數組。
*/
private Map<String, List<String[]>> fileDataMap = new HashMap<String, List<String[]>>();

/**
* 從指定的文件夾載入數據。
* Title: initFilesData
* Description:
* @author Owner
* @param folder
* @return
* @throws IOException
*/
public void initFilesData(String folderPath) throws IOException{
File folder = new File(folderPath);
if(folder.isFile()){
System.out.println("輸入路徑不是文件夾,請重新輸入路徑");
return;
}
File[] files = folder.listFiles();
for(File file : files){
loadData(file);
}
}

private void loadData(File file) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String lineString;
String[] lineDatas;
List<String[]> dataList;
String fileName = file.getName();
while((lineString = bufferedReader.readLine()) != null){
lineDatas = lineString.split(",");
// 找到數據行
if(lineDatas.length == 15 && !lineDatas[0].equals("Ilmn ID")){
// 找到對應的list對象
if((dataList = fileDataMap.get(lineDatas[1])) == null){
dataList = new ArrayList<String[]>();
}
// 將數據保存到list當中
dataList.add(new String[]{lineDatas[14], fileName});
// 將list保存到Map當中
fileDataMap.put(lineDatas[1], dataList);
}
}
}

public static void main(String[] args) {
GetSameData o = new GetSameData();
try {
o.initFilesData("d:/a");
Map<String, List<String[]>> fileDataMap = o.getFileDataMap();
Iterator<String> key = fileDataMap.keySet().iterator();
while(key.hasNext()){
String id = key.next();
if(fileDataMap.get(id).size() > 1){
for(String[] dates : fileDataMap.get(id)){
System.out.println(id + " " + dates[0] + " " + dates[1]);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Map<String, List<String[]>> getFileDataMap() {
return fileDataMap;
}

}
已經按照你的數據個數修改完畢。

⑷ java 數據比對功能 求幫助

有很多種方法

⑸ Java 兩個資料庫內容比對

可以把讀取出來的數據放在List里,然後進行比對,當ID和name不相等時,放到第三個list里,然後輸出第三個list里的內容。

⑹ 幾種Java讀寫數據流性能對比

public static int FileOutputStreamTime = 0; 2 public static int BufferedOutputStreamTime = 0; 3 public static int FileWriterTime = 0; 4 public static int FileInputStreamTime = 0; 5 public static int BufferedInputStreamTime = 0; 6 public static int FileReaderTime = 0; 7 8 public static void write(String filePath, String content){ 9 FileOutputStream out = null; 10 FileOutputStream outStr = null; 11 BufferedOutputStream buf = null; 12 FileWriter fw = null; 13 File f = new File(filePath); 14 15 try { 16 //Test FileOutputStream 17 long begin1 = System.currentTimeMillis();
18 out = new FileOutputStream(f); 19 out.write(content.getBytes()); 20 out.close(); 21 long end1 = System.currentTimeMillis(); 22 FileOutputStreamTime += end1 - begin1; 23 24 //Test BufferedOutputStream 25 long begin2 = System.currentTimeMillis(); 26 outStr = new FileOutputStream(f); 27 buf = new BufferedOutputStream(outStr); 28 buf.write(content.getBytes()); 29 buf.flush(); 30 buf.close(); 31 long end2 = System.currentTimeMillis(); 32 BufferedOutputStreamTime += end2 - begin2; 33 34 //Test FileWriter 35 long begin3 = System.currentTimeMillis(); 36 // the second parameter "true",Whether or not a file will be covered 37 // or appended. 38 fw = new FileWriter(f); 39 // fw = new FileWriter("d:/test/testwrite/add2.txt",true); 40 fw.write(content); 41 fw.close(); 42 long end3 = System.currentTimeMillis(); 43 FileWriterTime += end3 - begin3; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } finally { 47 try { 48 fw.close(); 49 buf.close(); 50 outStr.close(); 51 out.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void read(String filePath){ 59 FileInputStream in = null; 60 BufferedInputStream buf = null; 61 FileReader reader = null; 62 BufferedReader br = null; 63 StringBuffer sb = new StringBuffer(); 64 65 try { 66 //Test FileInputStream 67 long begin1 = System.currentTimeMillis(); 68 File f = new File(filePath); 69 in = new FileInputStream(f); 70 int len1 = 512; 71 byte[] bytes1 = new byte[len1]; 72 73 while ((len1 = in.read(bytes1, 0, len1)) != -1) { 74 if(len1 < 512){ 75 byte[] tmpBuf = new byte[len1]; 76 System.array(bytes1, 0, tmpBuf, 0, len1);
77 sb.append(new String(tmpBuf)); 78 tmpBuf = null; 79 }else{ 80 sb.append(new String(bytes1)); 81 } 82 } 83 84 in.close(); 85 long end1 = System.currentTimeMillis(); 86 FileInputStreamTime += end1 - begin1; 87 88 //Test BufferedInputStream 89 long begin2 = System.currentTimeMillis(); 90 int len2 = 512; 91 byte[] bytes2 = new byte[len2]; 92 buf = new BufferedInputStream(new FileInputStream(f)); 93 while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94 if(len2 < 512){ 95 byte[] tmpBuf = new byte[len2]; 96 System.array(bytes2, 0, tmpBuf, 0, len2);
97 sb.append(new String(tmpBuf)); 98 tmpBuf = null; 99 }else{100 sb.append(new String(bytes2));101 }102 }103 104 buf.close();105 long end2 = System.currentTimeMillis();106 BufferedInputStreamTime += end2 - begin2;107 108 //Test FileReader109 long begin3 = System.currentTimeMillis();110 reader = new FileReader(f);111 br = new BufferedReader(reader);112 String str;113 while ((str = br.readLine()) != null) {114 sb.append(str);115 }116 br.close();117 reader.close();118 long end3 = System.currentTimeMillis();119 FileReaderTime += end3 - begin3;120 121 } catch (Exception e) {122 e.printStackTrace();123 } finally {124 try {125 br.close();126 reader.close();127 in.close();128 buf.close();129 } catch (Exception e) {130 e.printStackTrace();131 }132 }133 }

⑺ JAVA比較數值的大小

publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);

intnum;
intmax=0;
intmin=0;

while(true){
System.out.println("請輸入一個整數「輸入0時結束」:");
num=input.nextInt();
if(num==0){
break;
}
if(num>max){
max=num;
}
if(num<min){
min=num;
}
}
System.out.print("最大值為:"+max+"最小值為:"+min);
}

⑻ JAVA文件數據對比怎麼差距巨大

首先,如果你用ArrayList來放的話,請先判斷數據條數,new對象時用帶容量參數的構造方法。
例如,有100000條數據, new ArrayList(100000);
這要在添加數據時效率大很多,原理是ArrayList的內部實現是用數組的,數組是不可變的對象。詳細的就不說了。
如果用LinkedList的話,在查找時,用iterator來遍歷。

⑼ Java中如何比較兩種數據類型為同一種類型

public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }

這是String中equals的實現,b1是Boolean類型的,不是instanceof String,所以返回了false,然後就沒有輸出了。

⑽ java中怎麼對兩組大批量數據進行比較

/**
* 使用Map和List的特性進行匹配:
* Map為key-value結構,不能放重復數據
* List可以放重復數據
* 使用String型id做key,List<Person>做value
* 遍歷List<String>, map.get(String)則取出id == str 的list
*/

閱讀全文

與java數據對比相關的資料

熱點內容
數學奇跡神奇運演算法 瀏覽:359
大廠的程序員的水平如何 瀏覽:700
遺傳演算法入門經典書籍 瀏覽:878
源碼炮台腳本 瀏覽:620
在位編輯命令 瀏覽:347
曲式分析基礎教程pdf 瀏覽:14
php生成靜態html頁面 瀏覽:964
怎麼分割pdf 瀏覽:812
壓縮垃圾報警器 瀏覽:629
小公司一般都用什麼伺服器 瀏覽:968
java獲取時間gmt時間 瀏覽:820
為什麼csgo一直連接不到伺服器 瀏覽:504
安卓登ins需要什麼 瀏覽:836
機器人演算法的難點 瀏覽:226
全自動化編程 瀏覽:727
程序員高薪限制 瀏覽:693
壓縮圖片壓縮 瀏覽:75
美國發明解壓魔方 瀏覽:302
電腦怎麼備案網上伺服器 瀏覽:515
旅行商問題Python寫法 瀏覽:953