Best way to create a hashmap of arraylist

You don’t need to re-add the ArrayList back to your Map. If the ArrayList already exists then just add your value to it. An improved implementation might look like: Map<String, Collection<String>> map = new HashMap<String, Collection<String>>(); while processing each line: String user = user field from line String value = value field from line Collection<String> … Read more

How Iterator’s remove method actually remove an object

How exactly Iterator removes elements depends on its implementation, which may be different for different Collections. Definitely it doesn’t break the loop you’re in. I’ve just looked how ArrayList iterator is implemented and here’s the code: public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet … Read more

Casting an array of Objects into an array of my intended class

Because toArray() creates an array of Object, and you can’t make Object[] into DataObject[] just by casting it. toArray(DataObject[]) creates an array of DataObject. And yes, it is a shortcoming of the Collections class and the way Generics were shoehorned into Java. You’d expect that Collection<E>.toArray() could return an array of E, but it doesn’t. … Read more

Symmetric difference of two sets in Java [duplicate]

You’re after the symmetric difference. This is discussed in the Java tutorial. Set<Type> symmetricDiff = new HashSet<Type>(set1); symmetricDiff.addAll(set2); // symmetricDiff now contains the union Set<Type> tmp = new HashSet<Type>(set1); tmp.retainAll(set2); // tmp now contains the intersection symmetricDiff.removeAll(tmp); // union minus intersection equals symmetric-difference

Does an ICollection have an order?

The ICollection<T> interface doesn’t specify anything about an order. The objects will be in the order specified by the object returned. For example, if you return the Values collection of a SortedDictionary, the objects will be in the the order defined by the dictionary’s comparer. If you need the method to return, by contract, a … Read more

API java 5 and more: should I return an array or a Collection?

Prefer Collection (or List, or Set as appropriate) to an array. With generics you get the type-checking that was lacking pre-Java 5. Also, by exposing only the interface, you are free to change the implementation later (e.g. switch an ArrayList for a LinkedList). Arrays and generics don’t mix very well. So, if you want to … Read more

ObservableCollection that also monitors changes on the elements in collection

Made a quick implementation myself: public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged { protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { Unsubscribe(e.OldItems); Subscribe(e.NewItems); base.OnCollectionChanged(e); } protected override void ClearItems() { foreach(T element in this) element.PropertyChanged -= ContainedElementChanged; base.ClearItems(); } private void Subscribe(IList iList) { if (iList != null) { foreach (T element in iList) element.PropertyChanged … Read more

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