小编Dim*_*eou的帖子

在O(1)中用Java反转一个字符串?

在给定CharSequence的情况下,标准Java库中是否有任何工具在O(1)时间内产生反转?

I guess this is "easy" to implement, just wondering whether it already exists. (I suspect the reason this is not offered is because the "easy" way would actually break multi-char code-points - but in many cases we know we are not dealing with those).

Thanks

Update Heh, it's a bit amusing that most thought this "impossible", good work guys! Well, actually it is (conceptually) trivial - pseudojava follows to make it clear:

class MyReverseString extends String { //of course …
Run Code Online (Sandbox Code Playgroud)

java string

19
推荐指数
3
解决办法
8305
查看次数

如何在Scala中使用多个输入的含义?

例如,如何编写隐式应用以下内容的表达式:

implicit def intsToString(x: Int, y: Int) = "test"

val s: String = ... //?
Run Code Online (Sandbox Code Playgroud)

谢谢

scala implicit

11
推荐指数
1
解决办法
1396
查看次数

带有HashMultiset的Multimap用于值

我正在尝试为每个键创建一个(基于散列的)Multimap,其中包含一个(基于散列的)Multiset值.看例子:

Multimap<Object, Object> mmap = Multimaps.newMultimap(
    Maps.<Object, Collection<Object>>newHashMap(), 
    new Supplier<Collection<Object>>() {
  public Collection<Object> get() {
    return HashMultiset.create();
  }
});
mmap.put("1", "2");
Run Code Online (Sandbox Code Playgroud)

但是之后,

System.out.println(mmap.get("1") instanceof Multiset<?>); 
//false, the returned collection is not a HashMultiset,
//but a (private) WrappedCollection
Run Code Online (Sandbox Code Playgroud)

所以我似乎无法访问我创建的多重集?我希望能够将其作为Multiset(包含在Multisets.unmodifiableMultiset()中)返回.我不希望每次都将它复制到新的Multiset中.除了切换回Map<K, Multiset<V>>代码并添加我Multimap想要消除的复杂性之外,我还有其他选择吗?

java collections multimap guava

9
推荐指数
1
解决办法
2468
查看次数

无锁队列算法,重复读取保持一致性

我正在研究Michael 和 Scott 的无锁(en-,de-)队列算法。问题是我无法解释/理解(除了代码本身的注释之外,论文也无法解释)几行。

入队:

  enqueue(Q: pointer to queue_t, value: data type)
   E1:   node = new_node()        // Allocate a new node from the free list
   E2:   node->value = value      // Copy enqueued value into node
   E3:   node->next.ptr = NULL    // Set next pointer of node to NULL
   E4:   loop                     // Keep trying until Enqueue is done
   E5:      tail = Q->Tail        // Read Tail.ptr and Tail.count together
   E6:      next = tail.ptr->next // Read next ptr and count fields together …
Run Code Online (Sandbox Code Playgroud)

concurrency nonblocking data-structures

3
推荐指数
1
解决办法
943
查看次数