Recently I wrote a clustering algorithm. The input dataset contains two-dimensional coordinate points. My data contains many repeated points. We need to perform preprocessing to remove these repeated points. Baidu and Google found a solution after a while, hoping to help those who read this article.
1. Data File points.txt. The file content and format are as follows:
2. Add a point object to save the point data. The most important thing is to implement the equal method, which is used to remove duplicate objects.
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 + ">" ;}// this method is the key 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 ;}}
3rd, the vertex information is read from the points.txt file and saved in the list object.
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 (); // filter duplicate Point Object List <point> List = new arraylist <point> (); For (Object O: lst) {If (! List. Contains (O) {list. Add (point) O) ;}} return list ;}