collections

Problem with underscore(_) in Collections.binarySearch (Java)

Hi all. Problem: I am using Java Tutorials™ sourcecode for this. This is the source code. I tried this: --following with another section of sorted words-- words.add("count"); words.add("cvs"); words.add("dce"); words.add("depth"); --following with another section of sorted words-- and it works perfectly. However when I use this: ...

List method creation

I'm creating a list of my defined objects like so List<clock> cclocks = new List<clocks>(); for each object in the list i'm calling a method moveTime, like so foreach(clock c in cclocks) { c.moveTime(); } is the a way i can write some cleaver thing so i can call cclocks.moveTime(); it would then go though the list doing tha...

Get Item from Collection by unique ID

I have a collection of Contacts that inherits from CollectionBase: public class ContactCollection : CollectionBase{ //... } each contact in the collection has a unique ID: public class Contact{ public int ContactID{ get; private set; } //... } I think what I would like to do is something like the fol...

Java: select from collection only elements of provided type

Hi! I have a collection of elements of type B and C, which all extends A. I need to filter the collection to get elements of only B type. Is there any way to do it except: for (A a : initCollection) { if (a instanceof B) { newCollection.add(a)? } } Thanks ...

performance of linq extension method ElementAt

Hi The MSDN library entry to Enumerable.ElementAt(TSource) Method says "If the type of source implements IList, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element." Let's say we have following example: ICollection<int> col = new List<int>...

Is it wise for me to use this LINQ statement for grabbing a subset of items based on a dynamic subset?

I am working on an ASPX page that needs to handle multiple different kinds of data. I came up with a potentially ideal fashion to fetch the information I need, but am unsure if it is as good an idea as it feels. Basically, I need to filter a set into a subset, but which values I filter by will differ by circumstance. I constructed the fo...

joining / merging two arrays

I have two arrays like this, actually this is mysql data retrieved from two different servers: $array1 = array ( 0 => array ( 'id' => 1, 'name' => 'somename') , 1 => array ( 'id' => 2, 'name' => 'somename2') ); $array2 = array ( 0 => array ( 'thdl_id' => 1, 'otherdate' => 'spmethi...

Inheritance and type parameters of Traversable

I'm studying the source code of the Scala 2.8 collection classes. I have questions about the hierarchy of scala.collection.Traversable. Look at the following declarations: package scala.collection trait Traversable[+A] extends TraversableLike[A, Traversable[A]] with GenericTraversableTemplate[A, Traversable] tr...

Quickly retrieve the subset of properties used in a huge collection in C#

I have a huge Collection (which I can cast as an enumerable using OfType<>()) of objects. Each of these objects has a Category property, which is drawn from a list somewhere else in the application. This Collection can reach sizes of hundreds of items, but it is possible that only, say, 6/30 of the possible Categories are actually used. ...

object reference set in java

I need to create a Set of objects. The concern is I do not want to base the hashing or the equality on the objects' hashCode and equals implementation. Instead, I want the hash code and equality to be based only on each object's reference identity (i.e.: the value of the reference pointer). I'm not sure how to do this in Java. The reas...

How do I write test code to exercise a C# generic Pair<TKey, TValue> ?

Hi, I am reading through Jon Skeet's "C# in Depth", first edition (which is a great book). I'm in section 3.3.3, page 84, "Implementing Generics". Generics always confuse me, so I wrote some code to exercise the sample. The code provided is: using System; using System.Collections.Generic; public sealed class Pair<TFirst, TSecond> ...

Different cache concurrent strategies for root entity and its collection (Hibernate with EHCache)?

Given example from Hibernate docs and modifying it so that root level entity (Customer) is read-only while one of its collections (tickets) is read-write: @Entity @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class Customer { ... @OneToMany(...) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public Sor...

Why can't I get properties from members of this collection?

I've added some form controls to a collection and can retrieve their properties when I refer to the members by index. However, when I try to use any properties by referencing members of the collection I see a 'Could not set the ControlSource property. Member not found.' error in the Locals window. Here is a simplified version of the c...

Whats the best to way convert a set of Java objects to another set of objects?

Basic Java question here from a real newbie. I have a set of Java objects (of class "MyClass") that implement a certain interface (Interface "MyIfc"). I have a set of these objects stored in a private variable in my class that is declared as follows: protected Set<MyClass> stuff = new HashSet<MyClass>(); I need to provide a public m...

Java: Why subList(0, 5).clear() doesn't work on my objects?

If I run this operation on List<Integer> for example, it works as expected (removes first 5 elements), but when I run it on a list of my objects, nothing happens (list stays the same). list.subList(0, 5).clear(); My class is a pojo that doesn't implement equals or hashCode, if that matters. UPDATE: The implementation I am using is Ar...

scala implicit or explicit conversion from iterator to iterable

Does Scala provide a built-in class, utility, syntax, or other mechanism for converting (by wrapping) an Iterator with an Iterable? For example, I have an Iterator[Foo] and I need an Iterable[Foo], so currently I am: val foo1: Iterator[Foo] = .... val foo2: Iterable[Foo] = new Iterable[Foo] { def elements = foo1 } This seems ug...

Are there any C# collections where modification does not invalidate iterators?

Are there any data structures in the C# Collections library where modification of the structure does not invalidate iterators? Consider the following: List<int> myList = new List<int>(); myList.Add( 1 ); myList.Add( 2 ); List<int>.Enumerator myIter = myList.GetEnumerator(); myIter.MoveNext(); // myIter.Current == 1 myList.Add( 3 ); m...

The best way to assign an immutable instance to a Collection in Java

Today I was reading through some Hibernate code and I encounter something interesting. There is a class called CollectionHelper that defines the following constant varibale: public final class CollectionHelper { public static final List EMPTY_LIST = Collections.unmodifiableList( new ArrayList(0 ) ; public static final Collection EMP...

How to cast a list of inheriting objects to a collection of objects in Java ?

I've a collection type: Collection<A> collecA And I've a list in my object: List<B> listB Where B is extending A class B extends A { ... } But I can't do the following: collecA = listB I can't understand why since Collection is implemented by List. ...

Hibernate HQL and Grails- How do I compare collections?

Hi everyone (my first post!), I have an HQL question (in Groovy/Grails) I was hoping someone could help me with. I have a simple Asset object with a one-to-many Tags collection. class Asset { Set tags static hasMany = [tags:Tag] } class Tag { String name } What I'm trying to do in HQL: A user passes in some tags in params...