Thursday, 5 June 2014

Avoid duplicate string values in the collection 'Set' or 'Map Keys' while adding strings with different cases

When, Set allows duplicates?
Examples:

Set<String> nameSet = new Set<String>;
nameSet.add(srinu);
nameSet.add(SRINU);
nameSet.add(Srinu);

Map<String,String> cityMap = new Map<String,String>;
cityMap .put('Newyork', USA);
cityMap .put('newyork',US);
cityMap .put('NEWYORK','America');

From the above examples, nameSet and cityMap allows all the three values because set and map key treat the values with different case as different values that means set string values are case-sensitive.

How to avoid?
Use the below method before adding string value to set or map key.

//Generic method to search a string from set of strings and which will ignore case-sensitive
public static Boolean isStrExists(Set<String> strSet,String searchStr) {
        Boolean isTrue = false;
        if(strSet != null && strSet.size() > 0) {
            for(String strVal : strSet) {
                if(strVal.equalsIgnoreCase(searchStr)) {
                    isTrue = true;
                    break;
                }
            }
        }
        return isTrue;
    }

How to use above method?
if(!isStrExists(nameSet,'SRINU'))
    nameSet.add('SRINU');

41 comments:

  1. Excellent post!!! The future of cloud computing is on positive side. With most of the companies integrate Salesforce CRM to power their business; there is massive demand for Salesforce developers and administrators across the world. Salesforce Training Institutes in Chennai

    ReplyDelete
  2. In coming years, cloud computing is going to rule the world. The cloud based CRM tool provider like Salesforce have massive demand in the market. Thus talking Cloud Computing Training in Chennai from reputed Cloud Computing Courses will ensure bright career prospects for aspiring professionals.

    ReplyDelete
  3. Very interesting and good article, it is very useful to me... keep rocks.. thanks for sharing your valuable time and views....

    Salesforce Training in chennai | Salesforce Training in velachery

    ReplyDelete
  4. Excellent blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog. MCSE Training Institute in Chennai | MCSA Training Institute in Chennai | Hardware & Networking Training in Chennai

    ReplyDelete
  5. The best thing is that your blog really informative thanks for your great information..Final Year Project Center in Chennai | Final Year Project Center in Velachery

    ReplyDelete
  6. Great tips and its so easy to understand. Kindly keep updating such a nice article..
    PCB Training Institute in Chennai | PCB Training Center in Chennai | PCB Training in Velachery

    ReplyDelete
  7. Awesome Blog, you have provided the right information that will be beneficial to us. Thanks for sharing your valuable Ideas to our vision. Linux Training in Chennai | Unix Training in Chennai | Python Training in Chennai

    ReplyDelete
  8. Thanks for this blog. provided great information. All the details are explained clearly.
    Robotics Training Center in Chennai | Robotics Project Center in Chennai | Robotics Training in Velachery

    ReplyDelete
  9. • Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog? My blog is in the same niche as yours, and my users would benefit from some of the information you provide here. Please let me know if this ok with you. Thank you."Devops Training in Chennai"

    ReplyDelete
  10. Thanks for posting this useful and informative article with impressive content..keep updating..Diploma Project Center in Chennai | Diploma Project Center in Velachery

    ReplyDelete
  11. I really understand the concepts clearly..Thanks for giving such a nice post..
    Best Real Time Project Center in Chennai | Real Time Project Center in Velachery

    ReplyDelete
  12. I really understand the concepts clearly..Thanks for giving such a nice post..Six Sigma Certification Training in Chennai | Six Sigma Exam Center in Velachery

    ReplyDelete
  13. Very interesting content which helps me to get the in depth knowledge about the technology. To know more details about the course visit this website.Final Year Project Center in Chennai | Best Project Center in Chennai | No.1 Final Year Project Center in Chennai | Best Project Center in Velachery

    ReplyDelete
  14. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation

    python training in marathahalli

    ReplyDelete
  15. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Robotics Training Center in Chennai | Robotics Project Center in Velachery

    ReplyDelete
  16. Good post and I like it very much. By the way, anybody try this increase app downloads? I do not how to use.

    ReplyDelete
  17. Hi, am a big follower of your blog. I am really happy to found such a helpful and fascinating post that is written in well manner.
    mobile website builder

    ReplyDelete
  18. You have done a great job, really the salesforce interview question was useful to us, Thank you for sharing such a useful information.. Keep updating such a nice blog..Automation Anywhere Training in Chennai | RPA Training Institute in Chennai | UI Path Training Institute in Chennai | Blue Prism Training Institute in Chennai

    ReplyDelete
  19. Very Useful information that i have found. don't stop and Please keep updating us..... ThanksJava Training in Chennai | Web Designing Training Institute in Chennai | DotNet Training Institute in Chennai

    ReplyDelete
  20. I have a nice posting helpful very awesome article great blog please
    information help you.
    Best Adobe Photoshop Training Institute in Chennai | No.1 CorelDraw Training Institute in Chennai

    ReplyDelete
  21. I am really happy to read your blog..Thanks for posting this informative post..
    Power System Project Center in Chennai | Power System Projects in Velachery

    ReplyDelete
  22. Nice post.. Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating..
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

    ReplyDelete
  23. Nice post.. Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating..
    Best VMware Training Institute in Chennai | Best VMware Training Institute in Velachery

    ReplyDelete
  24. I feel really happy to have seen your webpage and look forward to so
    many more entertaining times reading here. Thanks once more for all
    the details.

    white label website builder

    ReplyDelete
  25. I strongly believe that there will be good opportunities for those who looked at your site.
    Best Online Software Training Institute | Salesforce.com Integration Training

    ReplyDelete
  26. Congrats for your work. Thanks a lot for providing this with us. Keep on updating this with us regularly..
    ISTQB Certifications Exam institute in Chennai | QA Testing in Sholinganallur

    ReplyDelete
  27. Nice post.Thanks for sharing this post .I really appreciate the kind of topics you post here.
    Android Certifications Center in Chennai | No.1 Android Exam in Saidapet

    ReplyDelete
  28. Nice information about this valuable article,which helps to learn more....
    Real Time Project Center in Chennai | Real Time Project in Kanchipuram

    ReplyDelete