用Java寫演算法技巧(1)去掉List中的重複對象

來源:互聯網
上載者:User

    最近寫聚類演算法,輸入的資料集是一些二維點座標,我的資料有很多重複的點,需要做一下預先處理,去掉這些重複點。百度和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;}

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.