Search in sources :

Example 1 with PointComparator

use of hr.fer.oop.recap2.task3.PointComparator in project Lectures by FER-OOP.

the class MainList method main.

public static void main(String[] args) {
    List<Point> list = new ArrayList<>();
    list.add(new Point(-5, 12));
    list.add(new Point(3, -4));
    list.add(new Point(12, 9));
    list.add(new Point(3, 4));
    list.add(new Point(4, 3));
    list.add(new Point(-9, 12));
    list.add(new Point(-5, -12));
    System.out.println("Original list:\n" + list);
    Comparator<Point> comp = new PointComparator();
    list.sort(comp);
    System.out.println("Sorted by distance:\n" + list);
    list.sort(Comparator.naturalOrder());
    System.out.println("Sorted by natural order:\n" + list);
}
Also used : ArrayList(java.util.ArrayList) Point(hr.fer.oop.recap2.task2.Point)

Example 2 with PointComparator

use of hr.fer.oop.recap2.task3.PointComparator in project Lectures by FER-OOP.

the class Main method main.

public static void main(String[] args) {
    List<Point> list = new ArrayList<>();
    list.add(new Point(-5, 12));
    list.add(new Point(3, -4));
    list.add(new Point(12, 9));
    list.add(new Point(3, 4));
    list.add(new Point(4, 3));
    list.add(new Point(-9, 12));
    list.add(new Point(-5, -12));
    DistanceFromOrigin dist = new DistanceFromOrigin();
    List<String> newlist = list.stream().filter(new QuadrantPredicate(true, true, false, true)).sorted(new PointComparator().reversed()).map(p -> String.format("dist(%s) = %.2f", p, dist.apply(p))).collect(Collectors.toList());
    System.out.println(newlist);
}
Also used : List(java.util.List) PointComparator(hr.fer.oop.recap2.task3.PointComparator) QuadrantPredicate(hr.fer.oop.recap2.task2.QuadrantPredicate) Point(hr.fer.oop.recap2.task2.Point) DistanceFromOrigin(hr.fer.oop.recap2.task2.DistanceFromOrigin) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) DistanceFromOrigin(hr.fer.oop.recap2.task2.DistanceFromOrigin) QuadrantPredicate(hr.fer.oop.recap2.task2.QuadrantPredicate) ArrayList(java.util.ArrayList) Point(hr.fer.oop.recap2.task2.Point) PointComparator(hr.fer.oop.recap2.task3.PointComparator)

Example 3 with PointComparator

use of hr.fer.oop.recap2.task3.PointComparator in project Lectures by FER-OOP.

the class Main method main.

public static void main(String[] args) {
    List<Point> list = new ArrayList<>();
    addPoints(list);
    Map<Integer, Set<Point>> map = new TreeMap<>();
    Comparator<Point> comp = new PointComparator();
    comp = comp.thenComparing(Comparator.naturalOrder());
    map.put(1, new TreeSet<>(comp));
    map.put(2, new TreeSet<>(comp));
    map.put(3, new TreeSet<>(comp));
    map.put(4, new TreeSet<>(comp));
    Function<Point, Integer> quadrantFunction = QuadrantCalculator::quadrant;
    for (var point : list) {
        int quadrant = quadrantFunction.apply(point);
        if (quadrant != 0)
            map.get(quadrant).add(point);
    }
    System.out.println(map);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Point(hr.fer.oop.recap2.task2.Point) TreeMap(java.util.TreeMap) PointComparator(hr.fer.oop.recap2.task3.PointComparator) Point(hr.fer.oop.recap2.task2.Point)

Aggregations

Point (hr.fer.oop.recap2.task2.Point)3 ArrayList (java.util.ArrayList)3 PointComparator (hr.fer.oop.recap2.task3.PointComparator)2 DistanceFromOrigin (hr.fer.oop.recap2.task2.DistanceFromOrigin)1 QuadrantPredicate (hr.fer.oop.recap2.task2.QuadrantPredicate)1 List (java.util.List)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1