如果您还想获得结果总数(在分页之前),那么在SQL Server 2000,2005,2008,2012中分页结果的最佳方法(性能明智)是什么?
Java 8最有用的功能之一是default
接口上的新方法.基本上有两个原因(可能还有其他原因)为什么会被引入:
Iterator.remove()
Iterable.forEach()
从API设计者的角度来看,我希望能够在接口方法上使用其他修饰符,例如final
.在添加便捷方法时,这将非常有用,可防止在实现类时出现"意外"覆盖:
interface Sender {
// Convenience method to send an empty message
default final void send() {
send(null);
}
// Implementations should only implement this method
void send(String message);
}
Run Code Online (Sandbox Code Playgroud)
如果Sender
是一个类,上面已经是常见的做法:
abstract class Sender {
// Convenience method to send an empty message
final void send() {
send(null);
}
// Implementations should only implement this method
abstract void send(String message);
}
Run Code Online (Sandbox Code Playgroud)
现在,default
并final
有明显矛盾的关键字,但默认关键字本身不会一直严格要求 …
在Java 8中,我可以轻松地写:
interface Interface1 {
default void method1() {
synchronized (this) {
// Something
}
}
static void method2() {
synchronized (Interface1.class) {
// Something
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将获得完全同步语义,我也可以在类中使用.但是,我不能synchronized
在方法声明上使用修饰符:
interface Interface2 {
default synchronized void method1() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
static synchronized void method2() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我们可以认为,这两个接口的行为方式相同,只是Interface2
建立了一个合同上的method1()
和method2()
,这是比强一点Interface1
呢.当然,我们也可能会争辩说default
实现不应该对具体实现状态做出任何假设,或者这样的关键字根本不会减轻它的重量.
JSR-335专家组决定不支持synchronized
接口方法的原因是什么?
我需要找出客户的排名.在这里,我为我的要求添加了相应的ANSI标准SQL查询.请帮我转换为MySQL.
SELECT RANK() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender],
FirstName,
Age,
Gender
FROM Person
Run Code Online (Sandbox Code Playgroud)
有没有找到MySQL排名的函数?
Java枚举很棒.仿制药也是如此.当然,由于类型擦除,我们都知道后者的局限性.但有一点我不明白,为什么我不能创建这样的枚举:
public enum MyEnum<T> {
LITERAL1<String>,
LITERAL2<Integer>,
LITERAL3<Object>;
}
Run Code Online (Sandbox Code Playgroud)
这个泛型类型参数<T>
反过来可以在各个地方使用.想象一下方法的泛型类型参数:
public <T> T getValue(MyEnum<T> param);
Run Code Online (Sandbox Code Playgroud)
甚至在枚举类中:
public T convert(Object o);
Run Code Online (Sandbox Code Playgroud)
由于上面的例子对某些人来说可能看起来过于抽象,所以这里有一个更真实的例子,说明我为什么要这样做.在这个例子中我想使用
public interface MyProperties {
public <T> void put(MyEnum<T> key, T value);
public <T> T get(MyEnum<T> key);
}
Run Code Online (Sandbox Code Playgroud)
我有一个数据类型的枚举:
public interface DataType<T> {}
public enum SQLDataType<T> implements DataType<T> {
TINYINT<Byte>,
SMALLINT<Short>,
INT<Integer>,
BIGINT<Long>,
CLOB<String>,
VARCHAR<String>,
...
}
Run Code Online (Sandbox Code Playgroud)
每个枚举文字显然都有基于泛型类型的附加属性<T>
,同时又是枚举(不可变,单例,可枚举等).
没有人想到这个吗?这是与编译器相关的限制吗?考虑到事实,关键字" 枚举 "是作为语法糖实现的,表示生成的代码到JVM,我不明白这个限制.
谁能向我解释一下?在你回答之前,考虑一下:
String string = LITERAL1.convert(myObject); Integer …
我应该使用LINQ Skip()
和Take()
方法进行分页,还是使用SQL查询实现自己的分页?
哪个最有效?为什么我会选择一个而不是另一个?
我正在使用SQL Server 2008,ASP.NET MVC和LINQ.
我从一个在Varchar中有原始feed的表中导入数据,我需要将varchar中的一列导入到一个字符串列中.我尝试使用<column_name>::integer
以及to_number(<column_name>,'9999999')
但是我收到错误,因为有一些空字段,我需要将它们检索为空或null到新表中.
如果有相同的功能,请告诉我.
我已经多次看到以下语法定义了create/alter DDL语句中的列:
ALTER TABLE tbl ADD COLUMN col VARCHAR(20) NOT NULL DEFAULT "MyDefault"
Run Code Online (Sandbox Code Playgroud)
问题是:由于指定了默认值,是否还需要指定列不应该接受NULL?换句话说,DEFAULT不会呈现NOT NULL冗余吗?
前段时间,我发表了一篇关于递归计算斐波纳契数的Java 8函数式方法的博文,其中包含一个ConcurrentHashMap
缓存和新的有用computeIfAbsent()
方法:
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Test {
static Map<Integer, Integer> cache = new ConcurrentHashMap<>();
public static void main(String[] args) {
System.out.println(
"f(" + 8 + ") = " + fibonacci(8));
}
static int fibonacci(int i) {
if (i == 0)
return i;
if (i == 1)
return 1;
return cache.computeIfAbsent(i, (key) -> {
System.out.println(
"Slow calculation of " + key);
return fibonacci(i - 2) + fibonacci(i - 1);
});
}
} …
Run Code Online (Sandbox Code Playgroud) 我有以下使用本地类的 Java代码。
import java.util.Arrays;
public class X<T> {
void m() {
class Z {}
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}
Run Code Online (Sandbox Code Playgroud)
它不会与以下错误消息一起编译:
X.java:8: error: illegal generic type for instanceof
if (o instanceof Z) {}
^
1 error
Run Code Online (Sandbox Code Playgroud)
我了解到,本地类Z
继承了的通用类型签名X<T>
,即内部类。在此示例中,出现了相同类型的编译错误,该错误Z
不是本地的,但仍然是内部的:
import java.util.Arrays;
public class X<T> {
class Z {}
void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compilation error
} …
Run Code Online (Sandbox Code Playgroud) java ×5
sql ×4
java-8 ×3
generics ×2
jsr335 ×2
pagination ×2
sql-server ×2
asp.net-mvc ×1
casting ×1
ddl ×1
enums ×1
linq-to-sql ×1
local-class ×1
mysql ×1
notnull ×1
performance ×1
postgresql ×1
rank ×1
recursion ×1
synchronized ×1