Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 310835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:52:11+00:00 2026-05-12T07:52:11+00:00

Ok so here is my issue. I have to HashSet ‘s, I use the

  • 0

Ok so here is my issue. I have to HashSet‘s, I use the removeAll method to delete values that exist in one set from the other.

Prior to calling the method, I obviously add the values to the Sets. I call .toUpperCase() on each String before adding because the values are of different cases in both lists. There is no rhyme or reason to the case.

Once I call removeAll, I need to have the original cases back for the values that are left in the Set. Is there an efficient way of doing this without running through the original list and using CompareToIgnoreCase?

Example:

List1:

"BOB"
"Joe"
"john"
"MARK"
"dave"
"Bill"

List2:

"JOE"
"MARK"
"DAVE"

After this, create a separate HashSet for each List using toUpperCase() on Strings. Then call removeAll.

Set1.removeAll(set2);

Set1:
    "BOB"
    "JOHN"
    "BILL"

I need to get the list to look like this again:

"BOB"
"john"
"Bill"

Any ideas would be much appreciated. I know it is poor, there should be a standard for the original list but that is not for me to decide.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:52:11+00:00Added an answer on May 12, 2026 at 7:52 am

    In my original answer, I unthinkingly suggested using a Comparator, but this causes the TreeSet to violate the equals contract and is a bug waiting to happen:

    // Don't do this:
    Set<String> setA = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    setA.add("hello");
    setA.add("Hello");
    System.out.println(setA);
    
    Set<String> setB = new HashSet<String>();
    setB.add("HELLO");
    // Bad code; violates symmetry requirement
    System.out.println(setB.equals(setA) == setA.equals(setB));
    

    It is better to use a dedicated type:

    public final class CaselessString {
      private final String string;
      private final String normalized;
    
      private CaselessString(String string, Locale locale) {
        this.string = string;
        normalized = string.toUpperCase(locale);
      }
    
      @Override public String toString() { return string; }
    
      @Override public int hashCode() { return normalized.hashCode(); }
    
      @Override public boolean equals(Object obj) {
        if (obj instanceof CaselessString) {
          return ((CaselessString) obj).normalized.equals(normalized);
        }
        return false;
      }
    
      public static CaselessString as(String s, Locale locale) {
        return new CaselessString(s, locale);
      }
    
      public static CaselessString as(String s) {
        return as(s, Locale.ENGLISH);
      }
    
      // TODO: probably best to implement CharSequence for convenience
    }
    

    This code is less likely to cause bugs:

    Set<CaselessString> set1 = new HashSet<CaselessString>();
    set1.add(CaselessString.as("Hello"));
    set1.add(CaselessString.as("HELLO"));
    
    Set<CaselessString> set2 = new HashSet<CaselessString>();
    set2.add(CaselessString.as("hello"));
    
    System.out.println("1: " + set1);
    System.out.println("2: " + set2);
    System.out.println("equals: " + set1.equals(set2));
    

    This is, unfortunately, more verbose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a curious issue here In my ejbCreate() method from where i insert
Here's the issue: I have 2 data contexts that I would like to do
Here's the issue: I have a list of App names that I want to
Having an issue here that I have tried everything I can think of but
Here's a brief rundown of my issue: I have a JavaScript function that gets
Here my issue, I have 2 Jframe, one for controlling and one for displaying
Here's the issue: I have a hook in IE that reacts on WebBrowser.OnNavigateComplete2 event
Here's the issue: I have a JS dropdown for country and state that runs
i have an issue here, my mails sent from my PHP script never gets
Greetings! I have an issue here that i can't find. I am getting a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.