Writing algorithms using Java (1) Removing repeated objects in the list

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.