collections

Undesired behavior of ArrayList remove() in Java [duplicate]

六月ゝ 毕业季﹏ 提交于 2020-02-03 03:54:30
问题 This question already has answers here : Properly removing an Integer from a List<Integer> (8 answers) Closed 4 years ago . I have following two scenarios: 1. int value as parameter int intNum = 2; List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.remove(intNum); System.out.println(list.size()); // output: 2 2. long value as parameter long longNum = 2; List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.remove

ConcurrentModification Exception with Map and Hashtable

邮差的信 提交于 2020-02-02 13:15:30
问题 In my application I have used a Map to store POJO objects. As per the requirement I need to iterate over the keySet of the Map and remove objects which dont need any modification. Conside the code below: public void remove(Map<String,User> removeUser){ Set<String> keySet = removeUser.keySey(); User user = null; for(String key : keySet){ user = (user) removeUser.get(key); if(!user.isActive()){ removeUser.remove(key); } } } Here in above code, I am getting ConcurrentModificationException when I

Differences between an array and any collection from the java Collection framework?

牧云@^-^@ 提交于 2020-01-30 20:25:46
问题 As the title say I am looking into the "Differences between an array and any collection from the java Collection framework". Thought it's high level enough to provide some good understanding to a few (or many) of us who know too little about this or need to think far too long to come up with a interesting answer So far, I have come up with: Collection framework classes either use array underneath or use more complex data structure. When an array is simply...an array Array does not have

“Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”

为君一笑 提交于 2020-01-30 15:25:09
问题 Good morning Stackoverflow, I have the problem that it gives me the error: Failed to create sessionFactory object.org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: nl.scalda.pasimo.model.employeemanagement.EducationTeam.coachGroups Do you know why? @OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class) @JoinColumn(name="id") private TreeSet<CoachGroup> coachGroups = new TreeSet<>(); private

“Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”

泪湿孤枕 提交于 2020-01-30 15:25:08
问题 Good morning Stackoverflow, I have the problem that it gives me the error: Failed to create sessionFactory object.org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: nl.scalda.pasimo.model.employeemanagement.EducationTeam.coachGroups Do you know why? @OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class) @JoinColumn(name="id") private TreeSet<CoachGroup> coachGroups = new TreeSet<>(); private

Collections.sort() Comparison method violates its general contract in Java [duplicate]

Deadly 提交于 2020-01-30 13:17:10
问题 This question already has answers here : “Comparison method violates its general contract!” (11 answers) Closed 2 years ago . I know that this kind of question has been asked millions of times if not billions, however I couldn't find my answer yet :) This compare() method doesn't have Long , Double , Float , ..., it only has Date , boolean , and Null checker, however it shows me that contract violation error , can any one help plz? Collections.sort(users, new Comparator<MiniUser>() {

Collections.sort() Comparison method violates its general contract in Java [duplicate]

社会主义新天地 提交于 2020-01-30 13:17:07
问题 This question already has answers here : “Comparison method violates its general contract!” (11 answers) Closed 2 years ago . I know that this kind of question has been asked millions of times if not billions, however I couldn't find my answer yet :) This compare() method doesn't have Long , Double , Float , ..., it only has Date , boolean , and Null checker, however it shows me that contract violation error , can any one help plz? Collections.sort(users, new Comparator<MiniUser>() {

My Treeset only adds 1 class object

允我心安 提交于 2020-01-30 11:52:06
问题 I'm trying to add the below book objects to a TreeSet. However, when I debug the code, it says that the set has a size of 1 and only contains the first object added (book1). When I comment out book1, book2 is the only one added etc. Why is it that the JVm only recognises one object? Code: public static void main(String[] args) { Set<Book> bookSet = new TreeSet<Book>(); Book book1 = new Book("Digital Fortress", "Dan Brown", "St. Martins Press", 1998); Book book2 = new Book("Angels and Demons",

Java Set gets duplicate entry

房东的猫 提交于 2020-01-30 09:13:33
问题 JavaDoc defines set as : A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2) To verify the same, i created a very simple program: import java.util.HashSet; public class CheckHashSet { public static void main(String[] args) { HashSet<Employee> set = new HashSet<Employee>(); set.add(new Employee(10)); set.add(new Employee(10)); System.out.println(set.size()); System.out.println(new Employee(10).equals(new Employee

Why type casting is required while using itrerator object in the while loop? [duplicate]

强颜欢笑 提交于 2020-01-30 04:07:29
问题 This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Closed 2 years ago . I am learning java right now. While writing code for traversing ArrayList using Iterator I have to use the class name before using the iterator's object with next() function. Can anybody help me with this? import java.util.*; public class arraylistwithuserdefinedclass { public static void main(String[] args) { ArrayList<UserId> details=new ArrayList<UserId>(); UserId a=