• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

A good way to sort a List into categories?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have around 60+ distinct items which I would like to set in categories. I would like to add these items to a category based on a condition.

See my code below:



Category is an enum with all the existing categories.

Now, instead of having over 50+ if and else statements, what would be a good way to do this?

Thank you in advance,

Kevin.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Switch statements? Create a string variable to store how the string ends, and pass it to the switch statement.

I think you should be familiar with them by now, but in any case, here is the
The Switch Statement tutorial by Oracle.

Can't think of any solution less tiring than this.
Hope it helps
 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Garcia wrote:Category is an enum with all the existing categories.


Can you share what your Category class looks like?
 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Garcia wrote:



If there's some delimiter that precedes the 'endsWith', you could extract the endsWith using lastIndexOf(ch). Then instead of using an Enum you could use a String for your map.
If you really need to use an enum I don't see a way without nested loops. In which case I'd expand your enum to include an endsWith field.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing, as you have used example list, map and String as local variable so you don't have to use final. Also, String already final so you don't have to declare it as final.
 
Sheriff
Posts: 27463
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:String already final so you don't have to declare it as final.



I'm not sure what you mean by this. Perhaps you are confusing immutable (can't change the state of an object) with final (can't change a variable to refer to a different object)?
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably with the groupBy method of a Stream. Well, actually I think you use collect() and Collectors.groupingBy. Haven't got the time to look more closely at the moment. Sorry.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic