Questions tagged [enumeration]

The process of enumerating values, for example from some collection.

Use enums for enumeration types.

1779 questions
74
votes
7 answers

Iterate an Enumeration in Java 8

Is it possible to iterate an Enumeration by using Lambda Expression? What will be the Lambda representation of the following code snippet: Enumeration nets = NetworkInterface.getNetworkInterfaces(); while (nets.hasMoreElements())…
Tapas Bose
  • 25,780
  • 71
  • 202
  • 317
67
votes
5 answers

Why aren't Enumerations Iterable?

In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable: for (Object o : list) { doStuff(o); } However, Enumerable still does not implement Iterable, meaning that to iterate over an Enumeration…
SCdF
  • 51,261
  • 23
  • 74
  • 108
66
votes
7 answers

iterating through Enumeration of hastable keys throws NoSuchElementException error

I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list? Hashtable vars = new Hashtable(); vars.put("POSTCODE","TU1…
David Cunningham
  • 889
  • 1
  • 11
  • 22
61
votes
5 answers

What are the differences between a Java enum and a class with private constructor?

I was trying to understand how Java enum really works and I have come to the conclusion that it is very similar to a normal Java class that has its constructor declared private. I have just come to this conclusion and it isn't based on much…
user9349193413
  • 1,113
  • 2
  • 12
  • 26
60
votes
9 answers

Lookup Tables Best Practices: DB Tables... or Enumerations

If we have to store the available positions at a company (i.e. Manager, Team Lead, ... etc). What are the best practices for storing it? I have two opinions with comments... "sure, welcoming yours" Storing it as DB table with columns ID and Name,…
Bishoy Moussa
58
votes
5 answers

Java getting the Enum name given the Enum Value

How can I get the name of a Java Enum type given its value? I have the following code which works for a particular Enum type, can I make it more generic? public enum Category { APPLE("3"), ORANGE("1"), private final String…
Julia
  • 581
  • 1
  • 4
  • 4
58
votes
3 answers

Do C++ enums Start at 0?

If I have an enum that does not assign numbers to the enumerations, will it's ordinal value be 0? For example: enum enumeration { ZERO, ONE, TWO, THREE, FOUR, …
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
54
votes
7 answers

Enumeration extension methods

In vs2008, is it possible to write an extension methods which would apply to any enumeration. I know you can write extension methods against a specific enumeration, but I want to be able to every enumeration using a single extension method. Is this…
Eric Haskins
  • 8,227
  • 11
  • 35
  • 47
52
votes
3 answers

Java: Enumeration from Set

I have a simple collections question. I have a Set object. I want an Enumeration of the Strings in that Set. I need an Enumeration since I am overriding a method that specifically returns an Enumeration. What is the…
well actually
  • 10,670
  • 19
  • 49
  • 67
45
votes
11 answers

Which Typesafe Enum in C++ Are You Using?

It is common knowledge that built-in enums in C++ are not typesafe. I was wondering which classes implementing typesafe enums are used out there... I myself use the following "bicycle", but it is somewhat verbose and limited: typesafeenum.h: struct…
Alex Jenter
  • 4,112
  • 3
  • 33
  • 59
43
votes
6 answers

What is the best way to handle constants in Ruby when using Rails?

I have some constants that represent the valid options in one of my model's fields. What's the best way to handle these constants in Ruby?
Miles
  • 1,561
  • 1
  • 15
  • 21
42
votes
3 answers

Elixir - Looping through and adding to map

I'm rebuilding something in Elixir from some code I built in C#. It was pretty hacked together, but works perfectly (although not on Linux, hence rebuild). Essentially what it did was check some RSS feeds and see if there was any new content. This…
William Dunne
  • 479
  • 1
  • 5
  • 12
40
votes
9 answers

Enumerate over an enum in C++

In C++, Is it possible to enumerate over an enum (either runtime or compile time (preferred)) and call functions/generate code for each iteration? Sample use case: enum abc { start a, b, c, end } for each…
jameszhao00
  • 6,903
  • 14
  • 54
  • 109
40
votes
6 answers

Iteration over a sealed trait in Scala?

I just wanted to know if it is possible to iterate over a sealed trait in Scala? If not, why is it not possible? Since the trait is sealed it should be possible no? What I want to do is something like that: sealed trait ResizedImageKey { /** *…
Sebastien Lorber
  • 79,294
  • 59
  • 260
  • 386
39
votes
11 answers

How to add a method to Enumeration in Scala?

In Java you could: public enum Enum { ONE { public String method() { return "1"; } }, TWO { public String method() { return "2"; } }, THREE { public String method()…
Etam
  • 4,329
  • 10
  • 38
  • 57