Variable Amount of Generics in Java - java

In C++ you can create a tuple with a variable number of parameters. How would I implement something like this in Java without explicitly hard coding the amount of generics. I want to be able to do something like:
Tuple<Integer, Integer, String> t
but without forcing myself to use three items in a tuple.

You can't, that's not supported in Java.

Related

Compiler design: best way to store function signatures?

I am planning on storing all the function signatures which allows overloading.
Right now I have a nested HashMap that looks something like this:
HashMap<String,HashMap<ArrayList<Type>,Object>>
Where the first key, String, contains the name of the function. The second key, ArrayList<Type>, contains parameter data types. Now, I know using ArrayList as a key is a terrible practice, so I wonder if there is a better solution to store the function signatures?
The design is fine. I ended up keeping this design.

Using Java, how can I restrict an object property to have certain values?

Using Java, how can I restrict an object property to have certain values? I want to create a Java object that represents a "type of location" but I want to restrict the use of the class to only about 100 strings representing all possible types? What is the design pattern for this?
All I can think of is to create a String arraylist and each time a user instantiates the object I would iterate through the entire list looking for a match. That seems sorta like a hack to me though and I want to do it right.
How about using Java's Enumerations? Your Object would just by the type of that Enum and then you'd be bounded by the 100 or so Strings you have in your enum.
You can use a HashSet of allowed values (lookup is faster and you only want to know if its contained) for strings or an enumeration.
For a fixed set of 100 or so values, an enum type is the best answer. There are a couple of caveats though:
If the set of value is not fixed ... to the extent that you can hard-wire them into your code ... then enum classes won't work. There is no form of enum class in Java that allows you to add new values to an existing enum class without a recompilation, etcetera.
If you have a really large number of values, the enum class will run into one or more limitations that are imposed by the JVM spec. For instance, the static initialization code generated by the compiler for the enum class cannot consist of more than 64K of bytecodes.
Another thought, since it's a bounded set, would be to create an enum for those Strings. Make the object property an enum type. No worries then.

What does <int> mean in java? [duplicate]

I saw sometimes a type object inside <> beside of another object type declaration.
For instance:
NavigableMap<Double, Integer> colorMap = new TreeMap<Double, Integer>()
or
private final CopyOnWriteArrayList<EventListener> ListenerRecords =
new CopyOnWriteArrayList<EventListener>();
Could you give me an easy explication?
They're known as generics in java, and templates in C++.
http://java.sun.com/developer/technicalArticles/J2SE/generics/
These are called Generics. Here http://java.sun.com/docs/books/tutorial/java/generics/index.html is a tut from sun for them.
As some others said before: Your dealing with Java Generics. They're in Java since SDK 1.5.
E.g:
new CopyOnWriteArrayList<EventListener>()
means that you're creating a new (concurrent) ArrayList which is able to store objects of type EventListener. If you would create an ArrayList the old (pre Java 1.5) way like:
new ArrayList()
All contained objects would be of type Object and you would have to cast them to their real type. See also http://en.wikipedia.org/wiki/Generics_in_Java#Motivation_for_generics.
These are called Generics in Java. They give you a way to tell the compiler what type the collection is going to hold.
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
They're called Generics, and allow the compiler to do type checking of contents of lists etc, and also reduces the amount of casting you have to do in your code.
It's also helpful when reading code, as you know what type of object can be put into the item in question, or what type to expect out of it.
Java's implementation isn't as thorough as C++, as Java's is only available at compile time.
At runtime, the type information is no longer available.
In your example TreeMap the key of the TreeMap has type Double and the value referenced by this key has the type Integer. And as already answered it's called generics.
This is an extension introduced in java 1.5. This makes code more readable
They are Generics, classes that are written with one or more types left to be specified later, so they can be used with any type. The generics can be very useful for containers or algorithms, where the algorithm or the data structure used is independent from the actual type stored or manipulated.

Array vs array [] for java

I am writing a program that will be heavily reliant on ... something ... that stores data like an array where I am able to access any point of the data at any given time as I can in an array.
I know that the java library has an Array class that I could use or I could use a raw array[].
I expect that using the Array type is a bit easier to code, but I expect that it is slightly less efficient as well.
My question is, which is better to use between these two, and is there a better way to accomplish the same result?
Actually Array would be of no help -- it's not what you think it is. The class java.util.ArrayList, on the other hand, is. In general, if you can program with collection classes like ArrayList, do so -- you'll more easily arrive at correct, flexible software that's easier to read, too. And that "if" applies almost all the time; raw arrays are something you use as a last resort or, more often, when a method you want to call requires one as an argument.
The Array class is used for Java reflection and is very, very, rarely used.
If you want to store data in an array, use plain old arrays, indicated with [], or as Gabe's comment on the question suggests, java.util.ArrayList. ArrayList is, as your comment suggests easier to code (when it comes to adding and removing elements!!) but yes, is slightly less efficient. For variable-size collections, ArrayList is all but required.
My question is, which is better to use between these two, and is there a better way to accomplish the same result?
It depends on what you are trying to achieve:
If the number of elements in the array is known ahead of time, then an array type is a good fit. If not, a List type is (at least) more convenient to use.
The List interface offers a number of methods such as contains, insert, remove and so on that can save you coding ... if you need to do that sort of thing.
If properly used, an array type will use less space. The difference is particularly significant for arrays of primitive types where using a List means that the elements need to be represented using wrapper types (e.g. byte becomes Byte).
The Array class is not useful in this context, and neither is the Arrays class. The choice is between ArrayList (or some other List implementation class) and primitive arrays.
In terms of ease of use, the Array class is a lot easier to code.
The array[] is quite a problem in terms of the case that you need to know
the size of the list of objects beforehand.
Instead, you could use a HashMap. It is very efficient in search as well as sorting as
the entire process is carried out in terms of key values.
You could declare a HashMap as:
HashMap<String, Object> map = new HashMap<String, Object>();
For the Object you can use your class, and for key use the value which needs to be unique.

How to store more than one option in a variable - Java

In Java, how would I create a property/field/variable that can at any one time store a couple of options/values, similar to lower level environments where you can set bits in variables, each meaning different things. I know that one can do this in Java by using static finals or something and then just ORing them all together, but isn't there a more elegant solution? E.g in C# where you can have the [Flags] attribute to an Enum and then OR the states/values together into an instance of that Enum.
Any ideas?
Renault
Well, you can use an enum and then have an EnumSet for that enum. It's hard to know exactly what to recommend without more information. EnumSet is a good choice when you're naturally dealing with just "options" - but if you have multiple values (e.g. height and width; a fixed number of values which just naturally come together) then I'd just create a new type to encapsulate them.
If you could give concrete examples of what you're looking for, we may be able to help more.
The old-fashioned way (that you can still find in many libraries) would be to encode a bit mask using either an int value or a BitSet, but since JDK 1.5, an EnumSet is the preferred way to do it, as Jon Skeet says.
You mean like a C++ union? This is not allowed in Java because you lose type safety; this would allow you to get a reference to an Integer typed as a String, for example.

Categories

Resources