我想ArrayList使用布尔类型对我进行排序.基本上我想首先显示条目true.这是我的代码如下:
Abc.java
public class Abc {
int id;
bool isClickable;
Abc(int i, boolean isCl){
this.id = i;
this.isClickable = iCl;
}
}
Run Code Online (Sandbox Code Playgroud)
Main.java
List<Abc> abc = new ArrayList<Abc>();
//add entries here
//now sort them
Collections.sort(abc, new Comparator<Abc>(){
@Override
public int compare(Abc abc1, Abc abc2){
boolean b1 = abc1.isClickable;
boolean b2 = abc2.isClickable;
if (b1 == !b2){
return 1;
}
if (!b1 == b2){
return -1;
}
return 0;
}
});
Run Code Online (Sandbox Code Playgroud)
排序前的顺序:true true true false false false …
我有一个方法,每次执行它时都会生成一个对象,我需要颠倒我获取它们的顺序.所以我认为这样做的自然方式就是Stack,因为它是LIFO.
但是,Java Stack似乎不能很好地与新的Java 8流API一起使用.
如果我这样做:
Stack<String> stack = new Stack<String>();
stack.push("A");
stack.push("B");
stack.push("C");
List<String> list = stack.stream().collect(Collectors.toList());
System.out.println("Collected: " + list);
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Collected: [A, B, C]
Run Code Online (Sandbox Code Playgroud)
为什么不以预期的LIFO顺序将它们输出到流中?这是将所有项目从堆栈清除到右(LIFO)顺序列表的正确方法吗?
有没有一种有效的方法来查找Java中多个枚举之间的所有可能组合?
考虑以下三个枚举 -
public enum EnumOne {
One ("One"),
OneMore ("OneMore");
}
public enum EnumTwo {
Two ("Two"),
}
public enum EnumThree {
Three ("Three"),
ThreeMore ("ThreeMore");
}
Run Code Online (Sandbox Code Playgroud)
我希望输出产生这些多个枚举之间的所有可能组合,即
{EnumOne.One, EnumTwo.Two, EnumThree.Three},
{EnumOne.One, EnumTwo.Two, EnumThree.ThreeMore},
{EnumOne.OneMore, EnumTwo.Two, EnumThree.Three},
{EnumOne.OneMore, EnumTwo.Two, EnumThree.ThreeMore}
Run Code Online (Sandbox Code Playgroud)
希望找到一种有效的方法来处理它.
谢谢
我有两个清单
List<Map<String,Object>> list1 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>();
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("1", new Long(1L));
HashMap<String, Object> hm2 = new HashMap<String, Object>();
hm2.put("2", new Long(2L));
HashMap<String, Object> hm3 = new HashMap<String, Object>();
hm3.put("3", new Long(3L));
HashMap<String, Object> hm4 = new HashMap<String, Object>();
hm4.put("4", new Long(4L));
list1.add(hm);
list1.add(hm2);
list1.add(hm3);
list1.add(hm4);
HashMap<String, Object> hm1 = new HashMap<String, Object>();
hm1.put("1", new Long(1L));
HashMap<String, Object> hm5 = new HashMap<String, Object>();
hm5.put("2", new Long(2L));
list2.add(hm1);
list2.add(hm5);
Run Code Online (Sandbox Code Playgroud)
我想从list1中删除另一个list2中不存在的所有对象.
我的预期输出是list1 …
我正在寻找类似于这种语法的东西,即使它不存在.
我希望有一个方法作用于集合,并且在方法的生命周期中,确保集合不会被搞乱.
这可能看起来像:
private void synchronized(collectionX) doSomethingWithCollectionX() {
// do something with collection x here, method acquires and releases lock on
// collectionX automatically before and after the method is called
}
Run Code Online (Sandbox Code Playgroud)
但相反,我担心这样做的唯一方法是:
private void doSomethingWithTheCollectionX(List<?> collectionX) {
synchronized(collectionX) {
// do something with collection x here
}
}
Run Code Online (Sandbox Code Playgroud)
这是最好的方法吗?
是否有一种方便的方法来初始化Set等价于Collections.singleton,它返回一个可变的Set而不是一个不可变的?
许多建议使用CollectionUtils.isNotEmpty(coll)而不是coll != null在下面的用例也。
if (CollectionUtils.isNotEmpty(coll)) {
for (String str : coll) {
}
}
Run Code Online (Sandbox Code Playgroud)
代替
if (coll != null) {
for (String str : coll) {
}
}
Run Code Online (Sandbox Code Playgroud)
有什么理由/优势来CollectionUtils.isNotEmpty(coll)代替这里吗?谢谢。
我注意到在jdk源代码中,更具体地说,在集合框架中,在表达式中读取变量之前,首先要分配变量.这只是一个简单的偏好还是一些我不知道的更重要的东西?我能想到的一个原因是该变量仅在此表达式中使用.
由于我不熟悉这种风格,我觉得很难读懂.代码非常简洁.下面你可以看到一个例子java.util.HashMap.getNode()
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 && ...) {
...
}
Run Code Online (Sandbox Code Playgroud) 我有点惊讶,以下示例抛出NullPointerException:
fun main(args: Array<String>) {
val hm = HashMap<String, Int>()
hm.put("alice", 42)
val x = hm.get("bob")
println(x) // BOOM
}
Run Code Online (Sandbox Code Playgroud)
我以为NullPointerExceptionKotlin代码中没有s?
如果我使用x可选类型进行注释,则编程将打印null:
fun main(args: Array<String>) {
val hm = HashMap<String, Int>()
hm.put("alice", 42)
val x: Int? = hm.get("bob")
println(x) // null
}
Run Code Online (Sandbox Code Playgroud)
我是否发现了一个特例,或者这是Kotlin/Java互操作的一般问题?
我正在使用带有Kotlin 0.11.91.1插件的IntelliJ IDEA 14.1社区版.
null nullpointerexception java-interop kotlin java-collections-api
contains()方法的api 说
“如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e使得(o == null?e == null:o.equals(e))时,返回true。
我覆盖了equals()我班上的方法,contains()但当我检查时仍返回false
我的代码
class Animal implements Comparable<Animal>{
int legs;
Animal(int legs){this.legs=legs;}
public int compareTo(Animal otherAnimal){
return this.legs-otherAnimal.legs;
}
public String toString(){return this.getClass().getName();}
public boolean equals(Animal otherAnimal){
return (this.legs==otherAnimal.legs) &&
(this.getClass().getName().equals(otherAnimal.getClass().getName()));
}
public int hashCode(){
byte[] byteVal = this.getClass().getName().getBytes();
int sum=0;
for(int i=0, n=byteVal.length; i<n ; i++)
sum+=byteVal[i];
sum+=this.legs;
return sum;
}
}
class Spider extends Animal{
Spider(int legs){super(legs);}
}
class Dog extends Animal{
Dog(int legs){super(legs);}
}
class Man extends …Run Code Online (Sandbox Code Playgroud) 我有这种类型的集合:
List<com.liferay.portal.model.Role> ruoli
ruoli = currentUser.getRoles();
Run Code Online (Sandbox Code Playgroud)
包含3个元素(我使用调试器看到它),每个Role对象都有一个名称字段,并且previos列表中的3个对象具有以下名称:Administrator,Power User和User.
所以我试图使用迭代器迭代这个列表并打印包含在ruoli列表中的这3个对象的name字段的值,所以我这样做:
Iterator<Role> iteratorUserRoles = ruoli.iterator();
while (iteratorUserRoles.hasNext()) {
System.out.println(iteratorUserRoles.next().getName());
iteratorUserRoles.next();
}
Run Code Online (Sandbox Code Playgroud)
问题是它没有显示出来
但在堆栈跟踪中我得到:
**Administrator**
**Power User**
**User**
Run Code Online (Sandbox Code Playgroud)
正如我所料,但我得到了这样的信息:
**Administrator**
**User**
Run Code Online (Sandbox Code Playgroud)
然后它抛出此异常:
2015-01-26 10:20:13,071 [[ACTIVE] ExecuteThread:'1'表示队列:'weblogic.kernel.Default(self-tuning)'] ERROR commons.CommonsLogger.error(38) - 无法执行动作java .util.NoSuchElementException
似乎在尝试执行此操作时抛出异常:
iteratorUserRoles.next();
Run Code Online (Sandbox Code Playgroud)
为什么?可能是什么问题呢?我在做什么?如何解决此问题并正确迭代所有对象到我的列表中?
TNX
在下面的代码片段运行后,您期望"zs"的值是多少?
Collection<Integer> xs = Arrays.asList(1,2,3);
int[] ys = {1};
List<Integer> zs = new ArrayList<>(xs);
zs.removeAll(Arrays.asList(ys));
Run Code Online (Sandbox Code Playgroud)
我原本期望一个包含2和3的列表.但是,在Eclipse 4.5 M7中使用JDK 1.8.0_25它是一个包含1,2,3的列表.删除无效.但是,当我将"ys"指定为非原始数组时,我得到了预期的结果:
Collection<Integer> xs = Arrays.asList(1,2,3);
Integer[] ys = {1};
List<Integer> zs = new ArrayList<>(xs);
zs.removeAll(Arrays.asList(ys));
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
String[] arr = { "Java", "Champ", "." };
List<String> list = (List<String>) Arrays.asList(arr); // line 1
arr[2] = ".com"; // line 2
for (String word : list) {
System.out.print(word);
}
Run Code Online (Sandbox Code Playgroud)
JavaChamp.com即使它在第1行中转换为List并在第2行中修改,结果如何输出
java ×12
collections ×3
arraylist ×2
java-7 ×2
hashmap ×1
iterator ×1
java-8 ×1
java-interop ×1
java-stream ×1
kotlin ×1
list ×1
null ×1
sorting ×1