collections

Duplicates in OneToMany annotated List

落爺英雄遲暮 提交于 2019-12-21 21:42:45
问题 I'm working on a Java project using JPA 2 + Hibernate 4.2.6 and I'm getting a strange behaviour. In my model I have two related entites: Question and Answer @Entity public class Question { // ... @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private Set<Answer> answers; // ... } @Entity public class Answer { // ... @ManyToOne(optional = false) @JoinColumn(name = "question_id", nullable = false) private Question question; // ... } This works perfectly:

Correctly using collections in a meteor package?

烈酒焚心 提交于 2019-12-21 20:34:16
问题 I am trying to create a package that uses a collection. In a normal application, the same code works fine without any issues. collectiontest.js (Package code) // Why do I need to set the global property manually? var globals = this || window; TestCol = new Meteor.Collection('test'); globals.TestCol = TestCol; console.log('defined'); if(Meteor.isServer){ Meteor.publish('test', function(){ return TestCol.find(); }); Meteor.startup(function(){ console.log('wtf'); TestCol.remove({}); TestCol

Partial sort Collection with limit and custom Comparator

微笑、不失礼 提交于 2019-12-21 20:17:33
问题 I want to sort an ArrayList called imageList like this: Collections.sort(imageList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); This works fine, but now I want to be able to set a limit (show only the newest 100 images, where the ArrayList is unsorted, so simply creating a sublist won't work) for performance reasons. My MapComparator class looks like this: class MapComparator implements Comparator<HashMap<String, String>> { private final String key; private final String order; public

Do I need a concurrent collection for adding elements to a list by many threads?

眉间皱痕 提交于 2019-12-21 17:48:26
问题 static final Collection<String> FILES = new ArrayList<String>(1); for (final String s : list) { new Thread(new Runnable() { public void run() { List<String> file2List = getFileAsList(s); FILES.addAll(file2List); } }).start(); } This collections gets very big, but the code works perfect. I thought I will get a concurrent modifcation exception, because the FILES list has to extend its size, but it has never happened. is this code 100% threadsafe ? The code takes a 12 seconds to load up and a

Get the N smallest [Comparable] items in a set

为君一笑 提交于 2019-12-21 17:26:24
问题 I have an unsorted Collection of objects [that are comparable], is it possible to get a sub list of the collection of the list without having to call sort? I was looking at the possibility of doing a SortedList with a limited capacity, but that didn't look like the right option. I could easily write this, but I was wondering if there was another way. I am not able to modify the existing collection's structure. 回答1: Since you don't want to call sort() , it seems like you are trying to avoid an

Java - Intersection of multiple collections using stream + lambdas

北慕城南 提交于 2019-12-21 17:18:17
问题 I have the following function for the unification of multiple collections (includes repeated elements): public static <T> List<T> unify(Collection<T>... collections) { return Arrays.stream(collections) .flatMap(Collection::stream) .collect(Collectors.toList()); } It would be nice to have a function with a similar signature for the intersection of collections (using type equality). For example: public static <T> List<T> intersect(Collection<T>... collections) { //Here is where the magic

Memory usage difference between Generic and Non-generic collections in .NET

别等时光非礼了梦想. 提交于 2019-12-21 15:00:13
问题 I read about collections in .NET nowadays. As known, there is some advantages using generic collections over non-generic : they are type-safety and there is no casting, no boxing/unboxing. That's why generic collections have a performance advantage. If we consider that non-generic collections store every member as object , then we can think that generics have also memory advantage. However, I didn't found any information about memory usage difference. Can anyone clarify about the point? 回答1:

ImmutableList does not extend List?

对着背影说爱祢 提交于 2019-12-21 12:59:44
问题 When I dig into the gs-collection source for ImmutableList , it does not extends java.util.List . However the class javadoc mentioned that All ImmutableList implementations must implement the java.util.List . Why must ask the implementation to implement java.util.List and not the ImmutableList itself to extend java.util.List ? 回答1: Why doesn't ImmutableList extend List ? ImmutableCollection doesn't extend java.util.Collection (and ImmutableList doesn't extend java.util.List ) because

ImmutableList does not extend List?

こ雲淡風輕ζ 提交于 2019-12-21 12:59:41
问题 When I dig into the gs-collection source for ImmutableList , it does not extends java.util.List . However the class javadoc mentioned that All ImmutableList implementations must implement the java.util.List . Why must ask the implementation to implement java.util.List and not the ImmutableList itself to extend java.util.List ? 回答1: Why doesn't ImmutableList extend List ? ImmutableCollection doesn't extend java.util.Collection (and ImmutableList doesn't extend java.util.List ) because

MVC/Razor model binding collections when an element is missing

谁都会走 提交于 2019-12-21 12:17:34
问题 I've got a form that contains a variable length list of textboxes, rendered using a template similar to this.. @Html.TextBox("items[" + itemIndex + "].Title", someValue) So the final rendered HTML looks something like this... <input id="items_0__Amount" type="text" value="Apple" name="items[0].Title"> <input id="items_1__Amount" type="text" value="Banana" name="items[1].Title"> <input id="items_2__Amount" type="text" value="Orange" name="items[2].Title"> On form submission this binds to my