collections

how to handle large lists of data

We have a part of an application where, say, 20% of the time it needs to read in a huge amount of data that exceeds memory limits. While we can increase memory limits, we hesitate to do so to since it requires having a high allocation when most times it's not necessary. We are considering using a customized java.util.List implementatio...

Java: Get first item from a collection

If I have a collection, such as Collection<String> strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it? ...

unique key value collection in C#

I want to build 2-dimentional collection where i need unique combination of key value pairs. For example Domain "Company" (Id: 1) can have MachineName "Machine1" and "Machine2", but cannot add another MachineName "Machine1" again. Another Domain "Corporate" (Id:2) can have another machineName "Machine1". here my collection will be like ...

How can I fire event before item is added to collection in C#?

I would like to do some processing before an item is added to a BindingList. I see there is an ListChanged event but this is fired after the item is added. The AddingNew event is only fired when the AddNew method (not the Add method) is called. Has anyone done something like this before? UPDATE: I have created the following classes ...

Removing items from a collection in java while iterating over it

I want to be able to remove multiple elements from a set while I am iterating over it. Initially I hoped that iterators were smart enough for the naive solution below to work. Set<SomeClass> set = new HashSet<SomeClass>(); fillSet(set); Iterator<SomeClass> it = set.iterator(); while (it.hasNext()) { set.removeAll(setOfElementsToRemo...

javascript function that returns two string values, how to do this neatly?

I need to store values that would look like this: "somekey": "value1", "value2" So I want to create a function that will take in "somekey", and it will return "value1", "value2". It doesn't have to be a function, it could easily be a collection that I initialize in my javascript file and then reference like: var v1 = blah("somekey")....

.NET collection that throws an exception when a duplicate is added

Is there a collection (apart from Dictionary) in the .NET framework (3.5) that throws an exception when a duplicate is added? HashSet does not throw an exception here: HashSet<string> strings = new HashSet<string>(); strings.Add("apple"); strings.Add("apple"); Whereas the Dictionary does: Dictionary<string, string> dict = new Dictio...

Arraylist in Csharp

i have created a class named employees to hold the employee information. the class looks as follows. class Employee { private int employeeID; private string firstName; private string lastName; private bool eligibleOT; private int positionID; private string positionName; private ArrayList arrPhone; private...

Do any Java libraries provide a random access Queue implementation?

I'm implementing a sliding window over a stream of events, in Java. So I want a data structure which allows me to do the following: add to the end of the data structure when new events occur; remove from the start of the data structure when old events are processed; get standard random access (size(), get(i)) to the elements of the dat...

Custom Collection extends List<T> Add method

I want to create a custom collection and add my own custom Add method. Scenario: Teacher.Students.Add(Student s) I want to put LINQ methods to save the teacher/student relationship to the database, not just add it to the list. How can I do that? How can I know what "Teacher" object this is? ...

DataGridView rows show up but no data

I am trying to bind a collection (that inherits from BindingList) to a DataGridView. The grid headers show up fine and the I get the number of rows that I expect. However, the cells are empty. Has anyone else had this problem? If so, how did you resolve it? I've talked to someone else who had the same problem but they can't remember ...

Remove even numbers from an ArrayList

I have to create a method which has an ArrayList; I need to remove even numbers from this ArrayList. I have written code for that but there is a logical error which I couldn't identify. Here is my code: static void sortList(){ List <Integer> number=new ArrayList <Integer>(); number.add(11); number.add(45); number.add(12);...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I'm not sure it's safe to delete things from a hash whilst you're iterating over the key-value pairs of the hash. I checked the RI documentation but I ha...

Simple Java Map puzzle

What is the best implementation for this general-purpose library method? public static <K, V> boolean containsEntry( Map<K, V> map, K key, V value) {} Criteria for judging this puzzle, as with most coding puzzles, are in this order: Completeness Correctness Performance Beauty Receipt of PayPal contribution EDIT: Well, since i...

Dependency Injection and Generic Collections

I'm going round in circles at the moment trying to get the pattern right for using Dependency Injection with a number of IEnumerables. I have three types of object that I want to return from my database: Projects, Batches and Tasks. I want to create a Repository that has the following form: public interface IRepository<T> { IEnume...

How might a class like .NET's ConcurrentBag<T> be implemented?

I find myself very intrigued by the existence of a ConcurrentBag<T> class in the upcoming .NET 4.0 framework: Bags are useful for storing objects when ordering doesn't matter, and unlike sets, bags support duplicates. My question is: how might this idea be implemented? Most collections I'm familiar with essentially amount to (under...

How can I create a javascript class and access an internal collection using and index/name lookup?

I currently have this: function SystemCollection () { this.systems = []; this.getSystem = function (id) { for(var s in this.systems) { if(s.num == id) return s; }; return null; }; this.add = function (system) { this.systems.push(system); }; this.count = systems.length; }; In...

Defining an extension method on IEnumerable<T> where T is specific kind of type?

I want a method to only work on types which implement the /, +, -, * operators. Is there any "clean" way to do this? ...

Sort Map<String, Object> by keys with IgnoreCase?

Well, I tested TreeMap but it doesn't take in account IgnoreCase on string comparision. I need to order lexicographically and ignoring case. Is there any other way? Thanks, that works (TreeMap (Comparator c)). However, I have another question: public final Comparator<Object> STR_IGN_CASE_COMP = new Comparator<Object>() { public in...

From arrayList to long[]

Hi. I am doing a method that needs to return a long[]. It looks like this: public long[] parseString(String input) input are strings like: 1, 3, 4 10, 30, 40, 50 Inside parseString I use a regex to get all numbers and add them to an ArrayList as I can't know how many oconcurrences it will find. At the end I create a long[] with ...