最近寫聚類演算法,輸入的資料集是一些二維點座標,我的資料有很多重複的點,需要做一下預先處理,去掉這些重複點。百度和Google一陣後,找到瞭解決方法,希望對閱讀本文的人有協助。
1、 資料檔案points.txt,檔案內容和格式如下:
2、添加一個Point對象,用來儲存點資料。最重要的是要實現equal方法,這個在去除重複對象時會用到。
public class Point { private double x; private double y; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public Point(){ x=0; y=0; } public Point(double x,double y){ this.x=x; this.y=y; } public Point(String str){ String[] p=str.split(","); this.x=Double.valueOf(p[0]); this.y=Double.valueOf(p[1]); } public String print(){ return "<"+this.x+","+this.y+">"; } //這個方法是關鍵 public boolean equals(Object obj) {if (obj == null) {return false;}if (this == obj) {return true;}Point other = (Point) obj;if (this.getX() == other.getX() && this.getY() == other.getY()) {return true;}return false; }}
3、從檔案points.txt中讀取點資訊,然後儲存在List對象中。
public List<Point> getPointsList() throws IOException{ List<Point> lst=new ArrayList<Point>(); String txtPath="points.txt"; BufferedReader br=new BufferedReader(new FileReader(txtPath)); String str=""; while((str=br.readLine())!=null && str!=""){ lst.add(new Point(str)); } br.close(); //過濾重複的Point對象 List<Point> list = new ArrayList<Point>(); for (Object o:lst) { if (!list.contains(o)) { list.add((Point)o); } } return list;}