Deserialize a List object with Gson?

Method to deserialize generic collection: import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; … Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType(); List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType); Since several people in the comments have mentioned it, here’s an explanation of how the TypeToken class is being used. The construction new TypeToken<…>() {}.getType() captures a compile-time type (between the < and >) … Read more

Possible heap pollution via varargs parameter

Heap pollution is a technical term. It refers to references which have a type that is not a supertype of the object they point to. List<A> listOfAs = new ArrayList<>(); List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As This can lead to “unexplainable” ClassCastExceptions. // if the heap never gets polluted, this … Read more

List or IList [closed]

If you are exposing your class through a library that others will use, you generally want to expose it via interfaces rather than concrete implementations. This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won’t need … Read more

How to remove elements from a generic list while iterating over it?

Iterate your list in reverse with a for loop: for (int i = safePendingList.Count – 1; i >= 0; i–) { // some code // safePendingList.RemoveAt(i); } Example: var list = new List<int>(Enumerable.Range(1, 10)); for (int i = list.Count – 1; i >= 0; i–) { if (list[i] > 5) list.RemoveAt(i); } list.ForEach(i => Console.WriteLine(i)); … Read more

Get generic type of class at runtime

As others mentioned, it’s only possible via reflection in certain circumstances. If you really need the type, this is the usual (type-safe) workaround pattern: public class GenericClass<T> { private final Class<T> type; public GenericClass(Class<T> type) { this.type = type; } public Class<T> getMyType() { return this.type; } }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)