equals

Is it ok to compare immutable objects in Java using == instead of equals

柔情痞子 提交于 2019-12-23 09:19:34
问题 Consider two references of type Integer that call the static factory method valueOf as shown below:- Integer a = Integer.valueOf("10"); Integer b = Integer.valueOf("10"); Considering that Integer is immutable, is it ok to compare a and b using == instead of using equals method. I am guessing that the valueOf method makes sure that only one instance of Integer with the value 10 is created and a reference to this instance is returned for every Integer created with a value 10. In general, is it

how to implement a hamcrest matcher

笑着哭i 提交于 2019-12-23 08:54:47
问题 I want to run this line of code: assertThat(contextPin.get(), equalTo(pinPage.getPinObjFromUi())); but I want to print to the log be informative meaning that I could know which fields were not equal. So I have thought to implement a matcher. I have googled it, but couldn't write it properly as my method couldn't get the actual and expected objects together. here is my code: how can I write it clean? public class PinMatcher extends TypeSafeMatcher<Pin> { private Pin actual; private Object item

Weird behavior of java.util.Set.contains(Object o)

北城余情 提交于 2019-12-23 08:38:32
问题 The doc about java.util.Set.contains(Object o) says: Returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)). That said, here is a POJO (as you can see, I overwrote its equals method): public class MonthAndDay { private int month; private int day; public MonthAndDay(int month, int day) { this.month = month; this.day = day; } @Override public boolean equals(Object obj) { MonthAndDay monthAndDay = (MonthAndDay) obj; return monthAndDay.month ==

Using the == symbol in golang and using a loop to compare if string a equals string b,which performance is better?

余生颓废 提交于 2019-12-23 06:29:07
问题 for i:=0;i<len(a);i++{ if a[i] != b[i]{ return false } } and just a == b I've found that the same string have different address a := "abc" b := "abc" println(&a) println(&b) answer is : 0xc420045f68 0xc420045f58 so == not using address to compare. In fact, I would like to know how == compares two strings. I am searching for a long time on net. But failed... 回答1: You should use the == operator to compare strings. It compares the content of the string values. What you print is the address of a

Equality and Case Classes

喜欢而已 提交于 2019-12-23 04:53:29
问题 I have: sealed trait BEValue case class BEByteString(val value: Array[Byte]) extends BEValue { def equals(that: BEByteString): Boolean = this.value.deep == that.value.deep def ==(that: BEByteString) = this equals that } case class BEList(val value: List[BEValue]) extends BEValue BEByteString("spam".getBytes) == BEByteString("spam".getBytes) //true val l1 = BEList(BEByteString("spam".getBytes):: Nil) val l2 = BEList(BEByteString("spam".getBytes):: Nil) l1 == l2 // false. Why ? 回答1: You should

maximal sequence of equal elements in an array

删除回忆录丶 提交于 2019-12-23 04:52:19
问题 I have for homework the exercise: Write a program that finds the maximal sequence of equal elements in an array. Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} = {2, 2, 2}. I came up with this: Console.WriteLine("Enter array lenght"); int arrLenght = int.Parse(Console.ReadLine()); int[] arr = new int[arrLenght]; Console.WriteLine("Enter array elements"); for (int i = 0; i < arr.Length; i++) { arr[i] = int.Parse(Console.ReadLine()); } for (int i = 0; i < arr.Length; i++) { if (arr[i] == arr[i + 1] &&

Using HashMap to count instances

為{幸葍}努か 提交于 2019-12-22 07:52:27
问题 I have the following code to count the instances of different strings in an array; String words[] = {"the","cat","in","the","hat"}; HashMap<String,Integer> wordCounts = new HashMap<String,Integer>(50,10); for(String w : words) { Integer i = wordCounts.get(w); if(i == null) wordCounts.put(w, 1); else wordCounts.put(w, i + 1); } Is this a correct way of doing it? It seems a bit long-winded for a simple task. The HashMap result is useful to me because I will be indexing it by the string. I am

Why are autoboxed Integers and .getClass() values ==-equal, not only .equals()-equal?

倖福魔咒の 提交于 2019-12-21 19:45:51
问题 Maybe I've been working too long on Java without really understanding some of its basics. I do understand that == is for object reference equality and .equals() is for object value equality. Comparing Integers : Integer x = 1, y = 1; System.out.println(x == y); // true Why? Since object reference equality is used, it should be false since they are both different objects. Comparing getClass() return values: String s1 = "a", s2 = "b"; System.out.println(s1.getClass() == s2.getClass()); // true

setting objects equal to eachother (java)

青春壹個敷衍的年華 提交于 2019-12-21 17:36:50
问题 So I have a class called Person which looks like this public class Person { private String personName; public String toString(){ return personName; } public Person(String personName){ this.personName = personName; } } and another class in which I am creating the object(s) person public class IdanJavaTask { public static void main(String[] args) { Person p1 = new Person("Bob"); System.out.println("p1 : " + p1); Person p2 = new Person("Joe"); System.out.println("p2 :" + p2); } } so far

Bit Array Equality

老子叫甜甜 提交于 2019-12-21 17:03:31
问题 I need something a little more than the System.Collections.BitArray class in my application. Specifically, I need the bit array: To be immutable To implement equality using value semantics I created my own struct , largely copying the internals of the BitArray implementation. (Thanks, .Net Reflector!) I don't deal everyday with bitwise operations, so I don't have the highest degree of confidence in my equality implementation. (It's passing the unit tests I am throwing at it, but I may be