Get values from below string using regex in java - java

I am a beginner to regex.
I have below String:
fail:2,success:1,fetch:1
Output Map: Get Map which contains all key-values as below:
fail - 2 (key=fail, value=2)
success - 1
fetch - 1
I have tried using below solution:
public static void main(String arg[]) {
String msg = "fail:1,success:1,policyfetch:1";
System.out.println(getKeyValuesFromMsg(msg));
}
public static Map getKeyValuesFromMsg(String msg) {
if (msg != null) {
Map keyvalues = new HashMap();
Pattern p = Pattern.compile("(\\w+):(,+)");
Matcher m = p.matcher(msg);
while (m.find()) {
keyvalues.put(m.group(1), m.group(2));
}
return keyvalues;
} else
return Collections.emptyMap();
}

You can use the split function, The following snippet should work fine
Map<String,String> map = new HashMap();
String str = "fail:2,success:1,fetch:1";
String[] keyValueParts = str.split(",");
for(String s : keyValueParts){
String parts[] = s.split(":");
map.put(parts[0],parts[1]);
}
System.out.println(map);

i would have used below method for the same.
public static void main(String arg[]) {
String msg = "fail:1,success:1,policyfetch:1";
System.out.println(getKeyValuesFromMsg(msg));
}
private static Map<Object, Object> getKeyValuesFromMsg(String msg) {
Map<Object,Object> mapObj = new HashMap<Object,Object>();
for (int i=0;i<msg.split(",").length;i++)
mapObj.put(msg.split(",")[i].split(":")[0],msg.split(",")[i].split(":")[1]);
return mapObj;
}

my solution:
public static Map<String, Integer> trans2Map(String source) {
if (null == source) {
return Collections.emptyMap();
}
Map<String, Integer> result = new HashMap<>();
Arrays.stream(source.split(","))
.filter(pair -> pair.split(":").length == 2)
.forEach(pair -> {
String key = pair.split(":")[0];
Integer value;
try {
value = Integer.parseInt(pair.split(":")[1]);
} catch (Exception e) {
return;
}
result.put(key, value);
});
return result;
}

Related

How to find a key in a multiple-value HashMap?

my task is to make a synonym dictionary using HashMaps and Sets.
I have the following code in my main method:
public static void main(String[] args) {
addSynonym("casa", "imobil");
addSynonym("casa", "cladire");
addSynonym("casa", "locuinta");
addSynonym("casa", "camin");
addSynonym("casa", "domiciuliu");
addSynonym("jucarie", "joc");
addSynonym("jucarie", "marioneta");
addSynonym("jucarie", "papusa");
addSynonym("jucarie", "pantin");
addSynonym("om", "barbat");
addSynonym("om", "persoana");
afisarearray(getSynonyms("camin"));
}
One Method to insert the synonyms and the keys:
static Map<String, ArrayList<String>> synonymTable = new HashMap<String, ArrayList<String>>();
public static void addSynonym(String word, String synonym) {
ArrayList<String> checklist = synonymTable.get(word);
if (checklist == null) {
ArrayList<String> temporarylist = new ArrayList<String>();
temporarylist.add(synonym);
synonymTable.put(word, temporarylist);
} else {
synonymTable.get(word).add(synonym);
}
}
One Method to display an ArrayList:
public static void afisarearray(ArrayList<String> list) {
if (list != null) {
for (String s : list) {
System.out.println(s + "\n");
}
} else {
System.out.println("Empty list");
}
};
I want to find all the synonyms of a word for example the word "camin". And I wrote the following Method to do it, but it's not working, it is returning an empty set of keys:
public static ArrayList<String> getSynonyms(String word) {
if (word == "") {
System.out.println("No word to serach for ");
return null;
} else {
Set<String> keySet = new HashSet<String>();
for (Map.Entry<String, ArrayList<String>> entry : synonymTable.entrySet()) {
if (entry.getValue().equals(word)) {
keySet.add(entry.getKey());
}
}
if (keySet.isEmpty()) {
System.out.println("No keys found");
return null;
} else {
ArrayList<String> newlist = new ArrayList<String>();
for (String s : keySet) {
newlist.addAll(synonymTable.get(s));
}
return newlist;
}
}
}
The problem is inside this if:
if (entry.getValue().equals(word))
is a comparison between an ArrayList and a string.
I don't know how to correct it.
Try this, I also cleaned up your code a bit for readability
public static List<String> getSynonyms(String word) {
if (word == null || word.trim().isEmpty()) {
System.out.println("No word to serach for ");
return null;
}
Set<String> keySet = new HashSet<String>();
for (Map.Entry<String, List<String>> entry : synonymTable.entrySet()) {
if (entry.getValue().contains(word)) {
keySet.add(entry.getKey());
}
}
if (keySet.isEmpty()) {
System.out.println("No keys found");
return null;
}
//de-dupe and sort
Collection<String> terms = new TreeSet<>();
for (String s : keySet) {
terms.addAll(synonymTable.get(s));
}
return new ArrayList<>(terms);
}

