collections

The **equals** in Set interface.

As we know two Set instances are equal iff they contain the same elements, BUT is it possible to have the same element in two different sets (Set interface can not contain duplicate element)? ...

What is the exact difference between these two groups of statements?

Set<Type> union = new HashSet<Type>(s1); And Set<Type> union = new HashSet<Type>(); Set<Type> s1 = new HashSet<Type>(); union.addAll(s1); ...

What does this statement mean?

I am a beginner in Java and do not understand what if(!s.add(a)) means in this code excerpt: Set<String> s = new HashSet<String>(); for(String a:args) { if(!s.add(a)) System.out.println("Duplicate detected:"+a); } ...

Counting the number of occurrences of each item in a list

I have a streaming input which has repeated values. I can use any data structure but I have to count the number of occurence of each element. Suppose I have a list of mobile phone suppliers like the following: Apple Nokia Samsung Apple LG Nokia HTC Android Apple Nokia Nokia Apple Samsung I have to build any data structure preferably ...

scala Iterable#map vs. Iterable#flatMap

What is the difference between the map and flatMap functions of Iterable? ...

Interface/Superclass for Collections/Containers in c++

I'm coming from the Java world and are building a small c++ program at the moment. I have an object that does some work and then returns the result of the work as a list. Now a day later i changed the behavior of the object to save the results in a set to avoid duplicates in the container. But I can't simply return the set because I us...

When to use inverse=false on NHibernate / Hibernate OneToMany relationships?

I have been trying to get to grips with Hibernate's inverse attribute, and it seems to be just one of those things that is conceptually difficult. The gist that I get is that when you have a parent entity (e.g. Parent) that has a collection of Child objects using a one-to-many mapping, setting inverse=true on the mapping tells Hibernat...

Most efficient way to select a specific object type in Dictionary and delete it.

Okay I have a series of objects based on a base class which are stored randomly in a Dictionary object. e.g. class TypeA { public int SomeNumber {get; set;} public void SomeFunction() } class TypeB : TypeA { public string SomeString {get; set;} } class TypeC : TypeA { public bool StuffReady() } Dictionary listOfClasses <...

Is there any difference between the **copy** and ** addAll**??

1)Is there any difference between these two keywords for the elements of collections??(Copy those elements to the other collection and addAll those elements to the other collection) ...

Is there any difference between these two groups of statements.

Set<Type> union = new HashSet<Type>(s1); union.addAll(s2); AND Set <Type> union = new HashSet<Type>(); union.addAll(s1); union.addAll(s2); ...

Inheriting and encapsulating collection classes in Java

Suppose I have the following types of data: class Customer { String id; // unique OtherCustData someOtherData; } class Service { String url; // unique OtherServiceData someOtherData; } class LastConnection { Date date; OtherConnData someOtherData; // like request or response } Now I need to remember when each of the cust...

STXXL equivalent in Java

I'm searching a collection framework designed for huge datasets in Java that behaves transparently, like STXXL does for C++. It should transparently swap to disk, but in a much more efficient manner than plain OS-based VM swapping. A StringBuffer/String drop-in replacement would be a big plus. ...

Updating a Map

I have a map like this: private Map<String, List<List<String>>> someMap; private List<List<String>> someList1; private List<String> someList2; ....Some initialization..... ....Some list population.... Then I have, if(someMap.get(someKey) == null){ someList1.add(someList2); someMap.put(someKey, someList1); } else { som...

What is the most used class in Google collections framework?

I wonder what is the most useful class in the Google collections framework? ...

How do I use JPQL to delete entries from a join table?

I have a JPA object which has a many-to-many relationship like this: @Entity public class Role { //... @ManyToMany(fetch=FetchType.EAGER) @JoinTable( name="RolePrivilege", joinColumns= @JoinColumn(name="role", referencedColumnName="ID"), inverseJoinColumns= @JoinColumn(name="privilege", referencedColumnName="...

How to implement List, Set, and Map in null free design?

Its great when you can return a null/empty object in most cases to avoid nulls, but what about Collection like objects? In Java, Map returns null if key in get(key) is not found in the map. The best way I can think of to avoid nulls in this situation is to return an Entry<T> object, which is either the EmptyEntry<T>, or contains the va...

Java Crit-bit Trees

Is there a built-in data structure in Java to represent a Crit-bit tree? Or any libraries available that might provide this functionality? I would also accept brief code as an answer, if one can be implemented in a simplistic brief way. ...

Contract of ICollection<T>.IsReadOnly

I'm writing an array wrapper class that implements IList<T>. I’m unsure of what to return for IList<T>.IsReadOnly (inherited from ICollection<T>), though. My class disallows insertion and removal. It does allow modifying items via the this[int].set property. The MSDN states that A collection that is read-only does not allow the add...

How to convert int[] into List<Integer> in Java?

How do I convert int[] into List<Integer> in Java? Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java. ...

Construct a java.util.List from a java.util.Set in Scala

I would like to create a java List based on another java Collection eg. Set in Scala. Why is this not possible? I get a required: scala.this.Int error. val in: java.util.Set[String] = new java.util.HashSet() val out : java.util.List[String] = new java.util.ArrayList(in) This worked however, but doesn't feel right: val in: java.util....