Home > Back-end >  A collection of
A collection of

Time:09-21

Treeset collection cannot be stored in the collection of HashSet type? If not, why not?

CodePudding user response:

refer to the building of a 223317 response:
Treeset collection cannot be stored in the collection of HashSet type? If not, why not?


Treeset cannot put null values, hashset can fit into a null value, if there is a null hashset, deposit not treeset

CodePudding user response:

Don't understand your question
Deposit is treeset hashset elements, or deposit treeset hashset object?
1 if it is to point to the former, with the LS said treeset elements cannot be null, treeset elements to implement the Comparable interface, because the internal invokes the Comparable interface method to sort (so null element method call interface will be a null pointer exception, if not null elements not achieve Comparable interface can also because can't find the interface method and error)
2 if it is to point to the latter, because the hashset itself does not implement the Comparable interface, so can't store

CodePudding user response:

refer to the second floor qybao response:
don't understand your question
Deposit is treeset hashset elements, or deposit treeset hashset object?
1 if it is to point to the former, with the LS said treeset elements cannot be null, treeset elements to implement the Comparable interface, because the internal invokes the Comparable interface method to sort (so null element method call interface will be a null pointer exception, if not null elements not achieve Comparable interface can also because can't find the interface method and error)
2 if it is to point to the latter, because the hashset itself does not implement the Comparable interface, so you can't store

Is I created three collections:
A hashset type set of type String is put inside a
A collection of treeset type b type int is put inside the element
A collection of the arraylist type c inside a custom object of type
Then I created a collection of treeset type D, placed inside the above three collections, the runtime adds the collection statement in a to D make a mistake, is this why?

CodePudding user response:

Not to say, the treeset can only add to implement the Comparable interface object, hashset didn't realize the Comparable interface, so can't deposit treeset

CodePudding user response:

references a 223317 response: 3/f
Quote: refer to the second floor qybao response:
don't understand your question
Deposit is treeset hashset elements, or deposit treeset hashset object?
1 if it is to point to the former, with the LS said treeset elements cannot be null, treeset elements to implement the Comparable interface, because the internal invokes the Comparable interface method to sort (so null element method call interface will be a null pointer exception, if not null elements not achieve Comparable interface can also because can't find the interface method and error)
2 if it is to point to the latter, because the hashset itself does not implement the Comparable interface, so you can't store

Is I created three collections:
A hashset type set of type String is put inside a
A collection of treeset type b type int is put inside the element
A collection of the arraylist type c inside a custom object of type
Then I created a collection of treeset type D, placed inside the above three collections, the runtime adds the collection statement in a to D make a mistake, is this why?


If non-null hashset can manually deposit method with the treeset, want to direct deposit is impossible,

CodePudding user response:

Do you use a collection of generic statement D?
HasSet CollectionA=new HashSet<> (a);
.
TreeSet CollectionB=new TreeSet<> (a);
.
ArrayList CollectionC=new ArrayList<> (a);
.

TreeSet CollectionD=new TreeSet<> (a);
CollectionD. Add (collectionA);
CollectionD. Add (collectionB);
CollectionD. Add (collectionC);

CodePudding user response:

Set D generic write wrong? To use unified Collection as generics, because, A, B, C public interface is A Collection,
  • Related