Sort a map based on value which is an arraylist java 8

I have a Hashmap map<String, List<Student>>.
Student {
String name;
List<Date> absentDates;
}
Key Values pairs as follows:
["Class1",<Student1 absentDates = [02/11/2010, 02/09/2010]><Student2 absentDates = [02/10/2010]>]
["Class2",<Student3 absentDates = [02/12/2010]>]
["Class3",<Student4 absentDates = null>]
How can I sort this map using java 8 steams as follows, based on map value ie, List.get(0).getAbsentDates().get(0) ie, a nullable absentDates of first Student object in each list
Expected output is
["Class2",<Student3 absentDates = [02/12/2010]>]
["Class1",<Student1 absentDates = [02/11/2010, 02/09/2010]><Student2 absentDates = [02/10/2010]>]
["Class3",<Student4 absentDates = null>]
Steps I followed.
Map<String, List> Stream through the entrySet
convert to class MapValues{Key,List}. MapValue is the Custom wrapper class created to hold key and value
Implement Comaparator for MapValues based on stundents.get(0).getAbsentDates().get(0) and also handle null in comaprator
Collect using Collectors.toMap to preserve the order use LinkedHashMap
In Short the
Map<String, List<Student>> newmap = map.entrySet()
.stream()
.map(e -> new MapValues(e.getKey(), e.getValue()))
.sorted(new MapValuesComp())
.collect(Collectors.toMap(
MapValues::getKey, MapValues::getStdns,
(e1, e2) -> e1,
LinkedHashMap::new));
public class CustomMapSorting {
public static void main(String[] args) throws ParseException {
Map<String, List<Student>> map = new HashMap<>();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
// Class1 Stundent1
Date s1Date1 = format.parse("02/11/2010");
Date s1Date2 = format.parse("02/09/2010");
Date[] s1absentDates = { s1Date1, s1Date2 };
Student s1 = new Student("Student1", Arrays.asList(s1absentDates));
// Class1 Stundent2
Date s2Date1 = format.parse("02/10/2010");
Date[] s2absentDates = { s2Date1 };
Student s2 = new Student("Student2", Arrays.asList(s2absentDates));
// Class2 Stundent3
Date s3Date1 = format.parse("02/12/2010");
Date[] s3absentDates = { s3Date1 };
Student s3 = new Student("Student3", Arrays.asList(s3absentDates));
// Class3 Stundent4
Student s4 = new Student("Stundent4", null);
List<Student> class1SundLst = Arrays.asList(s1, s2);
map.put("Class1", class1SundLst);
map.put("Class2", Arrays.asList(s3));
map.put("Class3", Arrays.asList(s4));
Map<String, List<Student>> newmap = map.entrySet()
.stream()
.map(e -> new MapValues(e.getKey(), e.getValue()))
.sorted(new MapValuesComp())
.collect(Collectors.toMap(MapValues::getKey, MapValues::getStdns, (e1, e2) -> e1, LinkedHashMap::new));
//Printing the sorted values
newmap.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " : " + e.getValue().get(0).absentDates));
}
}
class MapValues {
String key;
List<Student> stdns;
public MapValues(String key, List<Student> stdns) {
super();
this.key = key;
this.stdns = stdns;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<Student> getStdns() {
return stdns;
}
public void setStdns(List<Student> stdns) {
this.stdns = stdns;
}
#Override
public String toString() {
// TODO Auto-generated method stub
return key;
}
}
class MapValuesComp implements Comparator<MapValues> {
public int compare(MapValues o1, MapValues o2) {
if (o1.stdns.get(0).absentDates == null) {
return (o2.stdns.get(0).absentDates == null) ? 0 : 1;
}
if (o2.stdns.get(0).absentDates == null) {
return 1;
}
return o2.stdns.get(0).absentDates.get(0).compareTo(o1.stdns.get(0).absentDates.get(0));
}
}
class Student {
String name;
List<Date> absentDates;
public Student(String name, List<Date> absentDates) {
super();
this.name = name;
this.absentDates = absentDates;
}
#Override
public String toString() {
if (absentDates == null)
return null;
SimpleDateFormat format = new SimpleDateFormat("dd/mm/YYYY");
return format.format(absentDates.get(0));
}
}
I tried a inline solution using lambdas from the answer posted using #Rono. Just a improved version of his solution.
Map<String, List<Student>> res = map.entrySet()
.stream()
.sorted((o1, o2) -> {
if (o1.getValue().get(0).absentDates == null) {
return (o2.getValue().get(0).absentDates == null) ? 0 : 1;
}
if (o2.getValue().get(0).absentDates == null) {
return 1;
}
return o2.getValue().get(0).absentDates.get(0).compareTo(o1.getValue().get(0).absentDates.get(0));
}).
collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue,(e1, e2) -> e1,
LinkedHashMap::new));

