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');

24 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