collections

Save a list of unique strings in the ArrayList

I read data from a text file, so there may be John Mary John Leeds and I need to get 3 elements in the ArrayList, cause there are only 3 unique values in the file. I can use the hidden HashTable and add information to it, then simply copy its data into the List. Are there other solutions? ...

Using Java generics in interfaces that return collections. Best practice? Pitfalls?

I ran into some code today that I found questionable. Here's a simplified example (not realistic). public interface IListable { //returns first n items from list public ArrayList getFirstNThings(int n); //returns last n items from list public ArrayList getLastNThings(int n); } Then there's an implementor like so: pu...

How to convert an IEnumerable<IEnumerable<T>> to a IEnumerable<T>

I have an IEnumerable<IEnumerable<T>> collection that I want to convert to a single dimension collection. Is it possible to achieve this with a generic extension method? Right now I'm doing this to achieve it. List<string> filteredCombinations = new List<string>(); //For each collection in the combinated results collection foreach (var...

Collection.synchronizedMap vs synchronizing individual methods in HashMap

What is the difference between a Collections.synchronizedMap() and a wrapper around a HashMap with all the methods synchronized. I dont see any difference becuase Collections.synchronizedMap() internally maintains the same lock for all methods. Basically, what is the difference between the following code snippets Class C { Obje...

Will Microsoft ever make all collections useable by LINQ?

I've been using LINQ for awhile (and enjoy it), but it feels like I hit a speedbump when I run across .NET specialized collections(DataRowCollection, ControlCollection). Is there a way to use LINQ with these specialized controls, and if not do you think Microsoft will address this in the next release of the framework? Or are we left to...

C# Library for a collection with many-to-many associations between items? (e.g. one node table & one relationship table)

Hi, Is anyone aware of a C# library that provide a means to manage a Web like map of items, which each item can have relationships to other items in the collection/table. As an example an implementation of this with a datastore might see the schema looking like the below. Picture a page with node objects scattered across it with lines...

Java's Collections.shuffle is doing what?

I recently found myself needing to be sure my list wasn't in order. Hibernate was nice enough to return it in perfect order. Silly hibernate, not reading my mind. I looked at my Java API and it tells me its shuffle method does this: Randomly permutes the specified list using a default source of randomness. Being the curious george t...

is HashSet is implemented via a HashMap instance

I am wondering since the HashSet is implemented via a HashMap instance , what would be the key that would be used to put data into HashSet. i gone through the link http://www.coderanch.com/t/251832/Programmer-Certification-SCJP/certification/Difference-HashMap-HashSet... which i dint understood properly.. Can anybody help me to underst...

Iterator Concurrent Modifiction Exception

This code will throw Concurrent Modification Exception if the list is modified in doSomething(). Is it possible to avoid it by enclosing the code in some synchronized block? List l = Collections.synchronizedList(new ArrayList()); // normal iteration -- can throw ConcurrentModificationException // may require external synchronization fo...

Why do ImmutableList.of() and friends prohibit null elements?

Summary pretty much says it all. Here's the relevant snippet of code in ImmutableList.createFromIterable(): if (element == null) { throw new NullPointerException("at index " + index); } I've run into this several times and can't see why a general-purpose library function should impose this limitation. Edit 1: by "general-pur...

How to get next object in IEnumberable / IQueryable if ID key is not consistent?

Hello everybody, After hacking more on my current app - once again, I am running into an issue which kills some of the joy I expected from my domain model. The problem here is that the aggregate root / most important class in my domain model doesn't have consistent ID values for its entries. (E.g messed up : 1..3..5..12..150..157.. an...

filling a collection using an iterator

Google Collections has an Iterables utility class for taking in a collection and iterable and putting all the elements from iterable into the collection, called addAll. Is there something similar when all you have is an iterator? ...

is there java.concurrent.util (or equivalent) for WeakHashMap?

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? `Collections.synchronizedMap(new WeakHashMap<Class, Object>());` i.e. is there something from java.util.concurrent one can use instead? Note that merely replacing with new ConcurrentHashMap<Class, Object>...

Convert collection of T into collection of QVariant in Qt

In Qt, how can I convert a typed collection of objects such as a QList<T> into a QList<QVariant>? I suppose I could construct a new list and copy the elements over, converting each to a QVariant along the way, but is there a shortcut? ...

Java equivalent of C++ std::map?

I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with o...

Searching in Java collection. Why it's so hard ?

Hi everyone, Is there a reasonable explanation, why searching an element in Java collection is so hard? For example, let's say I have: ArrayList<People> listPeople = new ArrayList<People>(); public class People { public String name; public String age; //some other code here } You got the idea ... Now, if I want to get from ...

Extending a Scala collection

I want a Map that throws on attempt to overwrite a value for existing key. I tried: trait Unoverwriteable[A, B] extends scala.collection.Map[A, B] { case class KeyAlreadyExistsException(e: String) extends Exception(e) abstract override def + [B1 >: B] (kv: (A, B1)): Unoverwriteable[A, B1] = { if (this contains(kv _1)) t...

Using for each syntax with Collections.synchronizedSet

The problem is this. I have made a set Set<User> users = Collections.synchronizedSet(new HashSet<User>()) ... for(User u : users){ //do something with u } Now, according to Java documentation. It is imperative that the user manually synchronize on the returned sorted set when iterating over it or any of its subSet, head...

Navigating a generic list of collection in c#

I have a collection of objects in a generic list. I am wondering what is the the best way to navigate this collection. I want to do operations such as "MoveNext", "MovePrevious" etc. Basically my collection is a number of steps in a flow and I want to be able to move along the steps. Is there a c# equivalent of MoveNext and MovePrev...

HashMap null keySet

Hi, I am trying to loop over an HashMap with the keySet as below: for (String key : bundle.keySet()) { String value = bundle.get(key); ... } I use a lot of for-each loops on HashMaps in other parts of my code, but this one as a weird behavior: its size is 7 (what's normal) but keySet, entrySet and values are null (according t...