public enum EnumWithIntValue {
ZERO(0),
ONE(1),
TWO(2),
THREE(3),
FOUR(4),
FIVE(5);
private final int intValue;
private EnumWithIntValue(final int intValue) {
this.intValue = intValue;
}
public final int getValue() {
return this.intValue;
}
}
I then wanted to do a reverse lookup where I pass in a primitive int value and return the corresponding enum value. This was readily accomplished by adding the following static method to EnumWithIntValue:
public static final EnumWithIntValue valueOf(final int intValueParam) {
for (EnumWithIntValue intValueEnum : values()) {
if (intValueEnum.intValue == intValueParam) {
return intValueEnum;
}
}
throw new IllegalArgumentException("Invalid int value parameter " + intValueParam);
}
Hold up. What on Earth does values() do? If you look at the java.lang.Enum code from the JDK it does not exist. But according to the Enum Types section of the Java Tutorials:
The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.
Ok all of that makes sense (sort of). However, what does values() do every time it is called? Or to put it another way, is values() giving me a reference to the same array each time it is called or is it cloning the encapsulated array and giving me a copy each time? Let's find out:
public static void main(final String[] args) {
EnumWithIntValue[] array = values();
System.out.println(array);
array = values();
System.out.println(array);
}
Well lookie here:
[Labc.test.EnumWithIntValue;@322ba3e4
[Labc.test.EnumWithIntValue;@4f14e777
[Labc.test.EnumWithIntValue;@4f14e777
Great, so each call to values() is obviously cloning the encapsulated array which is no good from a performance perspective. Of course the JDK is more worried about giving away the internal array reference which can lead to the possibility of someone altering said array which is also a big no-no. But for the purpose of my valueOf() method, that array is only used within my enum.
Therefore, let's cache the array inside the enum declaration and then update the valueOf() method to use the cached instance instead:
// Declare the enum cache as a private constant
private static final EnumWithIntValue[] enumValueArray = EnumWithIntValue.values();
// Then update the values() call inside valueOf() to reference the enum cache instead
for (EnumWithIntValue intValueEnum : enumValueArray) {
...
}
And voila! A reverse enum lookup that performs as well as it looks!
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai
IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. Final Year Project Domains for IT
DeleteJavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, project projects for cse. Angular Training
The blog gave me idea to reverse java enum values Thanks for sharing it
ReplyDeleteHadoop Training in Chennai
After reading this blog I am very strong in this topics and this blog is really helpful to all.. Explanation are very clear so it is easy to understand.. Thanks for sharing this blog…
ReplyDeleteBig Data Analytics Training in Chennai | Web Designing Training in Chennai
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.
ReplyDeleteI like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Java training in Indira nagar
Java training in Rajaji nagar
Java training in Marathahalli
Java training in Btm layout
Java training in Marathahalli
You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
ReplyDeleteJava Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore
Great Post!!! Thanks for the data update and waiting for your new updates.
ReplyDeleteAndroid Training in Chennai
Android Training Institute in Chennai
android training center in chennai
android development course in chennai
Android training in porur
Android training in OMR
Big data training in chennai
Android Training in Chennai
IOS Training in Chennai
Selenium Training in Chennai
Nice article, its very informative content..thanks for sharing...Waiting for the next update...
ReplyDeleteDrupal Training in Chennai
Drupal Course in Chennai
Drupal Training
Drupal Training in OMR
Drupal Training in Porur
Photoshop Classes in Chennai
clinical sas training in chennai
SAS Training in Chennai
javascript training in chennai
Hibernate Training in Chennai
I would like to thank the blog admin for sharing this useful information in my vision. I have been searching for this blog for a while.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar
Great blog, I was searching this for a while. Do post more like this.
ReplyDeleteAngularJS Training in Chennai
Angular 4 Training in Chennai
Angular 5 Training in Chennai
Angular Training in Chennai
ReactJS Training in Chennai
PHP course in Chennai
Web Designing Training in Chennai
AngularJS Training in Anna Nagar
AngularJS Training in Vadapalani
AngularJS Training in Thiruvanmiyur
Great blog!!! This information is very useful for all. Thanks for sharing with us...
ReplyDeleteAWS Training in Velachery
AWS Training in Anna Nagar
AWS Training in Tambaram
AWS Training in T Nagar
AWS Training in Vadapalani
AWS Training in Porur
AWS Training in Adyar
AWS Training in OMR
AWS Training in Thiruvanmiyur
I really appreciate your method to explaining, I hope to see more posts from your blog. thank you!
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Power BI Training in Chennai
Job Openings in Chennai
Pega Training in Chennai
Linux Training in Chennai
Placement Training in Chennai
Soft Skills Training in Chennai
Appium Training in Chennai
Tableau Training in Velachery
Tableau Training in OMR
ReplyDeleteThe blog you shared is very good. I expect more information from you like this blog. Thankyou.
Web Designing Course in chennai
Web Designing Course in bangalore
web designing course in coimbatore
web designing training in chennai
web designing course in madurai
Web Development courses in bangalore
web design training in coimbatore
php training in madurai
I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
ReplyDeletebest software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeleteWeb Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai
This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article.
ReplyDeleteLearn Best Digital Marketing Course in Chennai
Digital Marketing Course Training with Placement in Chennai
Learn Digital Marketing Course Training in Chennai
Digital Marketing Training with Placement Institute in Chennai
Excellent blog thanks for sharing Looking for the best creative agency to fuel new brand ideas? Adhuntt Media is not just a digital marketing company in chennai. We specialize in revamping your brand identity to drive in best traffic that converts. Buckle up for a ride that is going to be filled with SEO, Social media marketing, unique Graphic & Logo design and efficient ads strike the perfect chord! Join the Adhuntt Media adventure right at Adhuntt Media.
ReplyDeletedigital marketing company in chennai
Nice blog thanks for sharing Wondering where to buy healthy garden plants in Chennai for your home? Karuna Nursery Gardens offers you with tons of variety and organic choices suitable for our tropical climate. Make your garden fabulous with Karuna Nursery.
ReplyDeleteplant nursery in chennai
Excellent blog thanks for sharing Looking for the best place in Chennai to get your cosmetics at wholesale? The Pixies Beauty Shop is the premium wholesale cosmetics shop in Chennai that has all the international brands your salon deserves.
ReplyDeleteCosmetics Shop in Chennai
Your topic is very nice and helpful to us … Thank you for the information you wrote.
ReplyDeleteLearn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Bangalore Training Academy provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
Big Data Analytics Training in Bangalore
Tableau Training in Bangalore
Data Science Training in Bangalore
Workday Training in Bangalore
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
ReplyDeletesap abap training in bangalore
sap abap courses in bangalore
sap abap classes in bangalore
sap abap course syllabus
best sap abap training
sap abap training center
sap abap training institute in bangalore
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeletesap hana courses in bangalore
sap hana classes in bangalore
sap hana training institute in bangalore
sap hana course syllabus
best sap hana training
sap hana training centers
best sap hana training
Awesome,Thank you so much for sharing such an awesome blog.
ReplyDeletesap hr courses in bangalore
sap hr classes in bangalore
sap hr training institute in bangalore
sap hr course syllabus
best sap hr training
sap hr training centers
sap hr training in bangalore
Thanks for sharing this blog. This very important and informative blog
ReplyDeletesap mm training in bangalore
sap mm courses in bangalore
sap mm classes in bangalore
sap mm training institute in bangalore
sap mm course syllabus
best sap mm training
sap mm training centers
Learned a lot of new things from your post! Good creation and HATS OFF to the creativity of your mind.
ReplyDeletesap fico training in bangalore
sap fico courses in bangalore
sap fico classes in bangalore
sap fico training institute in bangalore
sap fico course syllabus
best sap fico training
sap fico training centers
I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
ReplyDeletephp online training in chennai
php programming center in chennai
php class in chennnai
php certification course
php developer training institution chennai
php training in chennnai
php mysql course in chennai
php institute in chennnai
php course in chennnai
php training with placement in chennnai
php developer course
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog.
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Bangalore
Software Testing Course in Coimbatore
Software Testing Training in Madurai
Software Testing Training Institute in Bangalore
Software Testing Course in Bangalore
Testing Course in Bangalore
Ethical hacking course in bangalore
wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up. leovely page
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
instagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
takipçi satın al
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
aşk kitapları
tiktok takipçi satın al
instagram beğeni satın al
youtube abone satın al
twitter takipçi satın al
tiktok beğeni satın al
tiktok izlenme satın al
twitter takipçi satın al
tiktok takipçi satın al
youtube abone satın al
tiktok beğeni satın al
instagram beğeni satın al
trend topic satın al
trend topic satın al
youtube abone satın al
beğeni satın al
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok beğeni satın al
sms onay
sms onay
perde modelleri
instagram takipçi satın al
takipçi satın al
tiktok jeton hilesi
pubg uc satın al
sultanbet
marsbahis
betboo
betboo
betboo
instagram takipçi satın al
ReplyDeleteucuz takipçi
takipçi satın al
https://takipcikenti.com
https://ucsatinal.org
instagram takipçi satın al
https://perdemodelleri.org
https://yazanadam.com
instagram takipçi satın al
balon perdeler
petek üstü perde
mutfak tül modelleri
kısa perde modelleri
fon perde modelleri
tül perde modelleri
https://atakanmedya.com
https://fatihmedya.com
https://smmpaketleri.com
https://takipcialdim.com
https://yazanadam.com
yasaklı sitelere giriş
aşk kitapları
yabancı şarkılar
sigorta sorgula
https://cozumlec.com
word indir ücretsiz
tiktok jeton hilesi
rastgele görüntülü sohbet
erkek spor ayakkabı
fitness moves
gym workouts
https://marsbahiscasino.org
http://4mcafee.com
http://paydayloansonlineare.com
takipçi satın al
ReplyDeletetakipçi satın al
takipçi satın al
marsbahis
ReplyDeletebetboo
sultanbet
marsbahis
betboo
sultanbet
This article provided me with a wealth of information. The article is incredibly helpful and offers some of the most useful information. Thank you for sharing it with us. web development company in chennai
ReplyDeleteReally, this is very important information which is shared by you. This info is meaningful and important for everyone to increase our knowledge about it. Always keep sharing this kind of info. Thank you. Read more info about gif construction
ReplyDeleteA very delightful article that you have shared here. Your blog is a valuable and engaging article for us, and also I will share it with my companions who need this info. Thankful to you for sharing an article like this. Website Logo Maker Services Usa
ReplyDeleteYou wrote this post very carefully.Web Design in Tampa The amount of information is stunning and also a gainful article for us. Keep sharing this kind of articles, Thank you.
ReplyDeleteseo fiyatları
ReplyDeletesaç ekimi
dedektör
instagram takipçi satın al
ankara evden eve nakliyat
fantezi iç giyim
sosyal medya yönetimi
mobil ödeme bozdurma
kripto para nasıl alınır
instagram beğeni satın al
ReplyDeleteyurtdışı kargo
seo fiyatları
saç ekimi
dedektör
fantazi iç giyim
sosyal medya yönetimi
farmasi üyelik
mobil ödeme bozdurma
perde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
TRAFİK SİGORTASI
DEDEKTOR
website kurma
aşk kitapları
smm panel
ReplyDeletesmm panel
Https://isilanlariblog.com
instagram takipçi satın al
hırdavatçı burada
beyazesyateknikservisi.com.tr
SERVİS
Tiktok jeton hilesi indir