I am learning about interfaces in Java and one example they give is being able to create a linked list using the List interface List&lt;String&gt; strings = new LinkedList&lt;&gt;(); but they don't go into any detail. Why would I want to do this as oppose to LinkedList&lt;String&gt; strings = new LinkedList&lt;&gt;();?
685
I have seen this mentioned a few times and I am not clear on what it means. When and why would you do this?   I know what interfaces do, but the fact I am not clear on this makes me think I am missing out on using them correctly.   Is it just so if you were to do:  IInterface classRef = new ObjectWhatever()   You could use any class that implements IInterface? When would you need to do that? The only thing I can think of is if you have a method and you are unsure of what object will be passed except for it implementing IInterface. I cannot think how often you would need to do that.   Also, how could you write a method that takes in an object that implements an interface? Is that possible?
19900
I have just found a static nested interface in our code-base.  class Foo {     public static interface Bar {         /* snip */     }     /* snip */ }   I have never seen this before. The original developer is out of reach. Therefore I have to ask SO:  What are the semantics behind a static interface? What would change, if I remove the static? Why would anyone do this?
27319
I want to use a collection initializer for the next bit of code:  public Dictionary&lt;int, string&gt; GetNames() {     Dictionary&lt;int, string&gt; names = new Dictionary&lt;int, string&gt;();     names.Add(1, "Adam");     names.Add(2, "Bart");     names.Add(3, "Charlie");     return names; }   So typically it should be something like:  return new Dictionary&lt;int, string&gt; {     1, "Adam",    2, "Bart"    ...   But what is the correct syntax for this?
79010
Take the following class as an example:  class Sometype {     int someValue;      public Sometype(int someValue)     {         this.someValue = someValue;     } }   I then want to create an instance of this type using reflection:  Type t = typeof(Sometype); object o = Activator.CreateInstance(t);   Normally this will work, however because SomeType has not defined a parameterless constructor, the call to Activator.CreateInstance will throw an exception of type MissingMethodException with the message "No parameterless constructor defined for this object." Is there an alternative way to still create an instance of this type? It'd be kinda sucky to add parameterless constructors to all my classes.
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?
67060
I want to be able to have LinkedList.contains() return true for a custom comparator.  Suppose that I have 1 LinkedList and 2 objects  LinkedList&lt;MyObject&gt; myList = new LinkedList&lt;MyObject&gt;();  MyObject a = new MyObject("HELLO"); MyObject b = new MyObject("HELLO");   Technicaly, both objects are identical in terms of comparison (MyObject implements Comparable)   ( a == b ) == true  however, when I do the following, myList does not return true for myList.contains(b)  myList.add(a) myList.contains(b) // == false   I think its because contains will check object reference and see that a and b are 2 distinct objects. Is there any way I can make it so I don't have to extend LinkedList to compare those objects?
79065
I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other?
50500
A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain.  For example, given the interface:  public interface IAnything {   void m1();   void m2();   void m3(); }   the following abstract class gets merrily compiled without a warning or an error:  public abstract class AbstractThing implements IAnything {   public void m1() {}   public void m3() {} }   Can you explain why?
24065
I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc.  So, what are the main differences between C++, C#, Java in generics? Pros/cons of each?
91748
Why would someone want to use a linked-list over an array?  Coding a linked-list is, no doubt, a bit more work than using an array and one may wonder what would justify the additional effort.  I think insertion of new elements is trivial in a linked-list but it's a major chore in an array. Are there other advantages to using a linked list to store a set of data versus storing it in an array?  This question is not a duplicate of  because the other question is asking specifically about a particular Java class while this question is concerned with the general data structures.
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
5431
I want to have a look at how Java implements LinkedList. Where should I go to look at the source code?
99253
I like how Java has a Map where you can define the types of each entry in the map, for example &lt;String, Integer&gt;.   What I'm looking for is a type of collection where each element in the collection is a pair of values.  Each value in the pair can have its own type (like the String and Integer example above), which is defined at declaration time.    The collection will maintain its given order and will not treat one of the values as a unique key (as in a map).    Essentially I want to be able to define an ARRAY of type &lt;String,Integer&gt; or any other 2 types.    I realize that I can make a class with nothing but the 2 variables in it, but that seems overly verbose.    I also realize that I could use a 2D array, but because of the different types I need to use, I'd have to make them arrays of OBJECT, and then I'd have to cast all the time.    I only need to store pairs in the collection, so I only need two values per entry.  Does something like this exist without going the class route?  Thanks!
103027
In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java?
67180
I remember reading in some Java book about any operator other than 'instanceof' for comparing the type hierarchy between two objects.  instanceof is the most used and common. I am not able to recall clearly whether there is indeed another way of doing that or not.
16584
would report a violation for:  ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   The violation was "Avoid using implementation types like 'ArrayList'; use the interface instead".  The following line would correct the violation:  List&lt;Object&gt; list = new ArrayList&lt;Object&gt;();   Why should the latter with List be used instead of ArrayList?
60345
C# doesn't require you to specify a generic type parameter if the compiler can infer it, for instance:  List&lt;int&gt; myInts = new List&lt;int&gt; {0,1,1,     2,3,5,8,13,21,34,55,89,144,233,377,     610,987,1597,2584,4181,6765};  //this statement is clunky List&lt;string&gt; myStrings = myInts.     Select&lt;int,string&gt;( i =&gt; i.ToString() ).     ToList&lt;string&gt;();  //the type is inferred from the lambda expression //the compiler knows that it's taking an int and  //returning a string List&lt;string&gt; myStrings = myInts.     Select( i =&gt; i.ToString() ).     ToList();   This is needed for anonymous types where you don't know what the type parameter would be (in intellisense it shows up as 'a) because it's added by the compiler.  Class-level type parameters don't let you do this:  //sample generic class public class GenericDemo&lt;T&gt;  {     public GenericDemo ( T value )      {         GenericTypedProperty = value;     }      public T GenericTypedProperty {get; set;} }  //why can't I do: int anIntValue = 4181; var item = new GenericDemo( anIntValue ); //type inference fails  //however I can create a wrapper like this: public static GenericDemo&lt;T&gt; Create&lt;T&gt; ( T value ) {     return new GenericDemo&lt;T&gt; ( value ); }  //then this works - type inference on the method compiles var item = Create( anIntValue );   Why doesn't C# support this class level generic type inference?