collections

Is it possible to apply a function to a collection using Java Guava?

非 Y 不嫁゛ 提交于 2019-12-22 04:55:07
问题 I want to apply a function to a collection, map, etc, using Guava. Basically, I need to resize the rows and columns of a Table separately so all rows and columns are of equal size, doing something like this: Table<Integer, Integer, Cell> table = HashBasedTable.create(); Maps.transformValues(table.columnMap(), new ResizeFunction(BlockDimension.WIDTH)); Maps.transformValues(table.rowMap(), new ResizeFunction(BlockDimension.HEIGHT)); public interface Cell { int getSize(BlockDimension dimension);

Best approach to use in Java 6 for a List being accessed concurrently

对着背影说爱祢 提交于 2019-12-22 04:53:19
问题 I have a List object being accessed by multiple threads. There is mostly one thread, and in some conditions two threads, that updates the list. There are one to five threads that can read from this list, depending on the number of user requests being processed. The list is not a queue of tasks to perform, it is a list of domain objects that are being retrieved and updated concurrently. Now there are several ways to make the access to this list thread-safe: -use synchronized block -use normal

What's the best way to return something like a collection of `std::auto_ptr`s in C++03?

℡╲_俬逩灬. 提交于 2019-12-22 04:48:13
问题 std::auto_ptr is not allowed to be stored in an STL container, such as std::vector . However, occasionally there are cases where I need to return a collection of polymorphic objects, and therefore I can't return a vector of objects (due to the slicing problem). I can use std::tr1::shared_ptr and stick those in the vector , but then I have to pay a high price of maintaining separate reference counts, and object that owns the actual memory (the container) no longer logically "owns" the objects

Scala conditional list construction

你离开我真会死。 提交于 2019-12-22 04:47:05
问题 I'm using Scala 2.9.2, and would like to construct a list based on some conditions. Consider the following, where cond is some function taking a predicate p and a value of type T (in this case t3): t1 :: t2 :: cond( p, t3 ) :: t4 The behaviour I want is as follows. If p is true, this should give: List[T]( t1, t2, t3, t4 ) If p evaluates to false, this should give: List[T]( t1, t2, t4 ) I'm probably thinking about this completely the wrong way, but I'm struggling to come up with an elegant

WPF DataGrid: Binding a Collection Property to Column

蹲街弑〆低调 提交于 2019-12-22 04:25:14
问题 I have a Class called Person: public class Person { private string _Name; private ObservableCollection<Smartphone> _Smartphones; // Properties } public class Smartphone { private string _Manufacturer; private bool _IsWorking; // Properties } And in my View I have a DataGrid. My Question is: Is there a way to make my DataGrid look like this: All Persons have the same Smartphones in their Collection, but with different values vor "IsWorking"... EDIT: I've tried it with a DataGrid in a DataGrid

Make your collections thread-safe?

那年仲夏 提交于 2019-12-22 04:18:19
问题 When designing a collection class, is there any reason not to implement locking privately to make it thread safe? Or should I leave that responsibility up to the consumer of the collection? 回答1: is there any reason not to implement locking privately to make it thread safe? It depends. Is your goal to write a collection class which is accessed by multiple threads? If so, make it thread safe. If not, don't waste your time. This kind of thing is what people refer to when they talk about

How to replace a value conditionally in a Collection, such as replaceIf(Predicate<T>)?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 04:09:42
问题 Is there any easy way we could replace a value in a List or Collection if the value is null? We can always do list.stream().filter(Objects::nonNull); and maybe add 0 back to the list. But what I am looking for is an API like list.replaceIf(Predicate<>) . 回答1: This will only work on a List , not on a Collection , as the latter has no notion of replacing or setting an element. But given a List , it's pretty easy to do what you want using the List.replaceAll() method: List<String> list = Arrays

Type-safe, generic, empty Collections with static generics

大兔子大兔子 提交于 2019-12-22 04:04:43
问题 I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections: return Collections.EMPTY_LIST; return Collections.emptyList(); where emptyList() is supposed to be type-safe. But I recently discovered: return Collections.<ComplexObject> emptyList(); return Collections.<ComplexObject> singletonList(new ComplexObject()); etc. I see this method in Eclipse Package Explorer: <clinit> () : void but I don't see how this is done in the

Output of Iterable.sliding as Tuple

前提是你 提交于 2019-12-22 03:48:38
问题 The method sliding on collections returns a sliding window of given size in the form of X[Iterable[A]] with X being the type of the collection and A the element type. Often I need two or three elements and I prefer to have them named. One ugly workaround for sliding(2) is the following: points.sliding(2).foreach{ twoPoints => val (p1,p2) = (twoPoints.head,twoPoints.last) //do something } This sucks and only works for two elements. Also note that (a,b) = (twoPoints(0),twoPoints(1)) doesn't

Maps (collection) that maintains insertion Order in java

☆樱花仙子☆ 提交于 2019-12-22 03:48:07
问题 I need to use Maps in Java for an Android Application. But the problem is that the list gets sorted automatically. How do I use Maps to get the data in the same order as I have inserted data. 回答1: You should use LinkedHashMap for this purpose..Visit Android Docs and Java Docs for more details. 回答2: The LinkedHashMap maintains insertion order. 回答3: A LinkedHashMap will keep the data in the same order as it has been inserted. 回答4: As you and I have discovered, LinkedHashMap isn't very helpful.