collections

Why can we change the unmodifiable list if we have the original one?

爷,独闯天下 提交于 2019-12-17 19:49:29
问题 By looking at the code of Collections class, i got to know that when we are using the method unmodifiableList(List list) or unmodifiableCollection(Collection c) it is not creating a new object but it is returning the reference of the same object and overriding the methods which can modify the List [ add , addall , remove , retainAll ... ] So i ran this test: List modifiableList = new ArrayList(); modifiableList.add ( 1 ); List unmodifiableList = Collections.unmodifiableList( modifiableList );

How does Collections.binarySearch work?

谁说我不能喝 提交于 2019-12-17 19:24:32
问题 I am trying to understand how Collections.binarySearch work in Java. I don't quite understand the output I get. public static void main(String args[]) { // create arraylist ArrayList<String> arlst=new ArrayList<String> (); arlst.add("A"); arlst.add("D"); arlst.add("C"); arlst.add("B"); arlst.add("E"); int index=Collections.binarySearch(arlst, "D", Collections.reverseOrder()); System.out.println(index); } } The output of this code is -1. And when the elements have been inserted at this order

How to compare two Dictionaries in C#

被刻印的时光 ゝ 提交于 2019-12-17 19:13:11
问题 I have two Generic Dictionaries.Both have same keys.But values can be different.I want to compare 2nd dictionary with 1st dictionary .If there are differences between values i want to store those values in separate dictionary. 1st Dictionary ------------ key Value Barcode 1234566666 Price 20.00 2nd Dictionary -------------- key Value Barcode 1234566666 Price 40.00 3rd Dictionary -------------- key Value Price 40 Can Anyone give me a best algorithm to do this.I wrote an algorithm but it have

How do I get an IntStream from a List<Integer>?

点点圈 提交于 2019-12-17 18:44:56
问题 I can think of two ways: public static IntStream foo(List<Integer> list) { return list.stream().mapToInt(Integer::valueOf); } public static IntStream bar(List<Integer> list) { return list.stream().mapToInt(x -> x); } What is the idiomatic way? Maybe there is already a library function that does exactly what I want? 回答1: I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); }

Comparing two hashmaps for equal values and same key sets?

会有一股神秘感。 提交于 2019-12-17 18:25:48
问题 How can I best compare two HashMap s, if I want to find out if none of them contains different keys than the other, and if the values of that keys match each other. Map<objA, objB> mapA = new HashMap<objA, objB>(); mapA.put("A", "1"); mapA.put("B", "2"); Map<objA, objB> mapB = new HashMap<objA, objB>(); mapB.put("D", "4"); mapB.put("A", "1"); When comparing A with B, it should fail due to different keys B and D. How could I best compare non-sorted hashmaps? 回答1: Make an equals check on the

C# Collection was modified; enumeration operation may not execute [duplicate]

两盒软妹~` 提交于 2019-12-17 18:25:29
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Collection was modified; enumeration operation may not execute HI there, I am create an project estimation program and am getting the following error: C# Collection was modified; enumeration operation may not execute. It is related to using this: I initally declare the diciontary globally with this: Dictionary<int, int> rankings = new Dictionary<int, int>(); The Next Method containing this dictionary does the

How to use java.Set

你说的曾经没有我的故事 提交于 2019-12-17 18:08:56
问题 I'm trying to make it working for quite some time,but just can't seem to get it. I have object Tower built of Block's. I've already made it working using arrays, but I wanted to learn Set's. I'd like to get similar functionality to this: public class Tower { public Tower(){ } public Tower add(Block k1){ //(...) //if block already in tower, return "Block already in tower" } public Tower delete(Block k1){ //(...) //if block already dleted, show "No such block in tower" } } Someone gave me some

Why does Hashtable not take null key?

☆樱花仙子☆ 提交于 2019-12-17 17:46:28
问题 Why does Hashtable not take a null key? Also why does HashMap allow null keys? What is the purpose of making these two classes Key behaviour so different? 回答1: From the Hashtable JavaDoc: To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. In a nutshell, since null isn't an object, you can't call .equals() or .hashCode() on it, so the Hashtable can't compute a hash to use it as a key. HashMap is newer,

UnmodifiableMap (Java Collections) vs ImmutableMap (Google) [duplicate]

筅森魡賤 提交于 2019-12-17 17:28:34
问题 This question already has answers here : Immutable vs Unmodifiable collection (7 answers) Closed 5 years ago . Context I need to return a reference to a map that I'm using for a data cache, and I'd like to make sure nobody can modify their reference. Question I've seen lots of references to UnmodifiableMap and ImmutableMap online, but I don't see anything comparing/contrasting them. I figure there is a good reason that Google/Guava created their own version - can someone tell me what it is?

Converting String array to java.util.List

一世执手 提交于 2019-12-17 17:25:48
问题 How do I convert a String array to a java.util.List ? 回答1: List<String> strings = Arrays.asList(new String[]{"one", "two", "three"}); This is a list view of the array, the list is partly unmodifiable, you can't add or delete elements. But the time complexity is O(1). If you want a modifiable a List: List<String> strings = new ArrayList<String>(Arrays.asList(new String[]{"one", "two", "three"})); This will copy all elements from the source array into a new list (complexity: O(n)) 回答2: Use the