java iterate over map except last iteration - java

I have the next map values:
{title=varchar(50), text=text}
I am trying to convert it into two strings like this:
StringBuffer string = new StringBuffer();
for (String keyinside: values.keySet()) {
string.append(keyinside + " " + values.get(keyinside) + ", ");
}
But what I want here - not inlude ", " at the last iteration. How can I do it?

Short java 8 alternative:
String result = values.entrySet().stream().map(e -> e.getKey() + " " + e.getValue())
.collect(Collectors.joining(", "))
Stream.map() to convert all entries in the map to a string description of the entry.
Note that Java 8 also finally adds the function String.join().

Use some indicator :
StringBuffer string = new StringBuffer();
boolean first = true;
for (String keyinside: values.keySet()) {
if (!first)
string.append (", ");
else
first = false;
string.append(keyinside + " " + values.get(keyinside));
}
BTW, it's more efficient to use StringBuilder (assuming you don't need thread safety).

I quite like Joiner from Google collections library. You could just do this:
on(",").withKeyValueSeparator(" ").join(values);
on is statically imported from com.google.common.base.Joiner.
If you use Maven, just add a dependency:
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>

Take a counter which increments in every iteration and get a if condition which checks whether it is iterating the last time
A simple trick is
int i = 0 ;
for (String keyinside: values.keySet()) {
string.append(keyinside + " " + values.get(keyinside));
if((i+1)<values.keySet().size()){
string.append(", ");
}
i++;
}
Also I suggest you to use StringBuilder if thread safety is not a concern

You can try this .by which we can remove the comma(,) at end.
Hope this helps you.
Map<String,String> s= new LinkedHashMap<String,String>();
s.put("1", "A");
s.put("2", "B");
s.put("3", "C");
StringBuffer sb = new StringBuffer();
String ans=null;
for (String keyinside: s.keySet()) {
sb.append(keyinside + " " + s.get(keyinside) + ", ").toString();
}
System.out.println(sb);
ans=sb.substring(0, sb.length()-2).toString();
System.out.println(ans);
Note: you can refer How to remove the last character from a string?

Related

Convert HOCON string into Java object

