Skip to main content
"Natural ordering misunderstanding"
Source Link
Funky
  • 950
  • 1
  • 8
  • 16

As ArrayLists don't keep a naturalWhen you insert into an ArrayList the "natural" ordering of the types is not accounted for as such the List will not be in alphabetical order beforebut in the order you put the data in. Before you return you will have to call Collections.sort() which will order each string in alphabetical order

As ArrayLists don't keep a natural order before you return you will have to call Collections.sort() which will order each string in alphabetical order

When you insert into an ArrayList the "natural" ordering of the types is not accounted for as such the List will not be in alphabetical order but in the order you put the data in. Before you return you will have to call Collections.sort() which will order each string in alphabetical order

Links for HashSet and TreeSet and ArrayList and Collections.sort
Source Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144

If you wish to only use simpler data types (primitive and ArrayListArrayList) then the following will apply

As ArrayLists don't keep a natural order before you return you will have to call Collections.sort()Collections.sort() which will order each string in alphabetical order

If you wish to only use simpler data types (primitive and ArrayList) then the following will apply

As ArrayLists don't keep a natural order before you return you will have to call Collections.sort() which will order each string in alphabetical order

If you wish to only use simpler data types (primitive and ArrayList) then the following will apply

As ArrayLists don't keep a natural order before you return you will have to call Collections.sort() which will order each string in alphabetical order

Links for HashSet and TreeSet
Source Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144

Firstly HashSetHashSet is an object that holds unique objects meaning no two strings can match inside therefore you should use:

A TreeSetTreeSet can be used to store the remaining words whilst keeping the "natural" ordering

Firstly HashSet is an object that holds unique objects meaning no two strings can match inside therefore you should use:

A TreeSet can be used to store the remaining words whilst keeping the "natural" ordering

Firstly HashSet is an object that holds unique objects meaning no two strings can match inside therefore you should use:

A TreeSet can be used to store the remaining words whilst keeping the "natural" ordering

Added ArrayList only answer
Source Link
Funky
  • 950
  • 1
  • 8
  • 16
Loading
Source Link
Funky
  • 950
  • 1
  • 8
  • 16
Loading