I want to sort Set of type Map, I have already sorted Map recursively, but when it comes to sort Set of Map and I am not able to sort it. I am not able to do so.
I have tried Override Comparator in TreeSet, but that didn't work.
I am getting below error for TreeSet<Object> sortedSet = new TreeSet(value); line :
TreeSet Cannot sort Set
it gives error message -
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1290)
at java.util.TreeMap.put(TreeMap.java:538)
at java.util.TreeSet.add(TreeSet.java:255)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at java.util.TreeSet.addAll(TreeSet.java:312)
at java.util.TreeSet.<init>(TreeSet.java:160)
value is Set( Map (String, Object) ), I want to sort and create a JSON object of it.
public String getSortCode(Object value) {
Set<Object> sortedSet = sortSet((Set<Object>) value);
}
private Set<Object> sortSet(Set<Object> value) {
TreeSet<Object> sortedSet = new TreeSet(value);
sortedSet.forEach(entry -> {
if (entry instanceof Map) {
Map<String, Object> sortedSubMap = sortMap((Map) entry);
sortedSet.add((Object) sortedSubMap);
} else if (entry instanceof Set) {
Set<Object> sortedSubSet = sortSet((Set<Object>) entry);
sortedSet.add((Object) sortedSubSet);
}
});
return (Set<Object>) sortedSet;
}
Thanks in Advance !!
Sushobhit
Related
Looking for solution how to match objects from list with data from map with condition if object field starts with map values and save to another map with Groovy
i have map with some data
Map<String, String> dataMap = new HashMap()
dataMap.put("d1", "DATA1")
dataMap.put("d2", "DATA2")
dataMap.put("d3", "DATA3")
and list of DataElement objects
List<DataElement> elements = new ArrayList()
elements.add(new DataElement("TEXT1"))
elements.add(new DataElement("TEXT2"))
elements.add(new DataElement("DATA1_text1"))
elements.add(new DataElement("DATA2_text2"))
class DataElement {
public field;
public DataElement(String text){
this.field = text
}
public getField(){
return this.field
}
And i'am trying to get new Map where keys are values from first map and values are objects(field) from List with condition if object field starts with map value: Result should be:
[d1=DATA1_text1, d2=DATA2_text2]
My code is working but may be there is more elegant variant with using collectEntries:
list = new HashMap()
mapping = dataMap.each { key, v ->
elements.each { el ->
if (el.getField().startsWith(v)) {
list.put(key, el)
}
}
}
dataMap.collectEntries{k,v->
[k,elements.find{e-> e.getField().startsWith(v)} ]
}.findAll{k,v-> v} //to keep only non empty values
I have to use a combobox to associate a list of values (key, Value). Key is the value to be stored in the database, Value is the description to be displayed in the combobox.
For example:
Key / value
C1 Code one
C2 Choice two
C3 Choice three
...
Retrieving, for example, the selected value 'Choice two' I would like to receive the code C2.
For storing the elements in items I defined the ComboVal class.
Defining my combobox, I am stuck on the definition of the setConverter function.
The compiler gives me the following error:
Error: (1093, 49) java: constructor FormatStringConverter in class javafx.util.converter.FormatStringConverter cannot be applied to given types; required: java.text.Format; found: no arguments
reason: actual and formal argument lists differ in length
Code:
class ComboVal {
String[] dato = {null, null};
ComboVal (String Key, String Value)
{
setValue(Key, Value);
}
ComboVal ()
{
setValue(null, null);
}
String getValue ()
{
return dato[1];
}
String getKey ()
{
return dato[0];
}
void setValue (String Key, String Value)
{
dato[0] = Key;
dato[1] = Value;
}
}
classe myclass {
....
/*
Parameter ctrl is a List containing information for dynamic creation of combobox
*/
void mothod (List ctrl)
{
VBox box = new VBox();
box.getChildren().add(new Label(ctrl. label));
ObservableList items = FXCollections.observableArrayList();
ComboBox<ComboVal> cb = new ComboBox<>();
cb.setId(ctrl.name);
cb.setItems(items);
//----->>> compiler report error in the next row <<<<<<<<
cb.setConverter(new FormatStringConverter<ComboVal>() {
#Override
public String toString (ComboVal object)
{
return (object.getValue());
}
#Override
public ComboVal fromString (String string)
{
return null;
}
});
ctrl.options.forEach((k, v) -> {items.add(new ComboVal(k, v));});
cb.setCellFactory(new Callback<ListView<ComboVal>, ListCell<ComboVal>>() {
#Override
public ListCell<ComboVal> call (ListView<ComboVal> p)
{
return new ListCell<ComboVal>() {
#Override
protected void updateItem (ComboVal item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty)
{
setGraphic(null);
}
else
{
setGraphic(new Text(item.getValue()));
}
}
};
}});
box.getChildren().add(cb);
}
The FormatStringConverter class needs to be constructed with a Format parameter. You however constructed it with no parameters.
Supply a Format, like this for example:
Format format = new MessageFormat("Bla bla");
cb.setConverter(new FormatStringConverter<ComboVal>(format);
The FormatStringConverter defines its own toString and fromString methods already, and will use the given format to parse and display the values. I doubt this is what you want as this is pretty limited.
So I think you'd be better of just using a regular StringConverter and provide the custom implementations for toString and fromString.
I need generic way to filter IQueryable data and filters are populated as dictionary. I have already created method like this.
public static IEnumerable<T> CustomApplyFilter<T>(this IQueryable<T> source, Dictionary<string, string> filterBy)
{
foreach (var key in filterBy.Keys)
{
source.Where(m => m.GetType().GetProperty(key).GetValue(m, null).Equals(filterBy[key]));
}
return source.ToList();
}
But its always returning same result.
please find the caller
Dictionary<string, string> dtFilter = new Dictionary<string, string>();
dtFilter.Add("Id", "2");
var res = context.Set<MyEntity>().CustomApplyFilter<MyEntity>(dtFilter);
The Where extension method does not change the content of the IQueryable it is applied to. The return value of the method should be used:
public static IEnumerable<T> CustomApplyFilter<T>(this IQueryable<T> source, Dictionary<string, string> filterBy)
{
foreach (var key in filterBy.Keys)
{
source = source.Where(m => m.GetType().GetProperty(key).GetValue(m, null).Equals(filterBy[key]));
}
return source.ToList();
}
UPDATE:
I should have noticed it, my answer so far was applicable to LINQ to Objects only. When using LINQ to Entities, however, there are certain restrictions; only expression that can be converted to an SQL query can be used. Getting properties through reflection is not such an expression obviously.
When this is the case, one possible solution would be to build the ExpressionTree manually.
public static IEnumerable<T> CustomApplyFilter<T>(this IQueryable<T> source, Dictionary<string, string> filterBy)
{
foreach (var key in filterBy.Keys)
{
var paramExpr = Expression.Parameter(typeof(T), key);
var keyPropExpr = Expression.Property(paramExpr, key);
var eqExpr = Expression.Equal(keyPropExpr, Expression.Constant(filterBy[key]));
var condExpr = Expression.Lambda<Func<T, bool>>(eqExpr, paramExpr);
source = source.Where(condExpr);
}
return source.ToList();
}
UPDATE2:
With the comment #Venkatesh Kumar given below, it is apparent that when the underlying type of the field provided is not of type string, this solution fails (with the error message : The binary operator Equal is not defined for the types 'System.Int64' and 'System.String').
One possible way to tackle this problem would be to have a dictionary of types and delegates to use for each such property.
Since this is a static method (an extension method which has to be static), declaring a static Dictionary in class scope would be reasonable:
Let's assume the name of the class in which CustomApplyFilter is declared is SOFExtensions:
internal static class SOFExtensions
{
private static Dictionary<Type, Func<string, object>> lookup = new Dictionary<Type, Func<string, object>>();
static SOFExtensions()
{
lookup.Add(typeof(string), x => { return x; });
lookup.Add(typeof(long), x => { return long.Parse(x); });
lookup.Add(typeof(int), x => { return int.Parse(x); });
lookup.Add(typeof(double), x => { return double.Parse(x); });
}
public static IEnumerable<T> CustomApplyFilter<T>(this IQueryable<T> source, Dictionary<string, string> filterBy)
{
foreach (var key in filterBy.Keys)
{
var paramExpr = Expression.Parameter(typeof(T), key);
var keyPropExpr = Expression.Property(paramExpr, key);
if (!lookup.ContainsKey(keyPropExpr.Type))
throw new Exception("Unknown type : " + keyPropExpr.Type.ToString());
var typeDelegate = lookup[keyPropExpr.Type];
var constantExp = typeDelegate(filterBy[key]);
var eqExpr = Expression.Equal(keyPropExpr, Expression.Constant(constantExp));
var condExpr = Expression.Lambda<Func<T, bool>>(eqExpr, paramExpr);
source = source.Where(condExpr);
}
return source.ToList();
}
}
Other types and proper delegates for them should be added to the lookup Dictionary as required.
I have tried several ways
e.g.
1.create a custom JsonSerializer, and override serialize method
#Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString("");
}
and set the JsonSerializer to NullValueSerializer,like this:
objectMapper.getSerializerProvider().setNullValueSerializer(new NullStringSerializer());
but we can not get the Class Type from null. All null will be covert to "" , include the object.
2.if use SimpleModule
SimpleModule simpleModule = new SimpleModule("StringModule", Version.unknownVersion());
simpleModule.addSerializer(Object.class, new NullStringSerializer());
objectMapper.registerModule(simpleModule);
in the serialize method, the param value do not have any properties which is null.
from the resouce code of jackson 2.6.0 , find method serializeFields in MapSerializer.java (my object is a map) line: 545
for (Map.Entry<?,?> entry : value.entrySet()) {
Object valueElem = entry.getValue();
// First, serialize key
Object keyElem = entry.getKey();
if (keyElem == null) {
provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
} else {
// One twist: is entry ignorable? If so, skip
if (ignored != null && ignored.contains(keyElem)) continue;
keySerializer.serialize(keyElem, gen, provider);
}
// And then value
if (valueElem == null) {
provider.defaultSerializeNull(gen);
} else {...}
}
when the valueElem is null, the provide just covert it to null.
and do not have any interface for me to change the strategy.
I can override MapSerializer ,but I do not know how to set the new MapSerializer to the factory.
Is there any solution?
expect your help,thank you!
find a solution :
the abstract class SerializerProvider has a method named findNullValueSerializer, which is called to get the serializer to use for serializing null values for specified property.
We can override SerializerProvider#findNullValueSerializer and match String class:
#Override
public JsonSerializer<Object> findNullValueSerializer(BeanProperty property) throws JsonMappingException {
if (property.getType().getRawClass().equals(String.class)) {
return EmptyStringSerializer.INSTANCE;
} else {
return super.findNullValueSerializer(property);
}
}
and then set SerializerProvider to our ObjectMapper instance.
done.
Given this these classes, how can I map a dictionary of them?
public class TestClass
{
public string Name { get; set; }
}
public class TestClassDto
{
public string Name { get; set; }
}
Mapper.CreateMap<TestClass, TestClassDto>();
Mapper.CreateMap<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>();
var testDict = new Dictionary<string, TestClass>();
var testValue = new TestClass() {Name = "value1"};
testDict.Add("key1", testValue);
var mappedValue = Mapper.Map<TestClass, TestClassDto>(testValue);
var mappedDict = Mapper.Map<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>(testDict);
Mapping one of them, mappedValue in this case, works fine.
Mapping a dictionary of them ends up with no entries in the destination object.
What am I doing worng?
The problem you are having is because AutoMapper is struggling to map the contents of the Dictionary. You have to think what it is a store of - in this case KeyValuePairs.
If you try create a mapper for the KeyValuePair combination you will quickly work out that you can't directly as the Key property doesn't have a setter.
AutoMapper gets around this though by allowing you to Map using the constructor.
/* Create the map for the base object - be explicit for good readability */
Mapper.CreateMap<TestClass, TestClassDto>()
.ForMember( x => x.Name, o => o.MapFrom( y => y.Name ) );
/* Create the map using construct using rather than ForMember */
Mapper.CreateMap<KeyValuePair<string, TestClass>, KeyValuePair<string, TestClassDto>>()
.ConstructUsing( x => new KeyValuePair<string, TestClassDto>( x.Key,
x.Value.MapTo<TestClassDto>() ) );
var testDict = new Dictionary<string, TestClass>();
var testValue = new TestClass()
{
Name = "value1"
};
testDict.Add( "key1", testValue );
/* Mapped Dict will have your new KeyValuePair in there */
var mappedDict = Mapper.Map<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>( testDict );
AutoMapper has changed a bit so it looks more like:
CreateMap<Thing, ThingDto>()
.ReverseMap();
CreateMap<Thing, KeyValuePair<int, ThingDto>>()
.ConstructUsing((t, ctx) => new KeyValuePair<int, ThingDto>(t.id, ctx.Mapper.Map<ThingDto>(t)));