假设我的课程中有两个构造函数:
public User (List<Source1> source){
...
}
public User (List<Source2> source) {
...
}
Run Code Online (Sandbox Code Playgroud)
假设这两个构造函数都提供了有关用户的相同信息,并且是为不同用例构建用户的同等有效方法.
在Java中,由于类型擦除而无法执行此操作 - Java不会接受两个具有作为参数List <?的构造函数.>.
那么,解决这个问题的方法是什么?什么是不是矫枉过正但仍然尊重基本OO的解决方案?由于Java没有强大的泛型支持,因此必须构建一个工厂方法或其他接口似乎是错误的.
以下是我能想到的可能性:
1)接受a List<?>作为构造函数的参数,并在构造函数中解析您需要哪种逻辑,或者如果它不是任何可接受的类型则抛出异常.
2)创建一个接受List的类,构造相应的User对象,并返回它.
3)创建包装器,List<Source1>然后List<Source2>可以将其传递给User构造函数.
4)使用两个类对这个人进行子类化,其中除了构造函数之外,所有的功能都是继承的.一个的构造函数接受Source1,另一个接受Source2.
5)用一个构建器包装这个人,其中两个不同的构建器方法用于实例化两个不同的数据源.
我的问题是这些:
1)是否需要使用Java或有意的设计决策来做这个缺陷?什么是直觉?
2)在保持良好代码而不引入不必要的复杂性方面哪种解决方案最强?为什么?
这个问题是类似的:在Java中围绕类型擦除设计构造函数但没有详细说明,它只是提出了各种解决方法.
我想插入N个相同的行,除了其中一个值不同.具体来说,这就是我想要的:
insert into attribute_list (id,value,name)
values
(
select (id,'Y','is_leveled') from value_list where val >= 50
);
Run Code Online (Sandbox Code Playgroud)
因此对于每个具有val> = 50的值,我会在attribute_list中插入一行.这可以用一个insert语句完成,还是我只需要在excel中手动生成这些插入?
(注意:这是一个简化的例子,以澄清问题,因此无需攻击此特定情况的不必要)
在我看到的从文件中读取的每个Java实现中,我几乎总是看到用于逐行读取的文件读取器.我的想法是,这将是非常低效的,因为它需要每行系统调用.
我一直在做的是使用输入流并直接获取字节.在我的实验中,这明显更快.我的测试是一个1MB的文件.
//Stream method
try {
Long startTime = new Date().getTime();
InputStream is = new FileInputStream("test");
byte[] b = new byte[is.available()];
is.read(b);
String text = new String(b);
//System.out.println(text);
Long endTime = new Date().getTime();
System.out.println("Text length: " + text.length() + ", Total time: " + (endTime - startTime));
}
catch (Exception e) {
e.printStackTrace();
}
//Reader method
try {
Long startTime = new Date().getTime();
BufferedReader br = new BufferedReader(new FileReader("test"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line …Run Code Online (Sandbox Code Playgroud) 我正在按照之前小组的移交包中的说明进行操作.我对rails或mysql2了解不多.我在Windows上.
当我做:
rake db:migrate RAILS_ENV=production
Run Code Online (Sandbox Code Playgroud)
这是输出:
C:\jcccf-goslowserver-e30bf00>rake db:migrate RAILS_ENV=production
(in C:/jcccf-goslowserver-e30bf00)
rake aborted!
Unknown database 'goslow_production'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/mysql2/client.r
b:37:in `connect'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/mysql2/client.r
b:37:in `initialize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/c
onnection_adapters/mysql2_adapter.rb:14:in `new'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/c
onnection_adapters/mysql2_adapter.rb:14:in `mysql2_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:230:in `new_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:238:in `checkout_new_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:194:in `block (2 levels) in checkout'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:190:in `loop'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:190:in `block in checkout'
C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:189:in `checkout'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:96:in `connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_pool.rb:318:in `retrieve_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_specification.rb:97:in `retrieve_connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/connect
ion_adapters/abstract/connection_specification.rb:89:in `connection'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/migrati
on.rb:486:in `initialize'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/migrati
on.rb:433:in `new'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/migrati
on.rb:433:in `up'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/migrati
on.rb:415:in `migrate'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/active_record/railtie
s/databases.rake:142:in …Run Code Online (Sandbox Code Playgroud) 拿这个表词
WORD
Hello
Aardvark
Potato
Dog
Cat
Run Code Online (Sandbox Code Playgroud)
这个清单:
('Hello', 'Goodbye', 'Greetings', 'Dog')
Run Code Online (Sandbox Code Playgroud)
如何返回不在单词表中但在我的列表中的单词列表?
如果我有一个"包含所有可能的单词"的表,我可以这样做:
SELECT * from ALL_WORDS_TABLE
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
and word not in
(SELECT word from WORDS
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
);
Run Code Online (Sandbox Code Playgroud)
但是我没有这样的表.怎么办呢?
此外,构建新表不是一种选择,因为我没有这种访问级别.
我的桌子:
ID NUM VAL
1 1 Hello
1 2 Goodbye
2 2 Hey
2 4 What's up?
3 5 See you
Run Code Online (Sandbox Code Playgroud)
如果我想返回每个ID的最大数量,它真的很干净:
SELECT MAX(NUM) FROM table GROUP BY (ID)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想获取与每个ID的每个数字的最大值相关联的值,该怎么办?
为什么我不能这样做:
SELECT MAX(NUM) OVER (ORDER BY NUM) FROM table GROUP BY (ID)
Run Code Online (Sandbox Code Playgroud)
为什么这是一个错误?我希望这个选择按ID分组,而不是为每个窗口单独分区...
编辑:错误是"不是GROUP BY表达式".
我需要转发一组只有DML访问的序列.由于一段代码中的错误,在没有序列的情况下抓取了几个值,而是手动抓取,所以现在序列正在复制这些值.所以,我想将序列推到最大值,以便下次调用nextval时,它会给出一个高于最大值的值.我有大约50个序列,每个序列都需要前进几千个.
只有DML访问才有可能吗?如果是这样,我应该怎么做呢?
如果密钥不存在,我只想插入此行.如果密钥已存在,我不想覆盖该行.
我的语法是这样的:
new PutItemRequest().withTableName(myTableName).withItem(myItem).withConditionExpression(?)
Run Code Online (Sandbox Code Playgroud)
根据AWS文档,我会使用属性ATTRIBUTE_NOT_EXISTS.我也可以使用ComparisonOperator.NULL等.据我所知.
语法提示?这个withConditionExpression机制的一些解释?
因为显然每个人都讨厌子选择,我想使用连接来做到这一点.
对于一个令人难以置信的设计示例,请选择两个表,一个包含1-6的数字列表,另一个包含0-8的偶数列表.然后,我的目标是输出Nums表中的所有奇数.
Table Nums
Number
One
Two
Three
Four
Five
Six
Table Even
Number
Zero
Two
Four
Six
Eight
Run Code Online (Sandbox Code Playgroud)
如果我只想获得Nums中偶数的列表,我会...
select nums.number
FROM nums,
even,
where nums.number = even.number;
Run Code Online (Sandbox Code Playgroud)
但是,我如何使用这些表来获取Nums表中的非平均值列表?换句话说,就像...
select nums.number
from nums
where nums.number not in (select number from even);
Run Code Online (Sandbox Code Playgroud) 我很难在没有搜索同一个表至少两次以获取最大行的情况下执行此操作,然后获取该行的值.有问题的表格很大,所以这是不可接受的.
这是我的表可能是这样的:
SCORES
ID ROUND SCORE
1 1 3
1 2 6
1 3 2
2 1 10
2 2 12
3 1 6
Run Code Online (Sandbox Code Playgroud)
我需要返回每个ID在最近一轮中获得的分数.也就是说,行具有最大(圆形),但不是最大分数.
OUTPUT:
ID ROUND SCORE
1 3 2
2 2 12
3 1 6
Run Code Online (Sandbox Code Playgroud)
现在我有:
SELECT * FROM
(SELECT id, round,
CASE WHEN (MAX(round) OVER (PARTITION BY id)) = round THEN score ELSE NULL END score
FROM
SCORES
where id in (1,2,3)
) scorevals
WHERE
scorevals.round is not null;
Run Code Online (Sandbox Code Playgroud)
这是有效的,但效率很低(我必须手动过滤掉所有这些行,当我应该首先不能抓住那些行时.)
我能做些什么来获得正确的价值观?