这个实例化有什么问题:
Map<String, String, HashMap<String,String>> map = new HashMap<String, String, HashMap<String,String>>();
Run Code Online (Sandbox Code Playgroud) 我写了一个mergesort算法.当我运行以下测试时:
public static void main(String[] args){
Integer[] arr = {3,7,9,11,0,-5,2,5,8,8,1};
List<Integer> list = new ArrayList<>();
list.addAll(Arrays.asList(arr)); // asList() returns fixed size list, so can't pass to mergesort()
List<Integer> result = mergesort(list);
System.out.println(result);
}
Run Code Online (Sandbox Code Playgroud)
我知道[-5, 0, 1, 2, 3, 5, 7, 8, 8, 9, 11],这是正确的.但是,我知道mergesort是一个稳定的类型,所以我怎么能编写一个测试来证明这两个8是按照它们原来的顺序?
编辑:因为我使用了Integer类,而不是原始的int,我想我可以得到hashCode()自从Integer扩展基Object类.
但是,当我尝试
Integer[] arr = {3,7,9,11,0,-5,2,5,8,8,1};
System.out.println(arr[8].hashCode());
System.out.println(arr[9].hashCode());
Run Code Online (Sandbox Code Playgroud)
我只得到:
8
8
Run Code Online (Sandbox Code Playgroud) 我有一个方法可以将String记录列表解析为对象并返回List对象.所以我的方法签名是这样的.
public List<ParsedObject> parse(String[] records);
但我也想返回,其他指标,如未成功解析的字符串记录数.现在我感到困惑,如何返回此指标.一种选择是创建另一个包装类,该类包含已解析记录列表和存储这些度量标准的成员.
但是我经常面对这种情况,这样我最终会创建许多包装类.
不确定我是否解释得很好.这里有什么建议?
我有一个包含以下界面的改造服务
public interface ApiService {
@GET("/users/me")
Observable<Account> authenticateUser(@Header("Authorization") String auth);
@GET("/membership/{userId}")
SubscriptionStatus getSubscriptionStatus(@Path("userId") String userId);
}
Run Code Online (Sandbox Code Playgroud)
我想定义一个方法来进行api调用以获取包含userId的Account,然后使用此ID进行第二次API调用以获取用户订阅状态.SubscriptionStatus包含一个布尔值,如果它是真的,我希望该方法返回Observable.
这就是我到目前为止的方式:
public Observable<Account> doLogin(ApiService service , String credentials) {
return service.authenticateuser(base64) // gets Account observable
.doOnNext(account -> {
currentAccount = account; // setting the Account Variable
})
.flatMap(account -> service.getSubscriptionStatus(account.getUserId())) // get Account Subscription status
... //unsure where to go from here I need to check
//the subscriptionStatus object and return account
//observable if condition is valid
}
Run Code Online (Sandbox Code Playgroud) 我有一个基本上是键值对的数据结构.然而,与字典不同,我可能有重复的密钥,这在我正在设计的系统中是合法的.目前我有一个实现Pair对象的Java类(很像这里的例子一个值集对的Java集合?(元组?)),它有一个左边和右边(键和值)然后我将它们存储在一个ArrayList中.
我想要的是一种以更快的方式查找键的方法,O(N)作为列表可以变得非常大.
我曾想过可能会创建一个倒排索引,但是想知道是否还有另一种方法?
为了减少重复项,我真的只想根据键获取列表中的位置列表.
不必是Java - 这正是我将要实现的内容.
干杯
大卫
我有不同类型的不同元素,例如:
<0, "none">, <0, "constructor">, <0, "none">, <0, "method">, <1, "method">, <2, "constructor">, <2, "method">, <2, "constructor">
Run Code Online (Sandbox Code Playgroud)
我想将它们存储在地图或任何其他数据结构中而不删除重复项。我将地图实现如下:
Map<Integer, String> m1 = new HashMap<>();
m1.put(0, "none");
m1.put(0, "constructor");
m1.put(1, "none");
m1.put(0, "method");
Run Code Online (Sandbox Code Playgroud)
打印的结果m1是{0=method, 1=method, 2=constructor}我不想要的。我想展示所有要素。
我能以某种方式访问列表中对象的索引吗?
myList.stream().sorted((o1, o2) -> 0).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
例如:
我希望首先显示奇数索引,最后显示索引.
我想以相同的方式洗牌两个列表。假设我有两个列表问题和答案。我想以相同的方式对它们进行混洗,以便问题答案对保持不变。