In java how to add nested map object

I am having a JSON data and I am converting that payload into a map object of nested. But it is overriding according to my logic.
I am having input json like this
{"mapping": {
"EVENT.alertMessage": "input.Message",
"EVENT.id": "input.id",
"EVENT.severity": "Functions.toString(\"P1\")",
"EVENT.eventTime": "input.eventTime",
"EVENT.eventType": "input.alertType",
"EVENT.geocoordinates.location": "Functions.toString(\"\")",
"EVENT.deviceName": "Functions.toString(\"\")",
"EVENT.visualInfo.imageUrl": "input.imageUrl",
"EVENT.deviceId": "input.cameraId",
"EVENT.geocoordinates.longitude": "Functions.toString(\"\")",
"EVENT.visualInfo.videoUrl": "input.videoUrl",
"EVENT.tenantCode": "Functions.toString(\"\")",
"EVENT.MAC": "input.cameraId",
"EVENT.DATE_TIME": "Functions.currentDate(\"yyyy-MM-dd HH:mm:ss\",\"UTC\")",
"EVENT.geocoordinates.latitude": "Functions.toString(\"\")"
}
}
Here from the above input JSON Keys I am iterating and forming map object.
ForEx:
INPUT:
{"mapping": {
"TEST.key1": "a",
"TEST.key2.key3": "b",
}
}
OUTPUT:
{
"TEST":{
"key1":a,
"key2":{
"key3":b
}
}
}
The code that I have written is
JSONObject json=new JSONObject(mappingData).getJSONObject("mapping");
Iterator<String> keys=new JSONObject(mappingData).getJSONObject("mapping").keys();
Map<String,Object> map = new HashMap<>();
while(keys.hasNext()) {
String val = keys.next();
String[] key=val.split("(?<!/)\\.");
Map<String, Object> lastKeyMap = null;
for(int i=0;i<key.length;i++)
{
if(i== 0 && key.length==1){
String outputVal=json.getString(val);
if(outputVal.contains("[]")){
outputVal=outputVal.replace("[]", "[i]");
}
//Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])|([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])")
// .matcher(outputVal);
Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9]{0,})")
.matcher(outputVal);
while (m.find()) {
outputVal=m.replaceAll("[`$1`]").replace("/", "");
}
if(key[i].contains("/"))
{
map.put("`"+key[i].replace("/", "")+"`",outputVal);
}
else{
map.put(key[i],outputVal);
}
}
else if(i== 0 && key.length>1){
if(map.containsKey(key[i])){
lastKeyMap = (Map<String, Object>) map.get(key[i]);
}else{
if(key[i].contains("/"))
{
lastKeyMap = new HashMap<String,Object>();
map.put("`"+key[i].replace("/", "")+"`",lastKeyMap);
}
else{
lastKeyMap = new HashMap<String,Object>();
map.put(key[i],lastKeyMap);
}
}
}else if(i== key.length-1 ){
String outputVal=json.getString(val);
if(outputVal.contains("[]")){
outputVal=outputVal.replace("[]", "[i]");
}
//Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])|([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9])")
// .matcher(outputVal);
Matcher m = Pattern.compile("\\.([a-zA-Z0-9]{0,}\\/.[a-zA-Z0-9]{0,})")
.matcher(outputVal);
while (m.find()) {
outputVal=m.replaceAll("[`$1`]").replace("/", "");
}
if(key[i].contains("/"))
{
lastKeyMap.put("`"+key[i].replace("/", "")+"`", outputVal);
}
else{
lastKeyMap.put(key[i], outputVal);
}
}else{
Map<String,Object> objMap = new HashMap<>();
if(key[i].contains("/"))
{
lastKeyMap.put("`"+key[i].replace("/", "")+"`", objMap);
lastKeyMap = objMap;
}
else{
lastKeyMap.put(key[i], objMap);
lastKeyMap = objMap;
}
}
}
}
The output I am getting is :
{EVENT={severity=Functions.toString("P1"), alertMessage=input.alertMessage, id=input.id, eventTime=input.eventTime, visualInfo={videoUrl=input.videoUrl}, eventType=input.alertType, tenantCode=Functions.toString(""), DATE_TIME=Functions.currentDate("yyyy-MM-dd HH:mm:ss","UTC"), geocoordinates={latitude=Functions.toString("")}, deviceName=Functions.toString(""), deviceId=input.cameraId, MAC=input.cameraId}}
But in the result EVENT.geocoordinates.longitude and EVENT.geocoordinates.longitude is skipped as the map is being overridden. Like that EVENT.visualInfo.imageUrl is also overridden by EVENT.visualInfo.videoUrl.So, how can I overcome this one and form a map or json with all the json keys by iterating without veing overriden.
The best approach is to create java class according to json schema:
public class Test {
#SerializedName("mapping")
public Mapping mapping;
static public class Mapping {
#SerializedName("EVENT.alertMessage")
public String alertMessage;
#SerializedName("EVENT.id")
public String id;
#SerializedName("EVENT.severity")
public String severity;
#SerializedName("EVENT.eventTime")
public String eventTime;
#SerializedName("EVENT.eventType")
public String eventType;
#SerializedName("EVENT.geocoordinates.location")
public String location;
#SerializedName("EVENT.deviceName")
public String deviceName;
#SerializedName("EVENT.visualInfo.imageUrl")
public String imageUrl;
#SerializedName("EVENT.deviceId")
public String deviceId;
#SerializedName("EVENT.geocoordinates.longitude")
public String longitude;
#SerializedName("EVENT.visualInfo.videoUrl")
public String videoUrl;
#SerializedName("EVENT.tenantCode")
public String tenantCode;
#SerializedName("EVENT.MAC")
public String mac;
#SerializedName("EVENT.DATE_TIME")
public String dateTime;
#SerializedName("EVENT.geocoordinates.latitude")
public String latitude;
}
}
And then parse it with google gson library
Test test = new Gson().fromJson("jsonString", Test.class);
Working with your own java object is much easier than with JSONObject
My current dependency for gson in gradle file:
implementation("com.google.code.gson:gson:2.8.6")

