A dictionary with multiple entries with the same key [duplicate]

In .NET 3.5 you can use a Lookup instead of a Dictionary. var items = new List<KeyValuePair<int, String>>(); items.Add(new KeyValuePair<int, String>(1, “first”)); items.Add(new KeyValuePair<int, String>(1, “second”)); var lookup = items.ToLookup(kvp => kvp.Key, kvp => kvp.Value); foreach (string x in lookup[1]) { Console.WriteLine(x); } The Lookup class is immutable. If you want a mutable version you … Read more

What is the use of Collections.synchronizedList() method? It doesn’t seem to synchronize the list

A synchronized list only synchronizes methods of this list. It means a thread won’t be able to modify the list while another thread is currently running a method from this list. The object is locked while processing method. As an example, Let’s say two threads run addAllon your list, with 2 different lists (A=A1,A2,A3 and … Read more

How to check if a generic type implements a specific type of generic interface in java? [duplicate]

Java implements erasure, so there’s no way to tell on runtime if genericObject is an instance of Set<String> or not. The only way to guarantee this is to use bounds on your generics, or check all elements in the set. Compile-time Generic Bounds Using bounds checking, which will be checked at compile-time: public <T extends … Read more

Java : convert List of Bytes to array of bytes

The compiler doesn’t like it, because byte[] isn’t Byte[]. What you can do is use commons-lang’s ArrayUtils.toPrimitive(wrapperCollection): Byte[] bytes = pdu.toArray(new Byte[pdu.size()]); return ArrayUtils.toPrimitive(bytes); If you can’t use commons-lang, simply loop through the array and fill another array of type byte[] with the values (they will be automatically unboxed) If you can live with Byte[] … Read more

Can java annotation have complex return type like HashMap

No, annotation elements can only be primitive types, Strings, enum types, Class, other annotations, or arrays of any of these. The typical way to represent these kinds of structures would be to declare another annotation type public @interface TableMapping { public String dbName(); public String tableName(); } then say @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface column { … Read more

What is the best way to store pairs of strings, make an object or use a class in .NET?

The “pair” generic class for .NET is Tuple. You would use it like: var strings=new List<Tuple<string, string>>(); strings.Add(Tuple.Create(“REFERENCE”, “Ref”)); A dictionary is a perfectly acceptable substitute if the left-most string is unique (ie a key). You’ll get errors otherwise. As to whether it’s better to use the built-in collections or create an actual object, depends … Read more

What determines ascending or descending order in Comparator / Comparable collection class?

What is logic behind ordering object elements? how “(this.grade – s.grade)” if positive 1 moves “this.grade” front and puts “s.grade” next in order, why not other way around? Using negative numbers to say “this is less than that”, positive numbers to say “this is more than that” and 0 to say “these 2 things are … Read more

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