日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString

發(fā)布時(shí)間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題

在Java領(lǐng)域,我們經(jīng)常需要編寫(xiě)equalshashCodetoString方法。 老實(shí)說(shuō),這通常只是一個(gè)樣板義務(wù)。

得益于智能IDE,我們通常不再自己這樣做。 我們只是讓和IDE一起努力。 不過(guò)有一個(gè)問(wèn)題。 生成的代碼通常非常丑陋。 讓我們考慮以下POJO:

public class Beer {private String brand;private String type;private int degrees;private double alcoholPercentage;private List<String> ingredients;// constructor// getters, setters if needed}

通常的解決方案

所有主要的IDE都具有生成我提到的方法的能力,但這就是hashCode,equals和toString的樣子:

1.等于– IF語(yǔ)句的長(zhǎng)列表...

@Override public boolean equals(final Object o) {if (this == o) {return true;}if (!(o instanceof Beer)) {return false;}final Beer beer = (Beer) o;if (Double.compare(beer.alcoholPercentage, alcoholPercentage) != 0) {return false;}if (degrees != beer.degrees) {return false;}if (!brand.equals(beer.brand)) {return false;}if (!ingredients.equals(beer.ingredients)) {return false;}if (!type.equals(beer.type)) {return false;}return true; }

2. hashCode –混淆魔術(shù)數(shù)字,異或運(yùn)算

@Override public int hashCode() {int result;long temp;result = brand.hashCode();result = 31 * result + type.hashCode();result = 31 * result + degrees;temp = alcoholPercentage != +0.0d ? Double.doubleToLongBits(alcoholPercentage) : 0L;result = 31 * result + (int) (temp ^ (temp >>> 32));result = 31 * result + ingredients.hashCode();return result; }

3. toString –討厭的字符串連接

@Override public String toString() {return 'Beer{' +'brand='' + brand + '\'' +', type='' + type + '\'' +', degrees=' + degrees +', alcoholPercentage=' + alcoholPercentage +', ingredients=' + ingredients +'}'; }

GOOGLE GUAVA解決方案

也許您聽(tīng)說(shuō)過(guò)Google Guava 。 也許您已經(jīng)在使用它。 無(wú)論如何,Google Guava是一個(gè)不錯(cuò)的小圖書(shū)館,為Java提供了很多便利。 使用番石榴,我們可以重寫(xiě)上面的三種方法來(lái)更好地尋找替代方法:

1.等于–將IF語(yǔ)句大軍轉(zhuǎn)變?yōu)殒準(zhǔn)节H回權(quán)

@Override public boolean equals(final Object obj) {if (this == obj) {return true;}if (obj == null || getClass() != obj.getClass()) {return false;}final Beer other = (Beer) obj;return Objects.equal(this.brand, other.brand) && Objects.equal(this.type, other.type) && Objects.equal(this.degrees, other.degrees) && Objects.equal(this.alcoholPercentage, other.alcoholPercentage) && Objects.equal(this.ingredients, other.ingredients); }

2. hashCode –單行

@Override public int hashCode() {return Objects.hashCode(brand, type, degrees, alcoholPercentage, ingredients); }

3. toString –一致的鏈?zhǔn)秸{(diào)用

@Override public String toString() {return Objects.toStringHelper(this).add('brand', brand).add('type', type).add('degrees', degrees).add('alcoholPercentage', alcoholPercentage).add('ingredients', ingredients).toString(); }

設(shè)置您的智能想法

對(duì)于equals和hashCode,有一個(gè)來(lái)自Michal Jedynak的名為Equals和HashCode Deluxe Generator的插件。 您可以直接在IntelliJ中安裝它,只需鍵入CTRL + SHIFT + A (在Mac上是CMD + SHIFT + A),然后鍵入Browser倉(cāng)庫(kù) 。 那應(yīng)該帶您到以下對(duì)話框,您可以在其中搜索插件:

使用新的equals和hashCode插件很簡(jiǎn)單,您將在舊版本旁邊緊挨著有一個(gè)新的上下文菜單選項(xiàng)equals()和hashCode()豪華版 。 只需按ALT + INS (在Mac上為CTRL + N),您將看到熟悉的生成菜單:

toString而言,我們只需要在IntelliJ中創(chuàng)建一個(gè)新模板。 按ALT + INS并轉(zhuǎn)到toString()菜單選項(xiàng)。 單擊設(shè)置按鈕 ,然后導(dǎo)航到模板選項(xiàng)卡 。 在模板標(biāo)簽中,點(diǎn)擊+按鈕

為新模板命名(例如Guava toString左右),并將以下代碼粘貼到編輯器中:

public String toString() {#set ($autoImportPackages = 'com.google.common.base.Objects')return Objects.toStringHelper(this)#foreach ($member in $members).add('$member.name', $member.accessor)#end.toString(); }

使用新模板很容易,只需進(jìn)入生成菜單( ALT + INS ),選擇toString()并確保選擇正確的模板:


參考: IntelliJ IDEA:通過(guò)vrtoonjava博客的JCG合作伙伴 Michal Vrtiak 使用Google Guava生成equals,hashCode和toString 。

翻譯自: https://www.javacodegeeks.com/2013/01/intellij-idea-generate-equals-hashcode-and-tostring-with-google-guava.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。