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

  • SEARCH
  • Home
  • 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 8898393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:36:18+00:00 2026-06-15T00:36:18+00:00

I want to copy my elements from a List<Set<String>> into a SortedMap<Set<String>,Integer> , but

  • 0

I want to copy my elements from a List<Set<String>> into a SortedMap<Set<String>,Integer>,
but I always get:

java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.Comparable. (or HashMap could be a TreeSet too, vice versa)

Some places I’ve been reading say it isn’t possible, but is this correct?

I can’t believe I can’t copy a original List or Set into a Map.

This is what I have tried:

List<Set<String> > tempnewOut= new ArrayList<>(); 
SortedMap<Set<String>,Integer> freqSetsWithCount= new TreeMap<>(); 
for (Set<String> set : tempnewOut) 
{ 
    freqSetsWithCount.put(set, 0); 
}
  • 1 1 Answer
  • 0 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-06-15T00:36:19+00:00Added an answer on June 15, 2026 at 12:36 am

    The class that you use as a key in a TreeMap (one of the implementations of interface SortedMap) must either implement interface Comparable, or you must create the TreeMap by providing a Comparator to the constructor.

    You’re trying to use a HashSet<String> for the keys. HashSet doesn’t implement Comparable, and you’re not supplying a Comparator, so you get a ClassCastException.

    One solution is to create the TreeMap by passing it a Comparator to the constructor. You’ll have to implement the compare method of the Comparator to specify how the sets should be sorted in the map.

    List<Set<String>> list = new ArrayList<>();
    
    Comparator<Set<String>> comparator = new Comparator<Set<String>>() {
        @Override
        public int compare(Set<String> o1, Set<String> o2) {
            // TODO: Implement your logic to compare the sets
        }
    };
    
    SortedMap<Set<String>, Integer> set = new TreeMap<>(comparator);
    
    // TODO: Fill the set
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to copy all elements of list A into list B. But
I want copy lib dependencies in maven into lib directory when install project. I
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I want to copy a selection of lines from a file to another file
I want to copy few files into an existing zip file using ANT script.
I want to copy directories and their subdirectories from C: to D: with same
I want to copy a xml file from one remote box to a bunch
By a question that I made, I figured out that tho copy elements from
I have a large list l . I want to create a view from
I have a PHP array that I'd like to duplicate but only copy elements

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.