Merge three HashMap<String, POJO> with unique key

I have three HashMap of this generic type HashMap<String,Server> but I want to combine the data of all the HashMap based on my unique key. But only Server pojo has different data.
So first hashmap has some servers information like ip_address and server name therefore I have both values in map and ip_address as key. Then I have some other hardware spec stored in other map and ip_address as key and so same in third map.
Therefore, combining all POJO based on key I will get complete server information with corresponding ip_address.
I don't know how to do it without doing nested operation
Here is a way to do this. I have shown the example code using only two Map collections, and, the third can be applied in similar way.
I am using Map's computeIfPresent method to "merge" the values of the two maps. The BiFunction (the argument for the computeIfPresent) need to take care of the "merging" aspect - any validations, etc., presently not known (to me). Also, see the note below on computeIfPresent.
Example code:
import java.util.*;
import java.util.function.*;
public class CombiningMaps {
private static Map<String, ServerInfo> map1 = new HashMap<>();
private static Map<String, ServerInfo> map2 = new HashMap<>();
private static BiFunction<String, ServerInfo, ServerInfo> combineWithMap2 =
(k, v) -> {
if (map2.get(k) != null) {
ServerInfo v2 = map2.get(k);
// combine values of map1 and map2
v.setHw2(v2.getHw2());
}
return v;
};
public static void main(String [] args) {
// Add some test data to map 1
map1.put("serv1", new ServerInfo("0A", "a1", "",""));
map1.put("serv2", new ServerInfo("0B", "b1", "",""));
map1.put("serv3", new ServerInfo("0C", "c1", "",""));
System.out.println(map1);
// Add some data to map 2
map2.put("serv1", new ServerInfo("0A", "", "a2",""));
map2.put("serv2", new ServerInfo("0B", "", "b2",""));
map2.put("serv3", new ServerInfo("0C", "", "c2",""));
System.out.println(map2);
// Update map1 with map 2's info
map1.forEach((k,v) -> map1.computeIfPresent(k, combineWithMap2));
System.out.println(map1);
}
}
class ServerInfo {
private String ipAddr;
private String hw1; // hw stands for hardware related info
private String hw2;
private String hw3;
public ServerInfo(String ipAddr, String hw1, String hw2, String hw3) {
this.ipAddr = ipAddr;
this.hw1 = hw1;
this.hw2 = hw2;
this.hw3 = hw3;
}
public String getIpAddr() {
return ipAddr;
}
public String getHw1() {
return hw1;
}
public void setHw1(String s) {
hw1 = s;
}
public String getHw2() {
return hw2;
}
public void setHw2(String s) {
hw2 = s;
}
public String getHw3() {
return hw3;
}
public void setHw3(String s) {
hw3 = s;
}
public String toString() {
return ipAddr + ":" + hw1 + "-" + hw2 + "-" + hw3;
}
}
The output:
{serv2=0B:b1--, serv3=0C:c1--, serv1=0A:a1--}
{serv2=0B:-b2-, serv3=0C:-c2-, serv1=0A:-a2-}
{serv2=0B:b1-b2-, serv3=0C:c1-c2-, serv1=0A:a1-a2-}
How the computeIfPresent behaves (some scenarios):
Consider a Map<String, Integer> map with keys and values: {four=4, one=1, ten=10, two=2, three=3, five=5, eleven=11, twelve=null}
(1) updates the mapping with new value (note the lambda is a BiFunction returning a newly computed value):
map.computeIfPresent("ten", (k, v) -> new Integer(100));
(2) the function returns a null, the existing mapping is removed:
map.computeIfPresent("eleven", (k, v) -> null);
(3) the mapping is not added, as there is no existing mapping:
map.computeIfPresent("twenty", (k, v) -> new Integer(20));
(4) the existing value is null, so there is no change:
map.computeIfPresent("twelve", (k, v) -> new Integer(12));
After getting all values in three maps I used finalMap to combine values. Here is the working for it since the key is same in all map therefore getting key of map using key of
map1 was a good idea.
Set<Map.Entry<String, Server>> set1 = map.entrySet();
for (Map.Entry<String, Server> me : set1) {
Server server=new Server();
server.setIp_Address(me.getKey());
server.setServerName(me.getValue().getServerName());
server.setOsName(map1.get(me.getKey()).getOsName());
server.setOsVersion(map1.get(me.getKey()).getOsVersion());
server.setOsArchitecture(map1.get(me.getKey()).getOsArchitecture());
server.setHardDiskCapacity(map1.get(me.getKey()).getHardDiskCapacity());
server.setRamCapacity(map1.get(me.getKey()).getRamCapacity());
server.setAvgNetWorkUtilizationSent(map2.get(me.getKey()).getAvgNetWorkUtilizationSent());
server.setAvgNetworkUtilizationReceived(map2.get(me.getKey()).getAvgNetworkUtilizationReceived());
server.setAvgCPUtilization(map2.get(me.getKey()).getAvgCPUtilization());
server.setAvgRamUtilization(map2.get(me.getKey()).getAvgRamUtilization());
finalMap.put(me.getKey(), server);
}
Set<Map.Entry<String, Server>> set2 = finalMap.entrySet();
for (Map.Entry<String, Server> me : set2) {
System.out.println(" ServerIP : "+ me.getValue().getIp_Address()+"\t"+" Server Name :"+me.getValue().getServerName()+"\t \t"+" Hardware Capacity :"+me.getValue().getHardDiskCapacity()+"\t"+" Average CPU Utlization: "+me.getValue().getAvgCPUtilization());
}
Use putAll():
Map<String, Server> all = new HashMap<>();
all.putAll(map1);
all.putAll(map2);
all.putAll(map3);
If you want a one-liner:
Map<String, Server> all = Stream.of(map1, map2, map3)
.reduce(new HashMap<>(), (a, b) -> {a.putAll(b); return a;});
Collisions result in replacement.
Your problem it to merge two POJO classes.
For example
class Server {
private String ipAddr;
private String hw1;
private String hw2;
private String hw3;
//Getter and Setters
}
Server s1 = new Server("0A", "a1", null, null);
Server s2 = new Server("0A", null, "b2", null);
So the merged pojo should be like this.
Server merged = merge(s1, s2);// Server{ipAddr=0A, hw1=a1, hw2=b2, hw3=null}
The merge function looks like this...
public static Server merge(Server s1, Server s2) throws Exception {
Server merged = new Server();
for (Field field : Server.class.getDeclaredFields()) {
field.setAccessible(true);
Object getS1 = field.get(s1);
Object getS2 = field.get(s2);
if(getS1 == null && getS2 != null) {
field.set(merged, getS2);
} else if (getS1 != null && getS2 == null) {
field.set(merged, getS1);
} else { //equal values
field.set(merged, getS1);
}
}
return merged;
}
Here is the example code to merge three maps, Its kinda quick and dirty but works well.
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
class MergeMaps {
public static void main(String[] args) throws Exception {
Map<String, Server> map1 = new HashMap<>();
Map<String, Server> map2 = new HashMap<>();
Map<String, Server> map3 = new HashMap<>();
// Add some test data to map 1
map1.put("serv1", new Server("0A", "a1", null, null));
map1.put("serv2", new Server("0B", "b1", null, null));
System.out.println(map1);
// Add some data to map 2
map2.put("serv1", new Server("0A", null, "a2", null));
map2.put("serv2", new Server("0B", null, "b2", null));
map2.put("serv3", new Server("0C", null, "c2", null));
System.out.println(map2);
// Add some data to map 3
map3.put("serv1", new Server("0A", null, null, "a3"));
map3.put("serv2", new Server("0B", null, null, "b3"));
map3.put("serv3", new Server("0C", null, null, "c3"));
map3.put("serv4", new Server("0D", null, null, "d4"));
System.out.println(map3);
Map<String, Server> resultingMap = new HashMap<>();
resultingMap.putAll(map1);
for (Map.Entry<String, Server> entry : map2.entrySet()) {
if (resultingMap.containsKey(entry.getKey())) {
Server s = resultingMap.get(entry.getKey());
Server t = entry.getValue();
Server merged = merge(s, t);
resultingMap.put(entry.getKey(), merged);
} else {
resultingMap.put(entry.getKey(), entry.getValue());
}
}
for (Map.Entry<String, Server> entry : map3.entrySet()) {
if (resultingMap.containsKey(entry.getKey())) {
Server server1 = resultingMap.get(entry.getKey());
Server server2 = entry.getValue();
Server merged = merge(server1, server2);
resultingMap.put(entry.getKey(), merged);
} else {
resultingMap.put(entry.getKey(), entry.getValue());
}
}
System.out.println(resultingMap);
}
public static Server merge(Server s1, Server s2) throws Exception {
Server merged = new Server();
for (Field field : Server.class.getDeclaredFields()) {
field.setAccessible(true);
Object getS1 = field.get(s1);
Object getS2 = field.get(s2);
if (getS1 == null && getS2 != null) {
field.set(merged, getS2);
} else if (getS1 != null && getS2 == null) {
field.set(merged, getS1);
} else {
field.set(merged, getS1);
}
}
return merged;
}
}
class Server {
private String ipAddr;
private String hw1;
private String hw2;
private String hw3;
public Server() {
}
public Server(String ipAddr, String hw1, String hw2, String hw3) {
this.ipAddr = ipAddr;
this.hw1 = hw1;
this.hw2 = hw2;
this.hw3 = hw3;
}
//Getter and setters
#Override
public String toString() {
return "Server{" + "ipAddr=" + ipAddr + ", hw1=" + hw1 + ", hw2=" + hw2 + ", hw3=" + hw3 + '}';
}
}
The output looks like this..
{serv2=Server{ipAddr=0B, hw1=b1, hw2=null, hw3=null}, serv1=Server{ipAddr=0A, hw1=a1, hw2=null, hw3=null}}
{serv2=Server{ipAddr=0B, hw1=null, hw2=b2, hw3=null}, serv3=Server{ipAddr=0C, hw1=null, hw2=c2, hw3=null}, serv1=Server{ipAddr=0A, hw1=null, hw2=a2, hw3=null}}
{serv2=Server{ipAddr=0B, hw1=null, hw2=null, hw3=b3}, serv3=Server{ipAddr=0C, hw1=null, hw2=null, hw3=c3}, serv4=Server{ipAddr=0D, hw1=null, hw2=null, hw3=d4}, serv1=Server{ipAddr=0A, hw1=null, hw2=null, hw3=a3}}
{serv2=Server{ipAddr=0B, hw1=b1, hw2=b2, hw3=b3}, serv3=Server{ipAddr=0C, hw1=null, hw2=c2, hw3=c3}, serv4=Server{ipAddr=0D, hw1=null, hw2=null, hw3=d4}, serv1=Server{ipAddr=0A, hw1=a1, hw2=a2, hw3=a3}}

