TreeSet和compareTo()方法。(TreeSet and compareTo() method. Single sort or multiple sorts)
我目前正在开发一个程序,我使用
TreeSet
存储唯一键。 我正在使用TreeSet,因为我希望它们排序。截至目前,我一直在制作一个TreeSet对象,并在需要时一次一个地添加字符串。
TreeSet set = new TreeSet(); set.add("How"); set.add("Are"); set.add("You");
据推测,TreeSet使用
Compareable interface
的compareTo
方法对字符串进行排序。 这意味着每次向其添加String时,TreeSet都必须进行排序。现在我的问题是:在将
all strings
添加到HashSet之后创建HashSet
然后创建TreeSet
会更有效吗?TreeSet<String> treeSet = new TreeSet<String>(set);
我的想法是关于TreeSet是否只需要以这种方式进行单一排序。
提前致谢
Im currently working on a program, where im using a
TreeSet
to store unique keys. I'm using a TreeSet, because I want them sorted.As of now, I have been making a TreeSet object, and added the strings one-at-a-time, when needed.
TreeSet set = new TreeSet(); set.add("How"); set.add("Are"); set.add("You");
Supposedly, the TreeSet uses the
compareTo
method in theCompareable interface
, to sort the strings. That would mean that the TreeSet would have to do a sort each time I add a String to it.Now my question is this: Would it be more efficient to create a
HashSet
and then create theTreeSet
afterall strings
are added to the HashSet?TreeSet<String> treeSet = new TreeSet<String>(set);
My thoughts are going about whether the TreeSet would only need to do a single sort that way.
Thanks in advance
原文:https://stackoverflow.com/questions/18452728
最满意答案
那么,
UITableView
是一个单列表。 所以,你通常可以不改变排序。 我建议阅读Table View Programming Guide 。 它在开头说得对:表格视图只有一列,只允许垂直滚动。 它由多个部分组成。 [...]
Well, the
UITableView
is a one-column table. So, you can usually not change the sorting. I'd recommend reading the Table View Programming Guide. It says right at the beginning:A table view has only one column and allows vertical scrolling only. It consists of rows in sections. [...]
相关问答
更多-
当您使用WH_MOUSE_LL注册钩子时, wparam的可能值为: WM_LBUTTONDOWN,WM_LBUTTONUP ,WM_MOUSEMOVE,WM_MOUSEWHEEL,WM_MOUSEHWHEEL,WM_RBUTTONDOWN或WM_RBUTTONUP。 我期待一旦发出WM_LBUTTONDOWN ,就必须发出相应的WM_LBUTTONUP以防止光标进入拖动状态。 我没有设备来测试这个,但我会尝试下面的调用以防止进入拖动状态。 CallNextHookEx(hMouseHook, nCode, ...
-
那么, UITableView是一个单列表。 所以,你通常可以不改变排序。 我建议阅读Table View Programming Guide 。 它在开头说得对: 表格视图只有一列,只允许垂直滚动。 它由多个部分组成。 [...] Well, the UITableView is a one-column table. So, you can usually not change the sorting. I'd recommend reading the Table View Programming G ...
-
两者都是: 单击表格或网格以显示小灰框。 单击左上角的,然后选择属性。 选择排序。 点击“Fx”按钮。 把你的表达放在上面。 对于动态排序,请选择表格中的单元格(而不是标题),然后单击“文本框属性”。 选择“交互式排序”并选择“在此文本框中启用交互式排序”。 放入数据集列,或者像上面所做的那样使用自定义表达式。 Yes on both: Click on the table or grid to make little gray boxes appear. Click in the upper left c ...
-
您缺少defaultTheme.css文件(或任何主题文件) $(document).ready(function () { $("table").fixedHeaderTable({ footer: true, cloneHeadToFoot: true, fixedColumn: true, height: 300 }); }); * {font-family: Segoe UI;} body {font-size: 80%;} table {} th {text-align: left ...
-
Microsoft SQL 2005中的自然(人类字母数字)排序(Natural (human alpha-numeric) sort in Microsoft SQL 2005)[2023-07-10]
当数据变得足够复杂(例如,其中一个或多个数字)时,我看到的大多数基于SQL的解决方案都会中断。 最初,我尝试在T-SQL中实现一个符合我的要求(除其他外,处理字符串中任意数量的数字)的NaturalSort函数,但是性能太慢了。 最终,我在C#中编写了一个标量CLR函数,以便自然排序,即使使用未优化的代码,从SQL Server调用它的性能却十分快速。 它具有以下特点: 会对前1000个字符进行排序(容易在代码中修改或作为参数) 正确排序小数,所以123.333来到123.45之前 由于上述原因,很可能不会 ... -
简短回答不,在这种情况下你不违反高。 你可以改变这种行为,你也可以使用苹果指南。 short answer no, you are not violating hig in this case. you can change that behavior and you should be fine with apple guidelines as well.
-
我可以告诉你问题是什么,虽然不是它的原因。 您要么打印出不同的对象,要么对象的值在一个打印和另一个打印之间变化。 您可以通过在对象上打印其哈希码或toString()调用来识别对象,从而在不同情况下判断它是否是同一个对象。 如果它们不同,我会检查你是不是在一个案例中加载一个具有临时范围的对象,并在下一个案例中打印出一个具有更大范围的对象。 但是你也可能只是提到一个不同的对象; 我不知道你发布的代码发生了什么,有可能你自己感到困惑。 你的帖子的标题是我这样说的原因。 “[hash]中的值不正确”是不可行的。 ...
-
Cassandra数据模型指导(Cassandra data model guidance)[2021-09-28]
标志status is_connected , is_up都是低基数分区。 假设所有机器都已启动,因此您的所有数据都将位于单个分区(在同一节点上),将创建热点,将无法扩展等。 因此,不要将低基数列( status , is_connected , is_up )作为分区键。 为每个标志值创建单独的表。 示例is_up : CREATE TABLE up_machines ( machine_id text PRIMARY KEY, machine_name text, ); CRE ... -
根据iOS人机界面指南启动横向应用程序(Launching landscape app according to iOS human interface guidelines)[2023-11-10]
我刚刚发现了解决方案:当以纵向模式启动仅横向应用程序时,将根据应用程序plist文件中UISupportedInterfaceOrientations节点下的条目顺序选择方向(即它是左还是右)。 所以解决方案是从xcode生成的更改项目顺序UISupportedInterfaceOrientations~ipad UIInterfaceOrientationLandscapeLeft UIInterfa ... -
人机界面指南只是指导原则,它们不是准确的规则。 没有必须列表,但有许多建议和积累的最佳实践。 The Human Interface Guidelines are just guidelines, they are not exact rules. There is not a list of must-dos but there are many recommendations, and best practices accumulated.