# Point Collections

1. create PointLib package

– Point 클래스를 넣는다.
package PointLib;

import java.util.*;

public class Point implements Comparable<Point> {
protected double x;
protected double y;

public Point()
{
this(0.0, 0.0);
}

public Point(double x, double y)
{
this.x = x;
this.y = y;
}

public double getX()
{
return x;
}

public double getY()
{
return y;
}

public int compareTo(Point other)
{
return Double.compare(this.getX(), other.getX()); //ascending order by X
}

public static Comparator<Point> YComparator = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
return Double.compare(p1.getY(), p2.getY()); //ascending order by Y
}
};

@Override
public int hashCode() // hashCode method override
{
return Double.valueOf(this.x).hashCode() * Double.valueOf(this.y).hashCode();
}

@Override
public boolean equals(Object obj) // equals method override
{
if (obj instanceof Point)
return equals((Point)obj);
return false;
}

public boolean equals(Point other) // equals method override
{
if (this.x == other.x && this.y == other.y)
return true;
return false;
}

@Override
public String toString() // toString method override
{
return “(” + this.x + “,” + this.y + “)”;
}

}

2. create PointCollectionLab package
– PointTest 클래스를 넣는다.
package PointTestApp;

import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;

import PointLib.Point;

class PointTest  {

public static void main(String[] args) {
new PointTest();
}

public PointTest()
{
Point[] pList = new Point[10];
for (int i=0; i<10; i++) {
pList[i] = new Point(i, i+1);
}
System.out.println(“Point!!!”);
Arrays.stream(pList).forEach((s) -> System.out.println(s));

System.out.println(“Point SORT BY X (DEFAULT)!!!”);
Arrays.sort(pList);
Arrays.stream(pList).forEach((s) -> System.out.println(s));

System.out.println(“Person SORT by Y!!!”);
Arrays.sort(pList, Point.YComparator);
Arrays.stream(pList).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
System.out.println(“Point SORT BY X (anonymous method)!!!”);
Arrays.sort(pList, new Comparator<Point>() {
public int compare(Point p1, Point p2) {
return Double.compare(p1.getX(), p2.getX()); //ascending order by X
}
});
Arrays.stream(pList).forEach((s) -> System.out.println(s));

System.out.println(“Point SORT by Y (anonymous method)!!!”);
Arrays.sort(pList, new Comparator<Point>() {
public int compare(Point p1, Point p2) {
return Double.compare(p1.getY(), p2.getY()); //ascending order by Y
}
});
Arrays.stream(pList).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
System.out.println(“Point SORT BY X (lambda)!!!”);
Arrays.sort(pList, (Point p1, Point p2)
-> Double.compare(p1.getX(), p2.getX())  );
Arrays.stream(pList).forEach((s) -> System.out.println(s));

System.out.println(“Point SORT by Y (lambda)!!!”);
Arrays.sort(pList, (Point p1, Point p2)
-> Double.compare(p1.getY(), p2.getY()) );
Arrays.stream(pList).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
ArrayList pointList = new ArrayList();
for (int i=0; i<10; i++) {
}
pointList.forEach((s) -> System.out.println(s));
/*
System.out.println(“Point SORT BY X (lambda)!!!”);
pointList.sort((Point pp1, Point pp2)
-> Double.compare(pp1.getX(), pp2.getX())
);
pointList.forEach((s) -> System.out.println(s));

System.out.println(“Point SORT by Y (lambda)!!!”);
pointList.sort((Point p1, Point p2) ->
Double.compare(p1.getY(), p2.getY())
);
pointList.forEach((s) -> System.out.println(s));
*/
System.out.println(“pointList[2]=” + pointList.get(2));

Iterator it = pointList.iterator(); // Iterator 객체 얻기
while(it.hasNext()) {
Object p = it.next();
System.out.println(p);
}

///////////////////////////////////////////////////////////////////////

Vector vList= new Vector();
for (int i=0; i<10; i++) {
}
vList.forEach((s) -> System.out.println(s));

vList.removeElementAt(vList.size() – 1); // delete the last element in vList
for (int i=0; i<vList.size(); i++) {
Object p = vList.get(i);
System.out.println(p);
}

///////////////////////////////////////////////////////////////////////

Hashtable<Integer, Point> hList = new Hashtable<Integer, Point>();
for (int i=0; i<10; i++) {
hList.put(i, new Point(i*1000, i*1000+1));
}
hList.forEach((i, s) -> System.out.println(s));

System.out.println(“hList contains point (1000, 1001) is ” + hList.contains(new Point(1000, 1001)));
hList.put(10, new Point(10, 10));
hList.forEach((i, s) -> System.out.println(s));
hList.remove(9); // remove item by key
hList.forEach((i, s) -> System.out.println(s));

System.out.println(“Key,Value print”);
Set keys = hList.keySet();
Iterator itr = keys.iterator();
while(itr.hasNext()) {
Integer key = (Integer) itr.next();
Point value = hList.get(key);
System.out.println(“(” + key + “,” + value + “)”);
}

///////////////////////////////////////////////////////////////////////
System.out.println(“/////////////”);
}
}