Tag: 集合

基于对象属性的排序

以下是Employee bean类。 public class Employee { public String name; public int age; public Employee() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 我有其他EmployeeTest类,在其中我创建了Employee类的对象并存储在ArrayList中。 import java.util.ArrayList; public class EmployeeTest […]

Java:为什么需要包装类?

在非常高的层次上,我知道我们需要通过使用它们各自的包装类来“包装”原始数据类型,例如int和char,以便在Java集合中使用它们。我想了解Java集合在Java集合中是如何工作的。低级别问:“为什么我们需要将原始数据类型包装为能够在集合中使用它们的对象?”我提前感谢您的帮助。

Arrays.asList给出UnsupportedOperationException

无法使用add或remove等方法修改Arrays.asList返回的List 。 但是如果将它传递给Collections.sort方法,它可以对数组进行排序而没有任何问题(我预计会有exception)。 这似乎是一种非常不一致的行为。 那么什么是List上允许的操作,由asList方法返回? List list = Arrays.asList(5,7, 10 , 8,9); list.remove(2);//Exception Collections.sort(list);//Ok, No Exception Sort… System.out.println(list); 我在文档中找不到任何线索。 编辑:是的我可以理解为什么它不支持remove或add 。 但那么它如何支持排序呢?

比较方法违反了其一般合同的例外

下面是一个代码块,导致exception,如图所示, 代码: Collections.sort( arrayList, new Comparator() { public int compare( Object o1, Object o2 ) { TypeAdapterSort tas1 = ( TypeAdapterSort ) o1; TypeAdapterSort tas2 = ( TypeAdapterSort ) o2; if ( tas1.order < tas2.order ) return -1; else return 1; } } ); 例外: java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:747) at […]

地图数据结构地图

MultiValueMap类(Apache commons集合)使得使用值为Collections的Map变得容易。 我正在寻找一个类,它可以很容易地使用Map,其键是对象,值是Maps。 我正在使用Java 1.4,因此无法使用Google Collections或generics。

循环遍历和arraylist并删除指定索引处的元素

我正在尝试练习,我会向一个arraylist添加1000个元素,然后再次从列表中系统地删除它们(通过指定索引)。 这背后的想法是比较LinkedList和ArrayList的性能。 int totalObjects = 0; for(int i = 0; i < 1000; i++) { totalObjects += 1; al.add("Object " + totalObjects); } System.out.println("The Arraylist size is " + al.size()); 如果我执行以下操作,只有一半的元素被删除……为什么会这样? for(int index = 0; index < al.size(); index++) { al.remove(index); } System.out.println("The Arraylist size after removal is " + al.size()); 亲切的问候阿里安

如何使用Collections和Comparator按升序对ArrayList进行排序

如何使用Comparator按升序对ArrayList进行排序? 我知道如何使用以下降顺序对其进行排序: Comparator mycomparator = Collections.reverseOrder(); 然后 Collections.sort(myarrayList,mycomparator); 只是想知道如何使用集合和比较器按升序对其进行排序? 谢谢!

Java:迭代列表时出现ConcurrentModificationException

当我执行以下代码时,我得到ConcurrentModificationException Collection myCollection = Collections.synchronizedList(new ArrayList(10)); myCollection.add(“123”); myCollection.add(“456”); myCollection.add(“789”); for (Iterator it = myCollection.iterator(); it.hasNext();) { String myObject = (String)it.next(); System.out.println(myObject); myCollection.remove(myObject); //it.remove(); } 为什么我得到exception,即使我使用Collections.synchronizedList? 当我将myCollection更改为 ConcurrentLinkedQueue myCollection = new ConcurrentLinkedQueue(); 我没有得到那个例外。 java.util.concurrent中的ConcurrentLinkedQueue与Collections.synchronizedList有什么不同?

如何将arrayList的元素传递给variadic函数

我有一个填充了元素的arrayList。 我想将该数组列表的元素作为参数传递给可变参数函数。 我的function public SequenceEntityModifier(final IEntityModifier… pEntityModifiers) 我的ArrayList ArrayList arr = new ArrayList(); arr.add(new MoveXModifier(1, 50, 120)); arr.add(new MoveXModifier(1, 120, 50)); 我想将它传递给函数,好像我会单独传递它们一样。 new SequenceEntityModifier( /* elements of arr here */ ); 这样的事情可能吗? 提前致谢。

为什么使用Collections.sort的程序仅对32或更大的列表失败?

以下程序抛出以下exception: java.lang.IllegalArgumentException: Comparison method violates its general contract! 我理解Comparator的问题。 请参阅无法复制:“比较方法违反了其总合同!” 我不明白为什么它只对32或更大的List s失败。 谁有人解释一下? class Experiment { private static final class MyInteger { private final Integer num; MyInteger(Integer num) { this.num = num; } } private static final Comparator COMPARATOR = (r1, r2) -> { if (r1.num == null || r2.num == null) return 0; return […]