One of my webservice return below Java string:
[
{
id=5d93532e77490b00013d8862,
app=null,
manufacturer=pearsonEducation,
bookUid=bookIsbn,
model=2019,
firmware=[1.0],
bookName=devotional,
accountLinking=mandatory
}
]
I have the equivalent Java object for the above string. I would like to typecast or convert the above java string into Java Object.
I couldn't type-cast it since it's a String, not an object. So, I was trying to convert the Java string to JSON string then I can write that string into Java object but no luck getting invalid character "=" exception.
Can you change the web service to return JSON?
That's not possible. They are not changing their contracts. It would be super easy if they returned JSON.
The format your web-service returns has it's own name HOCON. (You can read more about it here)
You do not need your custom parser. Do not try to reinvent the wheel.
Use an existing one instead.
Add this maven dependency to your project:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
Then parse the response as follows:
Config config = ConfigFactory.parseString(text);
String id = config.getString("id");
Long model = config.getLong("model");
There is also an option to parse the whole string into a POJO:
MyResponsePojo response = ConfigBeanFactory.create(config, MyResponsePojo.class);
Unfortunately this parser does not allow null values. So you'll need to handle exceptions of type com.typesafe.config.ConfigException.Null.
Another option is to convert the HOCON string into JSON:
String hoconString = "...";
String jsonString = ConfigFactory.parseString(hoconString)
.root()
.render(ConfigRenderOptions.concise());
Then you can use any JSON-to-POJO mapper.
Well, this is definitely not the best answer to be given here, but it is possible, at least…
Manipulate the String in small steps like this in order to get a Map<String, String> which can be processed. See this example, it's very basic:
public static void main(String[] args) {
String data = "[\r\n"
+ " {\r\n"
+ " id=5d93532e77490b00013d8862, \r\n"
+ " app=null,\r\n"
+ " manufacturer=pearsonEducation, \r\n"
+ " bookUid=bookIsbn, \r\n"
+ " model=2019,\r\n"
+ " firmware=[1.0], \r\n"
+ " bookName=devotional, \r\n"
+ " accountLinking=mandatory\r\n"
+ " }\r\n"
+ "]";
// manipulate the String in order to have
String[] splitData = data
// no leading and trailing [ ] - cut the first and last char
.substring(1, data.length() - 1)
// no linebreaks
.replace("\n", "")
// no windows linebreaks
.replace("\r", "")
// no opening curly brackets
.replace("{", "")
// and no closing curly brackets.
.replace("}", "")
// Then split it by comma
.split(",");
// create a map to store the keys and values
Map<String, String> dataMap = new HashMap<>();
// iterate the key-value pairs connected with '='
for (String s : splitData) {
// split them by the equality symbol
String[] keyVal = s.trim().split("=");
// then take the key
String key = keyVal[0];
// and the value
String val = keyVal[1];
// and store them in the map ——> could be done directly, of course
dataMap.put(key, val);
}
// print the map content
dataMap.forEach((key, value) -> System.out.println(key + " ——> " + value));
}
Please note that I just copied your example String which may have caused the line breaks and I think it is not smart to just replace() all square brackets because the value firmware seems to include those as content.
In my opinion, we split the parse process in two step.
Format the output data to JSON.
Parse text by JSON utils.
In this demo code, i choose regex as format method, and fastjson as JSON tool. you can choose jackson or gson. Furthermore, I remove the [ ], you can put it back, then parse it into array.
import com.alibaba.fastjson.JSON;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SerializedObject {
private String id;
private String app;
static Pattern compile = Pattern.compile("([a-zA-Z0-9.]+)");
public static void main(String[] args) {
String str =
" {\n" +
" id=5d93532e77490b00013d8862, \n" +
" app=null,\n" +
" manufacturer=pearsonEducation, \n" +
" bookUid=bookIsbn, \n" +
" model=2019,\n" +
" firmware=[1.0], \n" +
" bookName=devotional, \n" +
" accountLinking=mandatory\n" +
" }\n";
String s1 = str.replaceAll("=", ":");
StringBuffer sb = new StringBuffer();
Matcher matcher = compile.matcher(s1);
while (matcher.find()) {
matcher.appendReplacement(sb, "\"" + matcher.group(1) + "\"");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
SerializedObject serializedObject = JSON.parseObject(sb.toString(), SerializedObject.class);
System.out.println(serializedObject);
}
}

Write method that puts the word "like" in between each of the words in the original sentence

I need to write a method that puts the word "like" in between each of the words in the original sentence.
For example:
teenTalk("That is so funny!")
would return "That like is like so like funny!"
Would you use sentence split for this?
You can do it like this:
String teenTalk(String source) {
return String.join(" like ", source.split(" "));
}
or like this:
String teenTalk(String source) {
return source.replaceAll(" ", " like ");
}
You can replace the space character with "like":
text.replace(" ", " like ");
Using Java 8 :
public static void main(String[] args) {
String testJoiner = "That is so funny!";
// Split the string into list of word
List<String> splited = Arrays.asList(testJoiner.split("\\s"));
// create new StringJoiner that add the delimiter like between each entry
StringJoiner joiner = new StringJoiner(" like ");
// internal iteration over the words and adding them into the joiner
splited.forEach(joiner::add);
// printing the new value
System.out.println(joiner.toString());
}
Not very efficient but the first thing that comes to mind is something "like" this:
import java.util.StringTokenizer;
public String teenTalk(String inputString) {
String liked = "";
StringTokenizer token = new StringTokenizer(inputString);
while (token.hasMoreElements()) {
liked += token.nextToken();
if (token.hasMoreElements()) {
liked += " like ";
}
}
return liked;
}
You can do something like this
String text = "That is so funny!";
text = text.replaceAll("\\s\\b", " like ");
This will also make sure that all " like " 's come in between sentence. It will always put the "like" in between the text, not in the start or at the end. Even if the text is something like this "This is so funny! " it will work.
What about just replacing " " by " like " ?
http://www.tutorialspoint.com/java/java_string_replace.htm

How to implement the template(replacing string with the element of list) in java.

I am developing an application in which I came across the requirement where I need to do the replacement of identifying words with the strings i.e. replacing the actual data in templates. In this I am getting the data from an arraylist and for that I have implemented the following solution.
List<String> abc = new ArrayList<String>();
abc.add("willy brown");
abc.add("jamster");
String message="//$^$// has commented on //$^$// .";
String messages[] = message.split(" ");
StringBuffer finalMessage = new StringBuffer();
for(int i=0,j=0; i<messages.length && j<=abc.size(); i++){
System.out.println("messages : " + messages[i]);
if(messages[i].equals("//$^$//")){
messages[i] = abc.get(j);
System.out.println("after changes " +messages[i]);
j++;
}
finalMessage.append(messages[i] +" ");
}
System.out.println("final message: " + finalMessage);
I just want to know if there is any better way to implement this and also if there is any vulnerability in this.
I would use MessageFormat.format for this:
List<String> abc = new ArrayList<String>();
abc.add("willy brown");
abc.add("jamster");
String message = "{0} has commented on {1}";
String finalMessage = MessageFormat.format(message, abc.toArray(new String[abc.size()]));
System.out.println(finalMessage);
You can use the built in String and StringBuffer methods to perform this task without having to break apart the message into an array. I also suggest extracting your field identifier into a separate variable. God forbid you miss-type it somewhere!
String fieldIdentifier = "//$^$//";
List<String> abc = new ArrayList<>();
abc.add("willy brown");
abc.add("jamster");
String message= fieldIdentifier + " has commented on " + fieldIdentifier + ".";
StringBuffer finalMessage = new StringBuffer(message);
int fieldCount = 0;
while(finalMessage.indexOf(fieldIdentifier) > -1) {
int index = finalMessage.indexOf(fieldIdentifier);
finalMessage = finalMessage.replace(index, index + fieldIdentifier.length(), abc.get(fieldCount));
fieldCount++;
}
System.out.println("final message: " + finalMessage);
Edit: using StringBuffer for these kinds of operations is highly preferable, since it is mutable and much faster than using String.

Optimize String += or Concat? [duplicate]

This question already has answers here:
String concatenation: concat() vs "+" operator
(12 answers)
Closed 9 years ago.
I am writing a file with possibly 1000 data points. I have classes for all of these and am currently writing all of the data at the end (the datapoints are taken at 1s intervals). What I am currently doing is written below, and it's very slow. Would I be better off changing how I am writing the string/bytes to the file? Or would I be better off writing this information to some file pointer as the application is running?
Btw, all of the things such as getAccuracy() and such are floats/ints (so it has to convert those also).
fileStr = "";
fileStr += "timestamp,Accuracy,Altitude,Latitude,Longitude,GPSSatelliteEntries\r\n";
for (Iterator<Entry> i = entries.iterator(); i.hasNext(); ) {
Entry item = i.next();
long ts = item.getTs();
DataEntry d = item.getD();
List<GPSSatelliteEntry> satellites = item.getG();
// write stuff
fileStr += ts + ",";
fileStr += d.getAccuracy() + "," + d.getAltitude() + "," + d.getLatittude() + "," + d.getLongitude() + ",";
fileStr += "[";
boolean entered = false;
for (Iterator<GPSSatelliteEntry> j = satellites.iterator(); j.hasNext(); ) {
GPSSatelliteEntry item2 = j.next();
entered = true;
fileStr += "(" + item2.getAzimuth() + "," + item2.getElevation() + "," + item2.getPrn() + "," + item2.getSnr() + "),";
}
// chop off extra ,
if (entered)
fileStr = fileStr.substring(0, fileStr.length() - 1);
fileStr += "]";
fileStr += "\r\n";
}
Everytime you have hard work with Strings, use StringBuilder or StringBuffer to achieve better performance .
Don't forget that String is immutable, and each time you modify String new instance will be created and it costs performance.
Most probably string buffer
A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.
or go for string builder
StringBuilder stuff = new StringBuilder();
stuff.append("PUT YOUR STRINGS HERE");
stuff.append("PUT YOUR STRINGS HERE");
Then you can use 'stuff' to print the strings.
Put it in a loop and iterate over a large number with a timer to see the advantages, it's pretty interesting.

How to split String by taking space's in java

I know its very easy to split data in strings, but still i want guide to concate string,
my data is in the format. In my string the data is in the above format
104
inNetStandardGuest
windowsGuest
uestToolsTooOld
121
slesGuest
guestToolsTooOld
20569355609
Expected Output:
104,inNetStandardGuest,windowsGuest,uestToolsTooOld
121,slesGuest,guestToolsTooOld,20569355609
It's simply splitting and combining strings.
StringBuilder out = new StringBuilder();
for (String set : data.split("\n\n\n")) {
for (String line : set.split("\n")) {
out.append(line).append(',');
}
out.setCharAt(out.length(), '\n');
}
System.out.println(out);
With Guava's Splitter and Joiner:
final Iterable<String> lines = Splitter.on("\n\n\n").split(input);
for (final String line : lines) {
final Iterable<String> fields = Splitter.on("\n").split(line);
final String joined = Joiner.on(",").join(fields);
}
How about this?
String s = "104\n" +
"inNetStandardGuest\n" +
"windowsGuest\n" +
"uestToolsTooOld\n" +
"\n" +
"\n" +
"121\n" +
"slesGuest\n" +
"guestToolsTooOld\n" +
"20569355609\n";
System.out.println(s.replaceAll("(.)\\n","$1,")
.replaceAll(",,","\n")
.replaceAll(",\\n","\n"));
Probably not the most efficient way, though.
Buffered reader:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html
readLine() method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine()
For example you read 4 lines
string outputLine = line1 + "," + line2 + "," + line3 + "," + line4;
Then read 2 lines and skip it.
If you don't know how to implement it using my advices, you should read
some basics tutorial.
Try this :
String str = "104\ninNetStandardGuest\nwindowsGuest\nuestToolsTooOld\n\n\n121\nslesGuest\nguestToolsTooOld\n20569355609";
str= str.replaceAll("\\s", ",").replaceAll(",,,", "\n");
System.out.println(str);
Output :
104,inNetStandardGuest,windowsGuest,uestToolsTooOld
121,slesGuest,guestToolsTooOld,20569355609

Categories