TreeSet矩形比较器(TreeSet Rectangle Comparator)
快速问题,我在创建自己的TreeSet矩形比较器时遇到了很多麻烦。 这是我的代码,但我非常失落。 我想比较矩形的区域并按升序对它们进行排序。
import java.util.TreeSet; import java.awt.Rectangle; import java.util.Comparator; public class RectComp implements Comparator<Rectangle> { private double width; private double height; public RectComp(Comparator comp) { this.width = width; this.height = height; } public double getArea() { return width * height; } public int compare(Rectangle r1, Rectangle r2) { double diff = r1.getArea() - r2.getArea(); if (diff < 0) return -1; else if (diff == 0) return 0; else return 1; } public static void main(String[] args) { TreeSet<Rectangle> set = new TreeSet<>(new RectComp()); set.add(new Rectangle(1, 10)); set.add(new Rectangle(10, 1)); set.add(new Rectangle(5, 3)); set.add(new Rectangle(5, 2)); set.add(new Rectangle(5, 1)); for (Rectangle r : set) { System.out.println(r); } } }
Quick question, Im having a lot of trouble creating my own TreeSet rectangle comparator. Here is the code I have, but i'm extremely lost. I want to compare the areas of the rectangles and sort them in ascending order.
import java.util.TreeSet; import java.awt.Rectangle; import java.util.Comparator; public class RectComp implements Comparator<Rectangle> { private double width; private double height; public RectComp(Comparator comp) { this.width = width; this.height = height; } public double getArea() { return width * height; } public int compare(Rectangle r1, Rectangle r2) { double diff = r1.getArea() - r2.getArea(); if (diff < 0) return -1; else if (diff == 0) return 0; else return 1; } public static void main(String[] args) { TreeSet<Rectangle> set = new TreeSet<>(new RectComp()); set.add(new Rectangle(1, 10)); set.add(new Rectangle(10, 1)); set.add(new Rectangle(5, 3)); set.add(new Rectangle(5, 2)); set.add(new Rectangle(5, 1)); for (Rectangle r : set) { System.out.println(r); } } }
原文:https://stackoverflow.com/questions/40286793
更新时间:2023-03-27 12:03
最满意答案
尝试使用dense_rank来查找下一个事件。
SELECT t_a.*, EXTRACT(EPOCH FROM (t_a.event_time - t_b.event_time)) FROM (SELECT nodeid, code, event_time, dense_rank() over (partition by node_id order by event_time) as rnk FROM table) t_a JOIN (SELECT nodeid, code, event_time, dense_rank() over (partition by node_id order by event_time) as rnk FROM table) t_b ON (t_a.nodeid=t_b.nodeid and t_a.rnk + 1 = t_b.rnk)
try to use dense_rank to find the next event.
SELECT t_a.*, EXTRACT(EPOCH FROM (t_a.event_time - t_b.event_time)) FROM (SELECT nodeid, code, event_time, dense_rank() over (partition by node_id order by event_time) as rnk FROM table) t_a JOIN (SELECT nodeid, code, event_time, dense_rank() over (partition by node_id order by event_time) as rnk FROM table) t_b ON (t_a.nodeid=t_b.nodeid and t_a.rnk + 1 = t_b.rnk)
相关问答
更多-
Intersystems Cache中两个日期时间戳之间的差异(Difference between two date time stamp in Intersystems Cache)[2023-06-03]
另外一个选择: USER>set mm=$system.SQL.DATEDIFF("mi","2016-01-02 01:00","2016-01-03 02:30") USER>write "hours=", mm \ 60 hours=25 USER>write "minutes=", mm # 60 minutes=30 Another option: USER>set mm=$system.SQL.DATEDIFF("mi","2016-01-02 01:00","2016-01-03 02:3 ... -
计算两个时间戳对象之间的时差 - java(Calculating the time difference between two time stamp object - java)[2023-08-12]
手动时间计算: - 转换日期(以毫秒(ms)为单位)并计算两个日期之间的差异,并遵循以下规则: 1000毫秒= 1秒 60秒= 1分钟 60分钟= 1小时 24小时= 1天 示例示例: - package com.dps2.practice.dyuti; import java.text.SimpleDateFormat; import java.util.Date; public class DateDifferentExample { public static void main(Str ... -
如果你不想添加一个库,你可以很容易地做到这一点: If y ...
-
如何计算两个日期之间的差异,以分钟为单位(How can I calculate difference between two dates with time in minutes)[2022-07-10]
moment.js对于这类事情是一个很好的库: var firstTime = moment("2016-04-15 1230", "YYYY-MM-DD HHmm"); var secondTime = moment("2016-04-15 1210", "YYYY-MM-DD HHmm"); var difference = moment.duration(firstTime - secondTime); console.log(difference.asMinutes()); // 20 mom ... -
你的例子有一些问题(我看到你现在修复了它们): 第一行应该是: from datetime import datetime 看起来好像你错过了第4行的右括号: now_time = datetime.now(timezone('US/Pacific') 什么是timezone() ? 它来自哪里? 你真的不需要搞乱时区,我不认为 - 只需使用GMT(UTC)。 怎么样更像这样的东西: from datetime import datetime timestamp = 'Tue, 12 Feb 2013 ...
-
或者你可以做到这一点 public static boolean on_pause() { return System.currentTimeMillis() - saved_date > 3 * 60 * 1000; } public static void on_resume() { saved_date = System.currentTimeMillis(); } 这相差180秒。 如果你比较你可能在239秒后的分钟数,仍然不认为这超过3分钟,因为你忽略了秒数。 Or you c ...
-
您可以减去两个DateTime对象并获取TimeSpan对象。 比使用TimeSpan.TotalMinutes 。 DateTime dateTimeFromDatabase = DateTime.Parse("13:00"); double minutes = (DateTime.Now - dateTimeFromDatabase).TotalMinutes; You can substract two DateTime objects and get a TimeSpan object. The ...
-
获取BigQuery中连续行之间的时间戳差异?(Get difference in time stamp between consecutive rows in BigQuery?)[2023-06-10]
SELECT id, ts, ts - prev_ts AS diff FROM ( SELECT id, ts, LAG(ts) OVER(ORDER BY ts) AS prev_ts FROM (SELECT 601 AS id, 1459223437 AS ts), (SELECT 602 AS id, 1459224049 AS ts), (SELECT 603 AS id, 1459224139 AS ts), (SELECT 604 AS id, 1459 ... -
尝试使用dense_rank来查找下一个事件。 SELECT t_a.*, EXTRACT(EPOCH FROM (t_a.event_time - t_b.event_time)) FROM (SELECT nodeid, code, event_time, dense_rank() over (partition by node_id order by event_time) as rnk FROM table) t_a JOIN (SELECT nodeid, code, event_time, ...
-
比较时间戳(Comparing Time Stamp)[2022-02-22]
你可以使用你的get_the_time, $time2 = get_the_time( __( 'd-m-Y H:i:s', 'woocommerce' ), $order->ID ); 然后, $time = current_time( 'timestamp' ); $time1 = date('d-m-Y H:i:s', $time); $date1 = new DateTime($time1); $date2 = new DateTime($time2); $interval = $date1-> ...