使用hashcode()和length()进行字符串比较(String comparison using hashcode() and length())
在Java(或任何其他PL使用相同的哈希码功能)是否可以安全地说:
两个相等长度的字符串具有相同的散列码当且仅当它们相等时
让我们假设hashcode函数将会是
s [0] * 31 ^(n-1)+ s [1] * 31 ^(n-2)+ ... + s [n-1]
用例:
为了比较两个大规模的字符串集合,使用
option 1
而不是option 2
在理论上会更快,因为哈希码计算将会执行一次,因为字符串将缓存它的值:选项1:
for(String s1 : collection1){ for(String s2 : collection2){ if((s1.hashCode() == s2.hashCode()) && (s1.length()==s2.length()){ System.out.println("matched"); } } }
方案2
for(String s1 : collection1){ for(String s2 : collection2){ if(s1.equals(s2)){ System.out.println("matched"); } } }
更新:
在@tobias_k发表评论后,我意识到这个说法错了,所以我改变了这个问题。
是否有一个字符串的最大长度为M,对于任何两个长度相等的字符串,它们的哈希码将相同,当且仅当它们相等时
In java (or any other PL that uses same hashcode function) Is it safe to say that:
Two strings of equal length have same hashcode if and only if they are equal
let's just assume the hashcode function will be
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
use case:
for comparing two massive collections of strings, it'll be theoretically faster to use
option 1
rather thanoption 2
because hashcode computation will be done once as String will cache the its value:OPTION 1:
for(String s1 : collection1){ for(String s2 : collection2){ if((s1.hashCode() == s2.hashCode()) && (s1.length()==s2.length()){ System.out.println("matched"); } } }
OPTION 2
for(String s1 : collection1){ for(String s2 : collection2){ if(s1.equals(s2)){ System.out.println("matched"); } } }
UPDATE:
after @tobias_k comment I realized that statment wrong, So I change the question.
is there a max length M for string that for any two strings of equal length their hashcode will be same if and only if they are equal
原文:https://stackoverflow.com/questions/31027909
最满意答案
表单的内容窗格正在执行滚动,您需要底部容器来处理滚动。 尝试这个:
Form myForm = new Form(); myForm.setLayout(new LayeredLayout()); myForm.setTitle("Floating Action Button"); SpanLabel lbl = new SpanLabel("some long text "); Container conBottom = new Container(); conBottom.setScrollableY(true); conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); conBottom.addComponent(lbl); FlowLayout flow = new FlowLayout(Component.RIGHT); flow.setValign(Component.BOTTOM); Container conUpper = new Container(flow); conUpper.addComponent(new Button("+")); conUpper.setScrollable(false); myForm.addComponent(conBottom); myForm.addComponent(conUpper); myForm.setScrollable(false); myForm.show();
The Form's content pane is performing the scrolling, you need your bottom container to handle the scrolling instead. Try this:
Form myForm = new Form(); myForm.setLayout(new LayeredLayout()); myForm.setTitle("Floating Action Button"); SpanLabel lbl = new SpanLabel("some long text "); Container conBottom = new Container(); conBottom.setScrollableY(true); conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); conBottom.addComponent(lbl); FlowLayout flow = new FlowLayout(Component.RIGHT); flow.setValign(Component.BOTTOM); Container conUpper = new Container(flow); conUpper.addComponent(new Button("+")); conUpper.setScrollable(false); myForm.addComponent(conBottom); myForm.addComponent(conUpper); myForm.setScrollable(false); myForm.show();
相关问答
更多-
FloatingActionButton类主要扩展了VisibilityAwareImageButton ,它扩展了imagebutton 。 所以,从头开始,你应该从扩展图像按钮开始。 然后,当您获得VisibilityAwareImageButton您可以创建您的FloatingButton类。 VisibilityAwareImageButton代码如下 package android.support.design.widget; import android.content.Context; im ...
-
您可以将此库用于浮动菜单按钮 - https://android-arsenal.com/details/1/965 Hope this will help to other as well. I don't want to depend on others library. That's why I am looking for someone who have make the same with the animation and effect, not with the library. I hav ...
-
使用: showForm("MyFormName"); 这可能很有用: http://www.codenameone.com/how-do-i---handle-eventsnavigation-in-the-gui-builder--populate-the-form-from-code.html Use: showForm("MyFormName"); This can be useful: http://www.codenameone.com/how-do-i---handle-eventsnav ...
-
我强烈建议你避免使用渐变填充。 它们非常慢并且占用了大量内存! 使用图像背景或9补丁图像边框,这些边框更快,内存效率更高。 性能如下所述: http : //www.codenameone.com/how-do-i---improve-application-performance-or-track-down-performance-issues.html I strongly suggest you avoid gradient fills. They are VERY slow and take up ...
-
您需要修改列表的模型,覆盖设置模型的initList回调,并定义您自己的列表模型,该模型以您希望数据的方式存储数据。 然后添加操作可以只修改该数据结构。 You need to modify the model of the list, override the initList callback where the model is set and define your own list model which stores the data in the way you want the data t ...
-
使用一些滚轮菜单库并通过计算左,右,上和下边距来放置所需位置。 请参阅 - https://github.com/szugyi/Android-CircleMenu 快乐编码...... Use some wheel menu libraries and place where you need by calculating margin left,right,top and bottom. Refer - https://github.com/szugyi/Android-CircleMenu Happy ...
-
你应该使用官方的Font Awesome Library, https://github.com/bperin/FontAwesomeAndroid FontAwesome Library中有DrawableAwesome,您需要使用它来构造它 DrawableAwesome drable = new DrawableAwesome.DrawableAwesomeBuilder(Context context, int icon).build(); 并设置在您的FAB中。 int icon是字符串资源的i ...
-
尝试使用RelativeLayout而不是LinearLayout。 并且为此FloatingActionButton添加属性, android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="end|bottom" Try using RelativeLayout instead of Li ...
-
表单的内容窗格正在执行滚动,您需要底部容器来处理滚动。 尝试这个: Form myForm = new Form(); myForm.setLayout(new LayeredLayout()); myForm.setTitle("Floating Action Button"); SpanLabel lbl = new SpanLabel("some long text "); Container conBottom = new Container(); ...
-
JSON教程写入了Twitter 1.0 API,几个月前已被弃用并完全删除。 不幸的是,替代方案是使用的麻烦和痛苦,我们需要发布一个新的教程与新的Web服务,但教程的要点仍然存在,尽管它不再有效。 The JSON tutorial was written to the Twitter 1.0 API which was deprecated and completely removed a few months ago. Unfortunately the alternative is obtuse a ...