SHARE
TWEET

Untitled

a guest Jun 19th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. abstract class Clusterable<T> {
  3.     protected long id;
  4.     abstract long getID();
  5.     abstract double getDistance(T element);
  6. }
  7.  
  8. class Cluster<T extends Clusterable> {
  9.     private Map<Long, T> elements;
  10.  
  11.     public Cluster(){
  12.         elements = new HashMap<>();
  13.     }
  14.  
  15.     void addItem(T t){
  16.         elements.put(t.getID(), t);
  17.     }
  18.  
  19.     void near(long id, int top){
  20.         T element = elements.get(id);
  21.         elements.values()
  22.                 .stream()
  23.                 .sorted(Comparator.comparingDouble(element::getDistance))
  24.                 .skip(1) // se skoka sam sebe, nz dali vaka treba ili ne
  25.                 .limit(top)
  26.                 .forEach(System.out::println);
  27.     }
  28. }
  29.  
  30. class Point2D extends Clusterable<Point2D> {
  31.     private float x, y;
  32.  
  33.     public Point2D(long id, float x, float y){
  34.         this.id = id;
  35.         this.x = x;
  36.         this.y = y;
  37.     }
  38.  
  39.     @Override
  40.     long getID() {
  41.         return id;
  42.     }
  43.  
  44.     @Override
  45.     double getDistance(Point2D element) {
  46.         return Math.sqrt((x - element.x) * (x - element.x) + (y - element.y) * (y - element.y));
  47.     }
  48.  
  49.     @Override
  50.     public String toString(){
  51.         return String.format("%d: (%.0f,%.0f)",id,x,y);
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top