<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>-Flyぁ梦- &#187; 排序</title>
	<atom:link href="http://blog.11034.org/tag/%e6%8e%92%e5%ba%8f/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>警惕long值转化为int，比如在Comparator接口中</title>
		<link>http://blog.11034.org/2016-03/long2int.html</link>
		<comments>http://blog.11034.org/2016-03/long2int.html#comments</comments>
		<pubDate>Wed, 02 Mar 2016 08:39:52 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[排序]]></category>
		<category><![CDATA[时间戳]]></category>

		<guid isPermaLink="false">http://blog.11034.org/?p=2786</guid>
		<description><![CDATA[long值转为int会造成溢出大家都知道，很多时候却会忽视已发bug，造成明明的正值变为了负值（因为int最高 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>long值转为int会造成溢出大家都知道，很多时候却会忽视已发bug，造成明明的正值变为了负值（因为int最高位非0，long值大于int最大值<code class="markdown_inline_code">2^31-1=2147483647</code>）。</p>
<p>此次出错的地方在使用<code class="markdown_inline_code">java.util.Collections.sort</code>中使用<code class="markdown_inline_code">Comparator</code>接口中，因为compare方法通过返回int值来比较大小，而逻辑比较大小是通过比较时间戳long值按最近时间排序，一开始不注意就直接把时间戳long值差给直接转为int值返回了，造成了可能的排序错误（有趣的是只需要<code class="markdown_inline_code">24天的毫秒值就会超过int的最大值</code>）。</p>
<p><span id="more-2786"></span></p>
<p>错误代码如下：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Comparator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span>Message o1, Message o2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>o2.<span style="color: #006633;">timestamp</span> <span style="color: #339933;">-</span> o1.<span style="color: #006633;">timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// wrong</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>显然，我们只要简单的比较2个时间戳long值，返回1和-1即可。</p>
<p>修正代码如下：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Comparator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span>Message o1, Message o2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>o2.<span style="color: #006633;">timestamp</span> <span style="color: #339933;">&gt;</span> o1.<span style="color: #006633;">timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2013-04-19 -- <a target="_blank" href="http://blog.11034.org/2013-04/system_reinstall.html" title="再也不说会重装系统了>_<">再也不说会重装系统了>_<</a></li><li>2015-04-07 -- <a target="_blank" href="http://blog.11034.org/2015-04/wuhan.html" title="美丽樱花，“文明”武汉">美丽樱花，“文明”武汉</a></li><li>2024-06-25 -- <a target="_blank" href="http://blog.11034.org/2024-06/jeju_island.html" title="2024济州岛outing">2024济州岛outing</a></li><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/jabber_and_xmpp.html" title="jabber和XMPP简述原理">jabber和XMPP简述原理</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2016-03/long2int.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
