This question already has answers here:
How to make a CaseInsensitiveConcurrentMap?
(2 answers)
Closed 2 years ago.
So basically I have treeMap and I want to find keys in it the thing is that searching for ABC or Abc or aBc or abC or ABc or AbCor aBC it should return true in the containsKey after using some comparator i think.
The thing is that i already tried to covert the String all into lower case and upper case but sometimes i need the key to be like aBC because i need to print the key and printing abc and ABC or ABc are different things.
So do you know another way to do this?
Use toLower() when adding to the map, and also when searching. But, you'll have to add special handling if you want to be able to store distinct values for keys that differ only in upper/lower case.
If you need to keep the original case you'll have to modify the value object to store it.
You might also want to subclass TreeMap and override the put and get methods to take care of the toLower() calls. Remember to override ALL methods that get or put values.
Related
This question already has answers here:
Map implementation with duplicate keys
(20 answers)
Closed 1 year ago.
So what would be elegant way to store key -- value if both are non unique, i need fifo but with option to access pair that was added as Nth. As far as I know maps won't do. I tought about using tab/list of two lists that would be key -- value determined by index but it doesn't sound "clean".
That sounds not like a map entry but more like Pair<A, B> in which case, you need that as a custom class since there's no tuple/pair class in the JRE. The JRE Stack class will give you fifo that you can also treat as a List
This question already has answers here:
Java 8 lambdas, Function.identity() or t->t
(3 answers)
Closed 3 years ago.
Have been through some code and ran into Function.identity() which I found it is similar to s->s. I do not understand why and when I should use Function.identity().
I have tried to understand by working on an example, but it did not clarify my questions:
public static void main(String[] args){
Arrays.asList("a", "b", "c")
.stream()
.map(Function.identity())
//.map(str -> str) //it is the same as identity()
.forEach(System.out::println);
return;
}
When printing the list elements with and without mapping, I am getting the same result:
a
b
c
So, what is the purpose of including s-> s which is passing a string and retrieving an string? what is the purpose of Function.identity()?
Please provide me with a better example, maybe this example does not make sense to prove the importance of using identity().
Thanks
It's useful when the API forces you to pass a function, but all you want is to keep the original value.
For example, let's say you have a Stream<Country>, and you want to make turn it into a Map<String, Country>. You would use
stream.collect(Collectors.toMap(Country::getIsoCode, Function.identity()))
Collectors.toMap() expects a function to turn the Country into a key of the map, and another function to turn it into a value of the map. Since you want to store the country itself as value, you use the identity function.
This question already has answers here:
How to remove duplicates from a list?
(15 answers)
Closed 8 years ago.
I have a problem at the moment on my project. I have 1 arraylist (Lets call it BIG) that is formed by another 3 arraylists (A,B,C) from a SQL query each, so I have basically duplicated items (sometimes even same Item 3 or more times) in the BIG arraylist.
My problem is that I cannot use the .contains() method since the references of the objects are different between them (even though some objects represent the same "item"), and as "primary key", the attribute that never changes inside each item in arraylist is the ID of the items (an int). How can I do to delete duplicated elemets so I can get with only one of each "items"?
You need to override Equals method of that class, as per the business requirement.
And ofcourse hashcode method, because if equals is overridden hashcode method also should be overridden.
Explained well here :
How to remove duplicates from a list?
In equals check id attributes of both objects.
This question already has answers here:
What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?
(3 answers)
Closed 8 years ago.
I have the following scenario, where I want to test someFunction():
Collection<MyObject> objects = someFunction(someInput);
assertThat(objects , contains(hasProperty("property", is(propertyIWantToTest))));
This works fine if Collection<MyObject> objects should have just 1 MyObject object according to someInput which is passed to someFunction().
However, there are some cases for someInput that the Collection<MyObject> objects should have 2 or more MyObject object containg the same propertyIWantToTest object.
Is there a way to use Hamcrest matchers to test that?
Here's something closer to what I'm willing to achieve:
assertThat(objects , contains(exactlyTwoTimes(hasProperty("property", is(propertyIWantToTest)))));
If you want to verify that every item has that property, and that there are exactly two items, then use everyItem and hasSize:
assertThat(objects, everyItem(hasProperty("property", is(propertyIWantToTest))));
assertThat(objects, hasSize(2));
If you want to specifically test the contents of the collection, but it just so happens that both expected items are the same, use a variable and containsInAnyOrder:
Matcher<MyObject> m = hasProperty("property", is(propertyIWantToTest));
assertThat(objects, containsInAnyOrder(m, m));
This question already has answers here:
how does weakhashmap work? [duplicate]
(2 answers)
Closed 9 years ago.
I have read up quite a bit on these and am still rather confused on one aspect.
HashMaps take in a K,V pair. Why is this necessary?
For example I want to add "abracadabra" to HashMap myMap.
Would it not use String.hashCode() function as the key, and then "abracadabra" as the value?
And then if I were trying to lookup if "abracadabra" is there it would check if the 'bucket' for that hashCode is nonempty, and if it is then iterate through everything in that 'bucket' (At worst O(n)...but not in reality). So what I am saying is wouldn't the objects .hashCode() function be the key and the object is the hashcode? Why is an explicit Key necessary to be declared?
What is the purpose of having K,V pair? I have had this explained to me multiple times and have read multiple articles/examples/etc. and I still can't get it through my thick skull.
A hashmap is a mapping from key (in your case abracadabra) to an object. This is useful if you get the key from somewhere else, e.g. an id identifying an user and you need to load additional data for that user.
What you described sounds more like a HashSet
You are looking a the wrong object: HashMaps are not ment to store a single object (e.g. the string "abracadabra"), they are indeed ment to store key-value pairs, where both parts are of importance - an easy example would be a property store: The property name is the key, the property value the value.
If you want to really store just one object, look at other structures. HashSet comes to mind.
If I am getting you right, you want the functionality of a HashSet being accomplished by a HashMap. Have a look on the HashSet documentation, probably that is what you are searching for.
HashMaps work different, to give you a hint: you can store the same string (with equal hashCode) with different keys:
String myString = "hallo";
HashMap<String,String> map = new HashMap<String,String>();
map.put("key1", myString);
map.put("key2", myString);
A hashCode by itself is not enough to look up a value: different keys can have the same hashCode. The point of having a hashCode is only to quickly narrow down the places where the hash table would have the entry for that key and that value.