Menu
  • HOME
  • TAGS

Convenience method to initialize mutable Set in Java [duplicate]

java,java-collections-api

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 Collections Comparator incompatable types error? Why Doesn't my Comparator return anything?

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 How to Use Hashset Add String with Period when Reading from File

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.)...

Synchronizing on an object in Java

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...

How to override hashcode and equals method to avoid adding duplicate strings in HashSet in java?

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;...

Java UnsupportedOperationException with Collection objects

java,java-collections-api

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)....

A set that contains value pairs that performs checks without accounting for the key/value order

java,java-collections-api

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.

JVM space complexity details: Singly Linked Lists vs Doubly Linked Lists

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...

Remove all objects from list that does not exist in another list

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...

Why jdk code style uses a variable assignment and read on the same line - eg. (i=2) < max

java,java-collections-api

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...

Map and Map sorting

java,java-collections-api

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()) {...

Set within a Map in Java

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...

List difference with list constructed by Arrays.asList has surprising result. Why? [duplicate]

java,java-collections-api

The type of Arrays.asList(int[]) is List<int[]>. As such, none of the elements in xs is contained in that list.

How do I get the TreeMap (or other Map if necessary) entries in sorted order?

java,java-collections-api

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()); }...

Remove keys from HashMap by passing a List of keys - Java API method or Utility for the same?

java,java-collections-api

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...

Why this exception is thrown when I try to use an iterator on this list?

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....

Stack using the Java 8 collection streaming API

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...