假设我有一个Map<String, String>,我想删除所有值包含的条目foo.在优化/内存等方面,最好的方法是什么?syso下面的四个是打印相同的结果,也就是说{n2=bar}.
public static void main(String[] args) {
Map<String, String> in = new HashMap<String, String>();
in.put("n1", "foo");
in.put("n2", "bar");
in.put("n3", "foobar");
// 1- create a new object with the returned Map
Map<String, String> in1 = new HashMap<String, String>(in);
Map<String, String> out1 = methodThatReturns(in1);
System.out.println(out1);
// 2- overwrite the initial Map with the returned one
Map<String, String> in2 = new HashMap<String, String>(in);
in2 = methodThatReturns(in2);
System.out.println(in2);
// 3- use the clear/putAll methods
Map<String, …Run Code Online (Sandbox Code Playgroud) 我们的想法是编写一个脚本,通过调度程序每隔x分钟调用一次(例如CRON任务).这是保持MySQL和Lucene同步的一种方法.直到我还管理的内容:
这是我要求你帮助管理的一点:
这是我使用的代码,它试图索引MySQL表tag (id [PK] | name):
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), config);
String query = "SELECT id, name FROM tag";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query);
while (result.next()) {
Document document = new Document();
document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("name", result.getString("name"), Field.Store.NO, Field.Index.ANALYZED)); …Run Code Online (Sandbox Code Playgroud) OutputStream fos;
OutputStream bos;
OutputStream zos;
try {
fos = new FileOutputStream(anyFile);
bos = new BufferedOutputStream(fos);
zos = new ZipOutputStream(bos);
} finally {
if (zos != null) {
zos.close(); // + exception handling
}
}
Run Code Online (Sandbox Code Playgroud)
是否关闭zos自动关闭bos和fos过,或者我需要手动关闭它们?
我有一个list(<ul />),我尝试显示为网格.该电池具有一个固定的宽度(假设100像素):然后cols和rows的数量取决于屏幕分辨率.
当屏幕宽度很大时,有很多列但很少行:
______________________________________________________________
| ___________ ___________ ___________ ___________ |
| | | | | | | | | |
| | #1 | | #2 | | #3 | | #4 | |
| |<- 100px ->| |<- 100px ->| |<- 100px ->| |<- 100px ->| |
| | | | | | | | | |
| |___________| |___________| |___________| |___________| |
| ___________ |
| | | |
| | #5 | | …Run Code Online (Sandbox Code Playgroud) new String[] { "foo", "bar" }.clone();
Run Code Online (Sandbox Code Playgroud)
使用我最喜欢的IDE(即Eclipse),我想clone()通过按住Ctrl键点击它(像往常一样)来查看上述方法的源代码,但它将我带到了Object原生的一个,它只提供签名,而不是方法的主体.
自动完成告诉我,所述clone()方法属于Stringclass(clone() : String[] - String),但该类的源代码String不提供这样的方法(因为我正在处理String[]类...).
那么,该实现隐藏在哪里?应该修复自动完成吗?
使这个片段线程安全的最佳方法是什么?
private static final Map<A, B> MAP = new HashMap<A, B>();
public static B putIfNeededAndGet(A key) {
B value = MAP.get(key);
if (value == null) {
value = buildB(...);
MAP.put(key, value);
}
return value;
}
private static B buildB(...) {
// business, can be quite long
}
Run Code Online (Sandbox Code Playgroud)
以下是我能想到的几个解决方案:
ConcurrentHashMap,但如果我很好理解,它只是使原子put和get操作线程安全,即它不能确保对buildB()给定值只调用一次方法.Collections.synchronizedMap(new HashMap<A, B>()),但我会遇到与第一点相同的问题.putIfNeededAndGet()方法synchronized,但我可以有很多线程一起访问这个方法,所以它可能非常昂贵.我可以提供哪些其他解决方案?
我知道这是一个很常见的网络主题,但我还没有找到一个清晰,完整和有效的例子.
我有一个初始颜色:(#3F92DF蓝色)。
我可以减少其不透明度,这导致了淡蓝色(如果在白色背景)rgba(63, 146, 223, 0.5)。
这种颜色在白色背景上不透明,如果我选择它,我会得到:#A5CAEF。
问题是:如何在 JS 中#A5CAEF从#3F92DF(1)中找到(3 )?即找到颜色好像它在白色背景上有 0.5 不透明度?
我尝试使用初始颜色的 RGB 值(通过将它们增加到 255),但我无法获得预期的颜色(我得到的颜色更像是绿松石)。
var initialColor = "#3F92DF";
var colorWithOpacity = "rgba(63, 146, 223, 0.5)";
var colorWithoutOpacity = "#A5CAEF";
document.getElementById("d1").style.backgroundColor = initialColor;
document.getElementById("d2").style.backgroundColor = colorWithOpacity;
document.getElementById("d3").style.backgroundColor = colorWithoutOpacity;Run Code Online (Sandbox Code Playgroud)
div {
width: 100px;
height: 100px;
border: solid 1px white;
float: left;
}Run Code Online (Sandbox Code Playgroud)
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>Run Code Online (Sandbox Code Playgroud)
该DOC约java.util.Set.contains(Object o)说:
当且仅当此set包含元素e时返回true(o == null?e == null:o.equals(e)).
也就是说,这是一个POJO(你可以看到,我覆盖了它的equals方法):
public class MonthAndDay {
private int month;
private int day;
public MonthAndDay(int month, int day) {
this.month = month;
this.day = day;
}
@Override
public boolean equals(Object obj) {
MonthAndDay monthAndDay = (MonthAndDay) obj;
return monthAndDay.month == month && monthAndDay.day == day;
}
}
Run Code Online (Sandbox Code Playgroud)
那么,为什么以下代码打印false而不是true?
Set<MonthAndDay> set = new HashSet<MonthAndDay>();
set.add(new MonthAndDay(5, 1));
System.out.println(set.contains(new MonthAndDay(5, 1)));
// prints false
Run Code Online (Sandbox Code Playgroud)
一个解决方案是重写contains(Object o) …
在我的控制器中:
$scope.homeAction = function() {
console.log("HomeAction");
};
Run Code Online (Sandbox Code Playgroud)
在我看来:
<button ng-click="homeAction()">call homeAction()</button>
Run Code Online (Sandbox Code Playgroud)
单击按钮时,Chrome和Firefox会按预期执行该方法,但IE会执行两次.知道为什么吗?
这是一个再现问题的plunker:http://plnkr.co/edit/pedZKjIVGDAYfMl0ZphJ .
javascript internet-explorer angularjs angularjs-ng-click ionic-framework