equals

Scala学习之字符串篇(一):字符串的比较

对着背影说爱祢 提交于 2019-12-03 08:34:31
在Scala中你只需要使用==就可以判断字符串相等,而不需要像Java一样需要使用的equals方法来判断。 scala> val s1 = "hello" s1: String = hello scala> val s2 = "hello" s2: String = hello scala> val s3 = "h" + "ello" s3: String = hello scala> s1 == s2 res4: Boolean = true scala> s1 == s3 res5: Boolean = true 使用==判断字符串相等的好处是,可以避免空指针异常。即==左边的字符串是空,也可以正常判断。 scala> val s4 = null s4: Null = null scala> s4 == s3 res6: Boolean = false scala> s3 == s4 res7: Boolean = false 忽略大小写来比较两个字符串是否一致,有两个方法:一个是把两个字符串都转化为大写或者小写再比较,另一个是直接使用 equalsIgnoreCase 方法。 scala> val s1 = "hello" s1: String = hello scala> val s2 = "Hello" s2: String = Hello scala> s1

How to compare the length of a list in html/template in golang?

血红的双手。 提交于 2019-12-03 08:32:02
问题 I am trying to compare the length of a list in golang html/template. But it is loading forever in html. {{ $length := len .SearchData }} {{ if eq $length "0" }} Sorry. No matching results found {{ end }} Could anyone help me with this? 回答1: From documentation, {{if pipeline}} T1 {{end}}: If the value of the pipeline is empty, no output is generated; otherwise, T1 is executed. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero

Using the equals() method with String and Object in Java

戏子无情 提交于 2019-12-03 07:50:12
问题 Object o1 = new Object(); Object o2 = new Object(); //o1=o2; System.out.println(o1.equals(o2)); It returns false . It can return true , if the comment is removed. Why isn't the same thing applicable to the String class? String s1=new String(); String s2=new String(); System.out.println(s1.equals(s2)); It returns true . Why? (because String uses interns or something else involved?) 回答1: Because equals() for String compares the content, not the object itself. public boolean equals(Object

Java知识点系列:java.lang.Object

空扰寡人 提交于 2019-12-03 07:44:44
Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入。Object类没有定义属性,一共有13个方法,具体的类定义结构如下图: 1.类构造器public Object(); 大部分情况下 ,Java中通过形如 new A(args..)形式创建一个属于该类型的对象。其中A即是类名,A(args..)即此类定义中相对应的构造函数。通过此种形式创建的对象都是通过类中的构造函数完成。为体现此特性,Java中规定:在类定义过程中,对于未定义构造函数的类,默认会有一个无参数的构造函数,作为所有类的基类,Object类自然要反映出此特性,在源码中,未给出Object类构造函数定义,但实际上,此构造函数是存在的。 当然,并不是所有的类都是通过此种方式去构建,也自然的, 并不是所有的类构造函数都是public。 2.private static native void registerNatives(); registerNatives函数前面有native关键字修饰,Java中,用native关键字修饰的函数表明该方法的实现并不是在Java中去完成,而是由C/C++去完成,并被编译成了.dll,由Java去调用。方法的具体实现体在dll文件中,对于不同平台,其具体实现应该有所不同。用native修饰,即表示操作系统,需要提供此方法

hashCode() method when equals() is based on multiple independent fields

社会主义新天地 提交于 2019-12-03 06:15:11
i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for such an equals() so that the general contract of hashCode being equal when equals returns true is preserved? public class MyClass { int id; String name; public boolean equals(Object o) { if (!(o instanceof MyClass)) return false; MyClass other = (MyClass) o; if (other.id == this.id || other.name == this.name) return true; return false; } } how do i write a hashCode() function for this class? and i want to avoid the

ArrayList not using the overridden equals

隐身守侯 提交于 2019-12-03 06:13:21
I'm having a problem with getting an ArrayList to correctly use an overriden equals. the problem is that I'm trying to use the equals to only test for a single key field, and using ArrayList.contains() to test for the existence of an object with the correct field. Here is an example public class TestClass { private static class InnerClass{ private final String testKey; //data and such InnerClass(String testKey, int dataStuff) { this.testKey =testKey; //etc } @Override public boolean equals (Object in) { System.out.println("reached here"); if(in == null) { return false; }else if( in instanceof

Why does Java's Area#equals method not override Object#equals?

做~自己de王妃 提交于 2019-12-03 05:39:26
I just ran into a problem caused by Java's java.awt.geom.Area#equals(Area) method. The problem can be simplified to the following unit test: @org.junit.Test public void testEquals() { java.awt.geom.Area a = new java.awt.geom.Area(); java.awt.geom.Area b = new java.awt.geom.Area(); assertTrue(a.equals(b)); // -> true java.lang.Object o = b; assertTrue(a.equals(o)); // -> false } After some head scratching and debugging, I finally saw in the JDK source, that the signature of the equals method in Area looks like this: public boolean equals(Area other) Note that it does not @Override the normal

Is it possible for 'this' keyword to equal null?

限于喜欢 提交于 2019-12-03 05:34:22
问题 In an example, my professor has implemented Equals as follows: public class Person { private string dni; // ... public override bool Equals(object o) { if (o == null) return (this == null); else { return ((o is Person) && (this.dni == (o as Person).dni)); } } } I have no experience with C#, but as far as I know this cannot be null inside a member function (at least this is true in C++ and Java, the languages I know) so the if seems weird to me. Am I right or is there any component in c# I

C# - compare two SecureStrings for equality

房东的猫 提交于 2019-12-03 05:34:03
问题 I have a WPF application with two PasswordBoxes, one for the password and another for the password to be entered a second time for confirmation purposes. I was wanting to use PasswordBox.SecurePassword to get the SecureString of the password, but I need to be able to compare the contents of the two PasswordBoxes to ensure equality before I accept the password. However, two identical SecureStrings are not considered equal: var secString1 = new SecureString(); var secString2 = new SecureString(

In javascript, what do multiple equal signs mean? [duplicate]

故事扮演 提交于 2019-12-03 04:57:31
This question already has answers here : Javascript a=b=c statements (6 answers) I saw this code somewhere, but what does it mean? (all a, b, c are defined previously) var a = b = c; It quickly assigns multiple variables to a single value. In your example, a and b are now equal set to the value of c . It's also often used for a mass assign of null to clean up. a = b = c = d = null; Assign c to b. Assign b to a. So if I say var a = b = 1; >>> var a = b = 1; undefined >>> a 1 >>> b 1 This means a , b and c are the same reference. For example: var c = {hello: "world"}; var a = b = c; // now all