首页 \ 问答 \ TreeSet和compareTo()方法。(TreeSet and compareTo() method. Single sort or multiple sorts)

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 interfacecompareTo方法对字符串进行排序。 这意味着每次向其添加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 the Compareable 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 the TreeSet after all 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
更新时间:2023-07-30 06:07

最满意答案

那么, 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 ...
  • 当数据变得足够复杂(例如,其中一个或多个数字)时,我看到的大多数基于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]中的值不正确”是不可行的。 ...
  • 标志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 ...
  • 我刚刚发现了解决方案:当以纵向模式启动仅横向应用程序时,将根据应用程序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.

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)