Guava's Sets includes: public static <E> HashSet<E> newHashSet(E... elements) which: Creates a mutable HashSet instance containing the given elements in unspecified order. You can call it with a single item as: Sets.newHashSet(item); ...
java,sorting,java-collections-api
The Collections.sort method sorts the List in place; it returns void, and you can't assign the result of a void-returning method to anything. Call Collections.sort without assigning the result to anything, and return cleanedList. It may be a typo in your question, but cleanedList isn't defined in the code you...
java,string,file-io,hashset,java-collections-api
Either there is a third Set<String> that is returned via getDoubleTLD-S-Set or the getter Set<String> getDoubleTLDSet(){ //getDoubleTLD-S-Set ?? return singleTLDSet; } returns the singleTLDSet. Otherwise the code is fine. (What's the point of calling StringEscapeUtils.escapeJava? I wouldn't do that just for storing the strings.)...
java,thread-synchronization,java-collections-api
Yes it is the only way. private synchronized myMethod() { // do work } is equivalent to: private myMethod() { synchronized(this) { // do work } } So if you want to synchronize on an other instance than this, you have no other choice but declaring the synchronized block inside...
java,equals,hashcode,hashset,java-collections-api
Try this @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((placeId == null) ? 0 : placeId.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false;...
From the javadoc of Arrays.asList() (emphasis mine): Returns a fixed-size list backed by the specified array In short: you cannot .add*() or .remove*() from such a List! You'll have to use another modifiable List implementation (ArrayList for instance)....
I guess you already know the answer, on how to implement it, I don't think there is any library in java which can help you out for this requirement. So yes please implement your Pair class with similar implementation example given by patryk, in the previous answer.
java,data-structures,jvm,java-collections-api
The space used should be the same on the Oracle JVM/OpenJDK for a 64-bit JVM with 32-bit references (Compressed oops) For a Node with two references header: 12 bytes two references: 8 bytes alignment padding: 4 bytes total is 24 bytes per node as all objects are aligned by 8...
java,arraylist,collections,hashmap,java-collections-api
The Collection.retainAll method exists for exactly that purpose: Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection. Usage would be: list1.retainAll(list2); It might not be any...
As already mentioned in the comment: Doug Lea, who is one of the main authors of the collections framework and the concurrency packages, tends to do optimizations that may look confusing (or even counterintuitive) for mere mortals. A "famous" example here is copying fields to local variables in order to...
IMO a priority queue can also be a good approach: Map<Integer, String> myMap1 = new HashMap<Integer, String>(); PriorityQueue<Entry<Integer, String>> pq = new PriorityQueue<Map.Entry<Integer,String>>(myMap1.size(), new Comparator<Entry<Integer, String>>() { @Override public int compare(Entry<Integer, String> arg0, Entry<Integer, String> arg1) { return arg0.getValue().compareTo(arg1.getValue()); } }); pq.addAll(myMap1.entrySet()); while (!pq.isEmpty()) {...
java,aggregation,composition,class-diagram,java-collections-api
Try something like this class Project{ int pid; int name; //Getter and setter } Create Employee class public class Employee { int employeeId; String name; Set<Project> projects; //getter , setter or add methods } Now you can use above two classes in DAO public class EmployeesDao{ /* Get multiple employee-projects...
The type of Arrays.asList(int[]) is List<int[]>. As such, none of the elements in xs is contained in that list.
get gets by key, not index position. Iterate over the entrySet or keySet. Setup: TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(); treeMap.put(3, "Alessandro"); treeMap.put(12, "Mario"); treeMap.put(1, "Marco"); Example using entrySet, in which case you get a Map.Entry<Integer, String> for each iteration: for (Map.Entry<Integer, String> entry : treeMap.entrySet()) { System.out.println(entry.getValue()); }...
You can remove the keys from the keySet : issueMap.keySet().removeAll(listOfKeysToRemove); keySet returns a Set of the keys contained in the Map, which is backed by the Map. Therefore, changes to the Map are reflected in the Set and vice-versa. Javadoc: Set keySet() Returns a Set view of the keys contained...
java,list,iterator,java-collections-api
You are advancing the iterator twice in each iteration. while (iteratorUserRoles.hasNext()) { System.out.println(iteratorUserRoles.next().getName()); //iteratorUserRoles.next(); remove this } If the iterator has one remaining element and you call iteratorUserRoles.next() twice, you'll get an exception....
java,java-8,java-stream,java-collections-api
Like in the comments already mentioned and you have tested the Deque Class should be prefered. But i will you give the reason why the stack shouldn't be used. At first the Java Doc. of the Stack says itself: A more complete and consistent set of LIFO stack operations is...