How to copy value from one list to another list having different objects

I have MaterailInfo and StyleInfo, I want to set styleDescription based on StyleNumber matching with materialNumber. I am using 2 for loops, is there any alternative solution?
MaterailInfo:
class MaterailInfo {
private String materialNumber;
private String materialDescription;
public MaterailInfo(String materialNumber, String materialDescription) {
this.materialNumber = materialNumber;
this.materialDescription = materialDescription;
}
// getter setter methods
}
StyleInfo:
class StyleInfo {
private String StyleNumber;
private String styleDescription;
public StyleInfo(String styleNumber, String styleDescription) {
StyleNumber = styleNumber;
this.styleDescription = styleDescription;
}
// getter setter toString methods
}
TEst12:
public class TEst12 {
public static void main(String[] args) {
List<MaterailInfo> mList = new ArrayList<MaterailInfo>();
mList.add(new MaterailInfo("a", "a-desc"));
mList.add(new MaterailInfo("b", "b-desc"));
mList.add(new MaterailInfo("c", "c-desc"));
List<StyleInfo> sList = new ArrayList<StyleInfo>();
sList.add(new StyleInfo("a", ""));
sList.add(new StyleInfo("b", ""));
sList.add(new StyleInfo("c", ""));
for (MaterailInfo m : mList) {
for (StyleInfo s : sList) {
if (s.getStyleNumber().equals(m.getMaterialNumber())) {
s.setStyleDescription(m.getMaterialDescription());
}
}
}
System.out.println(sList);
}
}
If you use a Map instead of a List to store your data, you can get away with doing only a single loop:
Map<String, String> mMap = new HashMap<String, String>();
mMap.put("a", "a-desc");
mMap.put("b", "b-desc");
mMap.put("c", "c-desc");
Map<String, String> sMap = new HashMap<String, String>();
sMap.put("a", "");
sMap.put("b", "");
sMap.put("c", "");
for (Map.Entry<String, String> entry : mMap.entrySet()) {
sMap.put(entry.getKey(), mMap.get(entry.getKey());
}
This code will leave the style description empty if the style number does not match any known material number.
If your numbers can't have duplicates, using a HashMap instead of classes can be a bit faster.
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Demo {
public static void main(String[] args) {
HashMap<String, String> mList = new HashMap();
HashMap<String, String> sList = new HashMap();
mList.put("a", "a-desc");
mList.put("b", "b-desc");
mList.put("c", "c-desc");
sList.put("a", "");
sList.put("b", "");
sList.put("c", "");
Iterator entries = sList.entrySet().iterator();
while (entries.hasNext()) {
Entry entry = (Entry) entries.next();
if (mList.containsKey(entry.getKey())) {
sList.put((String) entry.getKey(), mList.get(entry.getKey()));
}
}
for (Map.Entry<String, String> entry : sList.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
You can do this using one for loop like this
for (int i = 0; i < mList.size(); i++) {
sList.get(i).setStyleDescription(mList.get(i).getMaterialDescription());
}
Note: i am assuming you have balanced lists in term of size.

Categories