I have sets of objects CustomObjectA, CustomObjectB, CustomObjectC... that I need to pass to a method. I would like to put them in a Map of type Map&lt; String, Set&lt; sObject &gt; &gt;, but when I try, I get an error:  Incompatible types since an instance of Set&lt;CustomObjectA&gt; is never an instance of Set&lt;sObject&gt;. Incompatible types since an instance of Set&lt;CustomObjectB&gt; is never an instance of Set&lt;sObject&gt;.  When I change it to lists instead of a sets (using Map&lt;String, List&lt;sObject&gt;&gt;) and lists of my custom objects, I don't encounter the same problem. Why would that occur?
122845
As most stories here begin, I was writing some code for a project that I was working on, and stumbled across an oddity when writing some unit tests. I was testing a method that has a Set&lt;sObject&gt; parameter, and I was attempting to pass this method a Set&lt;Account&gt;.  As Account inherits from sObject, I thought this would work fine. However, when running my test, I got the error     Method does not exist or incorrect signature: [TestClass].someMethod(Set&lt;Account&gt;)   Yes, the method that I was testing expects an argument of Set&lt;sObject&gt;, but an Account can be up-cast to an sObject, and Salesforce handles that implicitly, right?  Since Salesforce was throwing a fit when I tried to rely on implicit casting, the next thing I tried was explicitly up-casting. When I tried to explicitly up-cast my set, I was greeted with a different error     Line: someNumber, Column: otherNumber   Incompatible types since an instance of Set&lt;Account&gt; is never an instance of Set&lt;SObject&gt;   This can be reproduced with the following snippet  Set&lt;sObject&gt; testSet1; Set&lt;Account&gt; testSet2 = new Set&lt;Account&gt;();  testSet1 = (Set&lt;sObject&gt;)testSet2;   I haven't found anything in the documentation to suggest that up-casts for sets aren't allowed. Given that we can up-cast Lists and Maps, I feel this is rather unexpected.  Furthermore, knowing that an Apex set is really a Java HashSet in the back-end (look at the bottom of ), I tried to look for documentation to see if this is somehow dictated by Java, but I came up empty there as well.  I've been able to move on with my project, but this leaves me wondering...  Is there some documentation (Salesforce or Java) that explains this behavior? Failing that, can someone cobble together a feasible explanation for why Sets can't be up-cast?
114975
I need a list of integers from 1 to x where x is set by the user.  I could build it with a for loop eg assuming x is an integer set previously:  List&lt;int&gt; iList = new List&lt;int&gt;(); for (int i = 1; i &lt;= x; i++) {     iList.Add(i); }   This seems dumb, surely there's a more elegant way to do this, something like the
1296
Why isn't  generic?    Seems like Collection&lt;E&gt; could have boolean remove(E o);   Then, when you accidentally try to remove (for example) Set&lt;String&gt; instead of each individual String from a Collection&lt;String&gt;, it would be a compile time error instead of a debugging problem later.
16134
I have two complex (i.e. objects with string, int, double, List and other home made data type) objects of the same type. I would like to compare the content of both of them to ensure that they are identical. Note: The object doesn't implement .Equals (I have no control on that) and doesn't implement IComparable.   Is there a generic way (reflection?) to compare the content of the two objects?  Thanks!
76755
I have a bit of code where I've written a SOQL query that returns a list of SObject records, however I need a Set&lt;Id&gt; structure to pass to another method that I don't have control of (and thus can't change the method signature of).  How can I turn my returned List&lt;SObject&gt; into a Set&lt;Id&gt;? Is the best option just a for loop?
31501
Are sObjects supported as the keys for ?  I thought they were, but it appears that any subsequent changes to the key sObject break the mapping to the value.  This might be better shown with an anonymous apex example (API v27.0).  Map&lt;OpportunityLineItem, string&gt; testMap = new Map&lt;OpportunityLineItem, string&gt;(); OpportunityLineItem testOli = new OpportunityLineItem(); testOli.Description = 'foo'; testMap.put(testOli, 'bar'); System.assert(testMap.containsKey(testOli)); string mapValue = testMap.get(testOli); System.assertEquals('bar', mapValue);  // After this the OLI will no longer be a key in the map. testOli.OpportunityId = '0064000000RB2eJ';  System.assert(testMap.containsKey(testOli), 'Expected to still contain OLI'); string mapValue2 = testMap.get(testOli); System.assertEquals('bar', mapValue2, 'OLI instance still expected to map to string bar');   This fails for me with:     System.AssertException: Assertion Failed: Expected to still contain OLI   Is this the expected behaviour?
113807
Why does the following code not work?  I want to instantiate prefabs in a list and then use  it as a GameObject like that :  public GameObject prefab;  void Start() {   ArrayList players = new ArrayList();    for(int i=0; i&lt;5; i++)   {         players.Add( (GameObject)Instantiate(prefab));   }    players[2].transform.position = new Vector3(1, 1, 1);  }   there is an error message
110097
I often make a collection field unmodifiable before returning it from a getter method:  private List&lt;X&gt; _xs; .... List&lt;X&gt; getXs(){   return Collections.unmodifiableList(_xs); }   But I can't think of a convenient way of doing that if the X above is itself a List:  private List&lt;List&lt;Y&gt;&gt; _yLists; ..... List&lt;List&lt;Y&gt;&gt; getYLists() {   return Collections.unmodifiableList(_yLists); }   The problem in the above is of course that though the client cannot modify the List of lists, it can add/delete Y objects from the embedded lists.  Any thoughts?
40820
I have few methods that returns different Generic Lists.   Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this.  IF i have this:  List&lt;Whatever&gt; whatever = new List&lt;Whatever&gt;();   (This next code doesn't work of course, but i would like to have the possibility of:  DataTable dt = (DataTable) whatever;
27628
How would you initialise a static Map in Java?  Method one: static initialiser  Method two: instance initialiser (anonymous subclass) or some other method?  What are the pros and cons of each?  Here is an example illustrating the two methods:  import java.util.HashMap; import java.util.Map;  public class Test {     private static final Map&lt;Integer, String&gt; myMap = new HashMap&lt;&gt;();     static {         myMap.put(1, "one");         myMap.put(2, "two");     }      private static final Map&lt;Integer, String&gt; myMap2 = new HashMap&lt;&gt;(){         {             put(1, "one");             put(2, "two");         }     }; }
14775
There is a case where a map will be constructed, and once it is initialized, it will never be modified again.  It will however, be accessed (via get(key) only) from multiple threads.  Is it safe to use a java.util.HashMap in this way?  (Currently, I'm happily using a java.util.concurrent.ConcurrentHashMap, and have no measured need to improve performance, but am simply curious if a simple HashMap would suffice.  Hence, this question is not "Which one should I use?" nor is it a performance question.  Rather, the question is "Would it be safe?")
56376
I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the best practices around using mutable objects? Should you avoid them whenever possible?
76800
Do Apex collection classes implement Iterable interface? I want to write a generic function that can take any of the collection classes that support foreach. Is this possible?
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?
25516
I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?
30943
I think there must be something subtle going on here that I don't know about. Consider the following:  public class Foo&lt;T&gt; {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } }   Suppose that your main method contains the following:  Foo&lt;Double&gt; f = new Foo&lt;Double&gt;(); Double[] d = f.getA();   You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.  Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:  Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException   And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?  Thanks.
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?