Google 正在弃用 Android 11 中的 Android AsyncTask API,并建议java.util.concurrent改用。你可以在这里查看提交
*
* @deprecated Use the standard <code>java.util.concurrent</code> or
* <a href="https://developer.android.com/topic/libraries/architecture/coroutines">
* Kotlin concurrency utilities</a> instead.
*/
@Deprecated
public abstract class AsyncTask<Params, Progress, Result> {
Run Code Online (Sandbox Code Playgroud)
如果您在 Android 中维护带有异步任务的旧代码库,则将来可能需要对其进行更改。我的问题是,使用java.util.concurrent. 它是一个 Activity 的静态内部类。我正在寻找可以使用的东西minSdkVersion 16
private static class LongRunningTask extends AsyncTask<String, Void, MyPojo> {
private static final String TAG = MyActivity.LongRunningTask.class.getSimpleName();
private WeakReference<MyActivity> activityReference;
LongRunningTask(MyActivity context) {
activityReference = new WeakReference<>(context);
}
@Override
protected MyPojo doInBackground(String... params) { …Run Code Online (Sandbox Code Playgroud) 这是我的控制器里面的方法,注释方法是 @Controller
@RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8")
@ResponseBody
public JSONObject getServerAlertFilters(@PathVariable String serverName) {
JSONObject json = new JSONObject();
List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, "");
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(filteredAlerts);
json.put(SelfServiceConstants.DATA, jsonArray);
return json;
}
Run Code Online (Sandbox Code Playgroud)
我期待着{"data":[{"useRegEx":"false","hosts":"v2v2v2"}]}作为我的json.
这是我的JUnit测试:
@Test
public final void testAlertFilterView() {
try {
MvcResult result = this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").session(session)
.accept("application/json"))
.andDo(print()).andReturn();
String content = result.getResponse().getContentAsString();
LOG.info(content);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制台输出:
MockHttpServletResponse:
Status = 406
Error message = null
Headers = {} …Run Code Online (Sandbox Code Playgroud) 我正在经历的add方法HashSet.有人提到
如果此set已包含该元素,则调用将保持set不变并返回false.
但是该add方法在内部保存了值HashMap
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
Run Code Online (Sandbox Code Playgroud)
陈述的put方法HashMap
将指定的值与此映射中的指定键相关联.如果映射先前包含键的映射,则替换旧值.
因此,如果该put方法HashMap取代了旧的值,该怎么办HashSet add法叶集重复的元素的情况下保持不变?
使用新的Twitter Bootstrap版本:Bootstrap 4 alpha,我无法使Modal在远程模式下工作.它与Bootstrap 3完美配合.使用bootstrap 4我得到了弹出窗口,但是模型体没有被加载.没有远程调用myRemoteURL.do来加载模型体.
码:
<button type="button" data-toggle="modal" data-remote="myRemoteURL.do" data-target="#myModel">Open Model</button>
<!-- Model -->
<div class="modal fade" id="myModel" tabindex="-1"
role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="modal-title" id="myModalLabel">Model Title</h3>
</div>
<div class="modal-body">
<p>
<img alt="loading" src="resources/img/ajax-loader.gif">
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我在JAVA中浏览了TreeMap的源代码.根据JAVA doc:
基于红黑树的NavigableMap实现.地图根据其键的自然顺序进行排序,或者根据使用的构造函数在地图创建时提供的比较器进行排序.
此实现为containsKey,get,put和remove操作提供有保证的log(n)时间成本.算法是Cormen,Leiserson和Rivest的算法导论中的算法的改编.
在源代码中,我发现内部类条目被用作节点.
static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left = null;
Entry<K,V> right = null;
Entry<K,V> parent;
boolean color = BLACK;
....
Run Code Online (Sandbox Code Playgroud)
至于红黑树的定义.从维基百科我发现:
红黑树是一种自平衡二叉搜索树,是计算机科学中使用的数据结构.
通过用两种颜色中的一种(这些通常称为"红色"和"黑色",因此树的名称)绘制每个节点来提供自平衡,使得得到的绘制树满足某些属性.使它变得非常不平衡.修改树时,随后重新排列新树并重新绘制以恢复着色属性.这些属性的设计使得这种重新排列和重新着色可以有效地进行.
我试图分析源代码,但无法理解以下内容:
假设我已经在树中有两个键"C"和"E",然后我添加"D".如何安排节点(使用自然排序).
如何在Java源代码中实现Tree的自我平衡.
我尝试搜索TreeMap的详细实现,但无法找到任何文章,例如我为HashMap找到的以下文章
从昨天起我就挂在这棵树上了:(有人可以帮我下楼......
我想在客户端验证日期,所以我写了下面的代码.但是我没有得到异常,而是获得了2月31日日期字符串的正确日期对象,这显然是无效日期.
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/yyyy";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (Exception e) {
// Throw invalid date message
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:2015-02-28T11:30:59
有谁知道为什么LocalDateTime解析这个日期而不是抛出异常.
在Spring中,RestTemplate我们有以下删除方法.
@Override
public void delete(String url, Object... urlVariables) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null, urlVariables);
}
@Override
public void delete(String url, Map<String, ?> urlVariables) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null, urlVariables);
}
@Override
public void delete(URI url) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null);
}
Run Code Online (Sandbox Code Playgroud)
这些方法都没有任何传递标题信息的地方.是否有任何其他方法可用于DELETE带头信息的请求?
我能够从Redis使用中检索值Jedis:
public static void main(String[] args) {
Jedis jedis = new Jedis(HOST, PORT);
jedis.connect();
Set<String> set = jedis.smembers(KEY);
for (String s : set) {
System.out.println(s);
}
jedis.disconnect();
jedis.close();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用Spring时RedisTemplate,我没有得到任何数据.我的数据存储Redis为Set.
// inject the actual template
@Autowired
private RedisTemplate<String, Object> template;
// inject the template as SetOperations
@Resource(name="redisTemplate")
private SetOperations<String,String> setOps;
public String logHome() {
Set<String> set = setOps.members(KEY);
for(String str:set){
System.out.println(str); //EMPTY
}
Set<byte[]> keys = template.getConnectionFactory().getConnection().keys("*".getBytes());
Iterator<byte[]> it = …Run Code Online (Sandbox Code Playgroud) 我正在通过PMD规则AppendCharacterWithChar.它说避免在StringBuffer.append中将字符串联为字符串.
StringBuffer sb = new StringBuffer();
// Avoid this
sb.append("a");
// use instead something like this
StringBuffer sb = new StringBuffer();
sb.append('a');
Run Code Online (Sandbox Code Playgroud)
我真的需要这个PMD规则吗?以下两段代码之间有很大的性能差异吗?
String text = new StringBuffer().append("some string").append('c').toString();
String text = new StringBuffer().append("some string").append("c").toString();
Run Code Online (Sandbox Code Playgroud)