Java:
Describe one example of how generics are used in the Collections Framework to avoid a common programming mistake.Describe one example of how generics are used in the Collections Framework to avoid a common programming mistake.
Generics allows a type or method to operate on objects of various types while providing compile-time type safety, making Java a fully statically typed language.
Generics are implemented using Type Erasure
In Java a class or an interface can be declared to define one or more type parameters and those type parameters should be provided at the time of object construction. For example,
List<Long> list = new ArrayList<Long>();list.add(Long.valueOf(1));list.add(Long.valueOf(2));
In the example shown above a List is created which can only contain elements of type Long and if you try to add any other type of element to this list, it will give you compile time error. It helps detect errors at compile time and makes your code safe. Once this piece of code is compiled ,the type information is erased resulting into similar byte code as we would have got if the same piece of code was written using Java 1.4 and below. This results in binary compatibility between different versions of Java. So, a List or List<> are all represented at run-time by the same type, List.
Generics does not support sub-typing
Generics does not support sub-typing which means that List is not considered to be a sub-type of List, where S is a subtype of T. For example,
List<Number> numbers = new ArrayList<Integer>(); // will not compile
The piece of code shown above will not compile because if it compiles than type safety can't be achieved. To make this more clear, lets take the following piece of code shown below where at line 4 we are assigning a list of long to a list of numbers. This piece of code does not compile because if it could have compiled we could add a double value in a List of longs. This could have resulted in ClassCastException at runtime and type safety could not be achieved.
List<Long> list = new ArrayList<Long>();list.add(Long.valueOf(1));list.add(Long.valueOf(2));List<Number> numbers = list; // this will not compilenumbers.add(Double.valueOf(3.14));
You can't create Generic Arrays
You can't create generic arrays as shown below because arrays carry runtime type information about the type of elements . Arrays uses this information at runtime to check the type of the object it contains and will throw ArrayStoreException if the type does not match. But with Generics type information gets erased and the array store check will succeed in cases where it should fail.
T[] arr = new T[10];// this code will not compile
You can't even create Arrays of Generic classes of interfaces. For example, the code shown below does not compile.
List<Integer>[] array = new List<Integer>[10]; // does not compile
Arrays behave differently from the collections because arrays are covariant by default, which means that S[] is a subtype of T[] whenever S is a subtype of T, where as Generics does not support covariance. So, if the above code had compiled then the array store check would succeed in cases where it should fail. For example,
List<Integer>[] ints = new List<Integer>[10]; // does not compileObject[] objs = ints;List<Double> doubles = new ArrayList<Double>();doubles.add(Double.valueOf(12.4));objs[0] = doubles; // this check should fail but it succeed
If the generic arrays were allowed, then we could assign ints array to an object array because arrays are covariant. After that we could add a List of double to the obj array. We will expect that this will fail with ArrayStoreException because we are assigning List of double to an array of List of integers. But the JVM cannot detect type mismatch because the type information gets erased. Hence the array store check succeeds, although it should have failed.
Use of wildcards with extends or super to increase API flexibility
There are times that you need to work not only with T but also with sub types of T. For example, the addAll method in the Collection interface which adds all the elements in the specified collection to the collection on which it is called. addAll method has the signature
boolean addAll(Collection<? extends E> c)
This ? extends E makes sure that you can not only add collection of type E but also of subtype of E. ? is called the wildcard and ? is said to be bounded by E. So,if we create a List of Number then we can not only add List of number but we can also add List of Integer or any other subtype of Number.
List<Number> numbers = new ArrayList<Number>();ArrayList<Integer> integers = new ArrayList<Integer>();ArrayList<Long> longs = new ArrayList<Long>();ArrayList<Float> floats = new ArrayList<Float>();numbers.addAll(integers);numbers.addAll(longs);numbers.addAll(floats);
So far we have covered the use of extends with wildcards and we saw that API became more flexible after using extends . But where will we use super? Collections class has a method called addAll which add all the specified elements to the specified collection. It has following signature
public static <T> boolean addAll(Collection<? super T> c, T... elements) ;
In this method you are adding elements of type T to the
collection c. super is used instead of extends because elements are
added into the collection c whereas in the previous example of
Collection interface addAll method elements were read from the
collection . In the Effective Java book, Joshua Bloch calls this
PECS . PECS stands for producer extends,
consumer super.
It proves very helpful whenever you are confused about whether you
should use extends or super.
Use of Multiple Bounds
Multiple bounds is one of the generics features which most developer do not know. It allows a type variable or wildcard to have multiple bounds. For example, if you to define constraint such as that the type should be a Number and it should implement Comparable.
public static <T extends Number & Comparable<? super T>> int compareNumbers(T t1, T t2){return t1.compareTo(t2);}
It makes sure that you can only compare two numbers which implement Comparable. Multiple bounds follows the same constraints as followed by the a class i.e. T can't extend two classes ,you have to first specify the class then the interface, and T can extend any number of interfaces.
public static <T extends String & Number > int compareNumbers(T t1, T t2) // does not work..can't have two classespubl
Java: Describe one example of how generics are used in the Collections Framework to avoid a...
The 6 C's provides a framework for client care. Describe how this framework can be used in your current practice as a nurse.
Q1 :Explain various interfaces used in Collection framework ? Q2:What are the main differences between array and collection? Q3: why using Generics ?(List three advantages ) Q4 :give an example of how Generics make a program more flexible? Q5 :what is a raw types ? and how avoiding unsafe Raw Types ? Q6 :What is the difference between FileNotFoundException and NoSuchElementException ?
java question have a clear and detailed explanation for rating What is parameterized types/generics? Explain how it eliminates the need for downcasting?
47. What is overriding? Describe using java code showing an example of overriding. 48. Does Java allow us to refine behavior in subclasses? If so, show Java code that demonstrates this property. 49. Does Java allow us to replace behavior in subclasses? If so, show Java code that demonstrates this property. 50. Can dynamic binding be used in C++? If so, show how this is accomplished. 51. Is there a cost to providing the dynamic binding capability? If so, describe...
Describe the socioecological framework of patient advocacy, and give a example for each level
Q1 Describe one example of (a) how artificial intelligence has been used in healthcare to solve a specific problem. (b) What were some of the challenges the project faced? Q2 Post your Al example and address the following points (a) What was the problem the Al was aiming to solve (b) What did the Al do, and (c) how was it successful?
Java Question: Describe what is an ArrayList (API) in Java. Provide an example code segment to describe as well.
7. Describe one example of how fruit color affects foraging behavior in animals 8. There are three mechanisms to avoid self fertilization (temporal, spatial, and self-incompatibility). Describe one example of each of the three mechanisms. 9. Describe how flowers attract different types of pollinators (e.g. bees, flies, moths bats, birds, etc). Choose at least three different pollinators. 10. Explain why a flash of light during the night would prevent flowering in a short- day plant, but wouldn't prevent flowering in...
1.Describe one example of (a) how artificial intelligence has been used in healthcare to solve a specific problem. (b) What were some of the challenges the project faced? 2. (a) What was the problem the artificial intelligence was aiming to solve (b) What did the artificial intelligence do, and (c) how was it successful?
8. Wh at are peptide tags? How could these be used for bioorthogonal reactions? Describe one example.