我写了一个有地图的类<String, Object>.我需要它来保存任意对象,但有时我需要投射一些这些对象,所以我会做类似的事情
HashMap<String, Object> map = new HashMap<String, Object>();
Object foo = map.get("bar");
if (foo instanceof HashMap) {
((HashMap<String, Integer>) foo).put("a", 5);
}
Run Code Online (Sandbox Code Playgroud)
这给出了警告
Stuff.java:10: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.HashMap<java.lang.String,java.lang.Integer>
((HashMap<String, Integer>) foo).put("a", 5);
Run Code Online (Sandbox Code Playgroud)
我怀疑它与泛型的使用有关.我可以使用@SupressWarnings("unchecked")摆脱错误,但我想知道是否有更好的方法来做到这一点.或许我得到警告的事实意味着我应该重新考虑我正在做的事情.有什么我可以做的,或者我应该只使用@SupressWarnings?
我想知道如何保存我之前创建的二叉树.有谁知道怎么做?非常感谢.
PD:这里有一个关于如何实现二叉树的链接,我正在使用这个pice od代码:http: //code.activestate.com/recipes/286239-binary-ordered-tree/
这不是有效的类型定义:
scala> type Addable = { def +(subject: Addable) }
<console>:4: error: illegal cyclic reference involving type Addable
type Addable = { def +(subject: Addable) }
Run Code Online (Sandbox Code Playgroud)
这可以用scala表达吗?
如何获取f实例方法的函数值?
class X(i : Int){
def method(y : Int) = y + i
}
val x = new X(10)
val f : (Int) => Int = ?
val r = x.method(2)
val r2 = f(2)
Run Code Online (Sandbox Code Playgroud)
调用x.method(2)和f(2)将是相同的方法调用.
在听到最新的Stack Overflow播客后,Peter Norvig的紧凑型Python拼写检查器引起了我的兴趣,所以我决定在Scala中实现它,如果我能用功能Scala成语表达它,还要看看需要多少行代码.
这是整个问题.(我们还不比较代码行.)
(两个注意事项:如果你愿意,你可以在Scala解释器中运行它.如果你需要big.txt或整个项目的副本,它就在GitHub上.)
import scala.io.Source
val alphabet = "abcdefghijklmnopqrstuvwxyz"
def train(text:String) = {
"[a-z]+".r.findAllIn(text).foldLeft(Map[String, Int]() withDefaultValue 1)
{(a, b) => a(b) = a(b) + 1}
}
val NWORDS = train(Source.fromFile("big.txt").getLines.mkString.toLowerCase)
def known(words:Set[String]) =
{Set.empty ++ (for(w <- words if NWORDS contains w) yield w)}
def edits1(word:String) = {
Set.empty ++
(for (i <- 0 until word.length) // Deletes
yield (word take i) + (word drop (i + 1))) ++
(for (i <- 0 until word.length - …Run Code Online (Sandbox Code Playgroud) 鉴于以下特点和阶级.为什么编译?这真的可以用于什么吗?
trait Container {
type A
}
trait AnotherContainer[B]{
def x(b : B) : B
}
trait Mixed extends Container with AnotherContainer[Container#A]
class Impl extends Mixed{
def x(a : Container#A) = a
}
new Impl().x _
scala> new Impl().x _
res0: (Container#A) => Container#A = <function>
Run Code Online (Sandbox Code Playgroud)
更新:
class Baz { type T; }
Run Code Online (Sandbox Code Playgroud)
实际上是一个功能,但我找不到它的动机:#1753.
有没有更好的方式来排序比写一个itemgetter替代提取嵌套元组值的嵌套元组值的列表:
def deep_get(*idx):
def g(t):
for i in idx: t = t[i]
return t
return g
>>> l = [((2,1), 1),((1,3), 1),((3,6), 1),((4,5), 2)]
>>> sorted(l, key=deep_get(0,0))
[((1, 3), 1), ((2, 1), 1), ((3, 6), 1), ((4, 5), 2)]
>>> sorted(l, key=deep_get(0,1))
[((2, 1), 1), ((1, 3), 1), ((4, 5), 2), ((3, 6), 1)]
Run Code Online (Sandbox Code Playgroud)
我想过使用compose,但这不在标准库中:
sorted(l, key=compose(itemgetter(1), itemgetter(0))
Run Code Online (Sandbox Code Playgroud)
我在libs中遗漏了哪些内容会使这段代码变得更好?
实施应该合理地使用100k项目.
上下文:我想对直方图项目的字典进行排序.键是元组(a,b),值是计数.最后,项目应按计数递减,a和b排序.另一种方法是展平元组并直接使用itemgetter,但这样会产生很多元组.
假设您要构建ImmutableSet/ List/ Map对象的副本,但过滤掉一些原始条目.实现这一目标的一种方法如下:
ImmutableList.copyOf(Iterables.filter(myObject, myObject.EQUALS));
Run Code Online (Sandbox Code Playgroud)
where myObject.EQUALS是Iterables.filter()操作的谓词.我认为这是一个非常优雅且易于阅读的实现.但是,一个构建两个列表对象(第一个通过Iterables.filter(...)调用,第二个通过ImmutableList.copyOf(...)),这是非常低效的.
有人知道更有效的方法吗?
我想最好的事情是将过滤器谓词添加到ImmutableSet/ List/ Map构建器,以便该对象必须只构造一次.但不幸的是,没有这样的参数.
这并不是非常重要,但我很好奇是否有办法编写一个Java内核可能使用guava或者某些东西来填充具有相同值的元素的数组.所以例如像Arrays.getSameElementArray(new long[12], 42L);
java ×4
scala ×4
python ×3
arrays ×1
binary-tree ×1
casting ×1
generics ×1
guava ×1
immutability ×1
markdown ×1
markup ×1
maven ×1
performance ×1
persistence ×1
sorting ×1
textile ×1
tuples ×1
types ×1