How to fetch the HashMap based on Max-key field
Eaxmple:-
List<Map<String, Integer>> data = new ArrayList<Map<String, Integer>>();
Map<String, Integer> map1 = new HashMap<>();
map1.put("A", 10);
map1.put("B", 15);
map1.put("C", 20);
Map<String, Integer> map2 = new HashMap<>();
map2.put("A", 20);
map2.put("B", 30);
map2.put("C", 50);
Map<String, Integer> map3 = new HashMap<>();
map3.put("A", 50);
map3.put("B", 60);
map3.put("C", 70);
data.add(map1);
data.add(map2);
data.add(map3);
Here I have 3 maps that I'm storing in the List.
Now I want to filter a map based on max A key value.
In the above map3 A value has the max integer value.
Expected out is:-
In the last only the map3 should present inside the List.
Is it possible to filter using java8?
Any suggestions would be helpful
I finally able to do this with help of comparator
Here is the code that will filter based filtering based on max date:-
Result is stored in the firstMap variable...
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String args[]) throws ParseException {
Map<String, String> s = test();
System.err.println(s.toString());
}
public static Map<String, String> test() throws ParseException {
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<>();
map1.put("A", "2021-03-08");
Map<String, String> map2 = new HashMap<>();
map2.put("A", "2021-03-23");
Map<String, String> map3 = new HashMap<>();
map3.put("A", "2021-03-21");
data.add(map1);
data.add(map2);
data.add(map3);
SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd");
Date firstMax = si.parse(data.get(0).get("A"));
Map<String, String> firstMap = data.get(0);
for (Map<String, String> map : data) {
Date loopMax = si.parse(map.get("A"));
if (firstMax.compareTo(loopMax) < 0) {
firstMax = loopMax;
firstMap = map;
}
}
return firstMap;
}
}
Output:-
{A=2021-03-23}
Related
I have a List<Items> items of model Items
class Items {
String name;
String? value;
String price;
}
And i have a Map<String, String> values witch contains values for model. As a key it uses same data as Items.name and value of it contains data.
So my question is. How can i update my items list with added value data from values map? Basically, list of items contains only required field name. Later i get values for it and place it inside map. Now i want to update my list with added value data
This is how you structure the class to accept the Map<String, String> values:
class Item {
String? name;
String? value;
Item({this.name, this.value});
Item.fromJson(Map<String, dynamic> json) {
name = json['name'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = name;
data['value'] = value;
return data;
}
}
If this is your map:
Map<String, String> values = {"name": "wambua", "value": "Programmer"};
This is how you map the map to the class:
Item.fromJson(values);
I have this DTO class and in response of API i get the list of this :
class ProjectCode {
String id;
String projectCode;
String projectTitle;
ProjectCode({this.id, this.projectCode, this.projectTitle});
ProjectCode.fromJson(Map<String, dynamic> json) {
id = json['Id'];
projectCode = json['ProjectCode'];
projectTitle = json['ProjectTitle'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Id'] = this.id;
data['ProjectCode'] = this.projectCode;
data['ProjectTitle'] = this.projectTitle;
return data;
}
}
now how can i search in list and find the ProjectTitle that i need to find it and that method return the id in DTO ?
This is simple, you can use this method to find the String that you need to find. make method like this for everything do you need to search like this :
String find(List<ProjectCode> projectCodeList, String projectTitle) {
return projectCodeList
.firstWhere(
(projectCode) => projectCode.projectTitle.contains(projectTitle))
.id;
}
In my unit test, I need to test order of elements as that :
test("Check map order", () {
LinkedHashMap<String, String> actualMap = LinkedHashMap();
actualMap["0"] = "firstValue";
actualMap["1"] = "secondValue";
LinkedHashMap<String, String> expectedMap = LinkedHashMap();
expectedMap["1"] = "secondValue";
expectedMap["0"] = "firstValue";
print("actual: $actualMap");
print("expected: $expectedMap");
expect(actualMap, isNot(expectedMap));
});
This test fails, event if order is respected:
actual: {0: firstValue, 1: secondValue}
expected: {1: secondValue, 0: firstValue}
package:test_api expect
test/services/game/negotiation/NegotiationGameService_test.dart 33:7 main.<fn>.<fn>
Expected: not {'1': 'secondValue', '0': 'firstValue'}
Actual: {'0': 'firstValue', '1': 'secondValue'}
First idea that works but is a bit boring:
test("Check map order", () {
LinkedHashMap<String, String> actualMap = LinkedHashMap();
actualMap["0"] = "firstValue";
actualMap["1"] = "secondValue";
LinkedHashMap<String, String> expectedMapSameOrder = LinkedHashMap();
expectedMap["0"] = "firstValue";
expectedMap["1"] = "secondValue";
LinkedHashMap<String, String> expectedMapDifferentOrder = LinkedHashMap();
expectedMap["1"] = "secondValue";
expectedMap["0"] = "firstValue";
print("actual: $actualMap");
print("expected: $expectedMapSameOrder");
print("expected: $expectedMapDifferentOrder");
expect(actualMap, expectedMapSameOrder);
expect(actualMap.keys, isNot(orderedEquals(expectedMapSameOrder.keys)));
expect(actualMap, expectedMapDifferentOrder);
expect(actualMap.keys, orderedEquals(expectedMapDifferentOrder.keys));
});
Im trying to order the names(ListaSTR) according to the order of the integers in ListaINT. Checked other post with this solution but is now working for me. Im newbie. What am I missing?
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
using System.Linq;
namespace Simple
{
public static class Program
{
static void Main()
{
List<string> ListaSTR = new List<string>{"Alberto","Bruno","Carlos","Mario","Pepe","Rodrigo"};
List<int> ListaINT = new List<int>{4,6,1,8,2,5};
List<string> O_ListaSTR = OrderBySequence(ListaSTR, ListaINT, Func<string,string>);
Console.WriteLine(O_ListaSTR);
Console.ReadLine();
}
public static List<string> OrderBySequence<string, int>(this List<string> source, List<int> order, Func<string,int> idSelector)
{
var lookup = source.ToLookup(idSelector, t => t);
foreach (var id in order)
{
foreach (var t in lookup[id])
{
yield return t;
}
}
}
}
}
Try this:
static void Main(string[] args)
{
List<string> ListaSTR = new List<string> { "Alberto", "Bruno", "Carlos", "Mario", "Pepe", "Rodrigo" };
List<int> ListaINT = new List<int> { 4, 6, 1, 3, 2, 5 };
var O_ListaSTR = ListaSTR.OrderBySequence(ListaINT);
Console.WriteLine(O_ListaSTR);
Console.ReadLine();
}
And your extension method can be in a simple form like this:
public static IEnumerable<string> OrderBySequence(this List<string> source, List<int> order)
{
var result = new List<string>();
foreach (var i in order)
{
result.Add(source[i - 1]);
};
return result;
}
this is my code ,i can get proxy from getActionProxy,but i can't get Action.
that's my first time test Struts2 action and don't know error.
The TestCase ClaimActionTest extends StrutsSpringTestCase.
#Test
public void testPrepareAddClaim() throws Exception{
ActionProxy proxy = getActionProxy("/claim.action/prepareAddClaim.do");
System.out.println(proxy.getActionName());
System.out.println(proxy.getNamespace());
System.out.println(proxy.getAction());
ClaimAction action = (ClaimAction) proxy.getAction();
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("registNo", "34244432432");
ActionContext actionContext = proxy.getInvocation().getInvocationContext();
actionContext.setParameters(paramMap);
String result = proxy.execute();
assertEquals("success", result);
}