Let's suppose i have a HashMap with some entries:
Map hm= new HashMap();
hm.put(1,"ss");
hm.put(2,"ss");
hm.put(3,"bb");
hm.put(4,"cc");
hm.put(5,"ss");
i want output like:
[{1,ss},{2,ss},{5,ss}]
Is it possible?
Of course it is:
List<Map.Entry<Integer,String>> list =
hm.entrySet().stream().collect(Collectors.toList());
You should change the definition of your Map to:
Map<Integer,String> hm = new HashMap<>();
P.S. You didn't specify whether you want all the entries in the output List, or just some of them. In the sample output you only included entries having "ss" value. This can be achieved by adding a filter:
List<Map.Entry<Integer,String>> list =
hm.entrySet().stream().filter(e -> e.getValue().equals("ss")).collect(Collectors.toList());
System.out.println (list);
Output:
[1=ss, 2=ss, 5=ss]
EDIT: You can print that List in the desired format as follows:
System.out.println (list.stream ().map(e -> "{" + e.getKey() + "," + e.getValue() + "}").collect (Collectors.joining (",", "[", "]")));
Output:
[{1,ss},{2,ss},{5,ss}]
Firstly, you declare your HashMap like this:
HashMap<Integer, String> hm = new HashMap<>();
Then after putting the key and the values you can print the whole HashMap it like this:
System.out.println("Mappings of HashMap hm1 are : " + hm);
If you want to print the value where the key is equal to 1 then:
if (hm.containsKey(1)) {
String s = hm.get(1);
System.out.println("value for key 1 is: " + s);
}
Related
My program reads a line from a file. This line contains comma-separated text like:
123,test,444,"don't split, this",more test,1
I would like the result of a split to be this:
123
test
444
"don't split, this"
more test
1
If I use the String.split(","), I would get this:
123
test
444
"don't split
this"
more test
1
In other words: The comma in the substring "don't split, this" is not a separator. How to deal with this?
You can try out this regex:
str.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
This splits the string on , that is followed by an even number of double quotes. In other words, it splits on comma outside the double quotes. This will work provided you have balanced quotes in your string.
Explanation:
, // Split on comma
(?= // Followed by
(?: // Start a non-capture group
[^"]* // 0 or more non-quote characters
" // 1 quote
[^"]* // 0 or more non-quote characters
" // 1 quote
)* // 0 or more repetition of non-capture group (multiple of 2 quotes will be even)
[^"]* // Finally 0 or more non-quotes
$ // Till the end (This is necessary, else every comma will satisfy the condition)
)
You can even type like this in your code, using (?x) modifier with your regex. The modifier ignores any whitespaces in your regex, so it's becomes more easy to read a regex broken into multiple lines like so:
String[] arr = str.split("(?x) " +
", " + // Split on comma
"(?= " + // Followed by
" (?: " + // Start a non-capture group
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" [^\"]* " + // 0 or more non-quote characters
" \" " + // 1 quote
" )* " + // 0 or more repetition of non-capture group (multiple of 2 quotes will be even)
" [^\"]* " + // Finally 0 or more non-quotes
" $ " + // Till the end (This is necessary, else every comma will satisfy the condition)
") " // End look-ahead
);
Why Split when you can Match?
Resurrecting this question because for some reason, the easy solution wasn't mentioned. Here is our beautifully compact regex:
"[^"]*"|[^,]+
This will match all the desired fragments (see demo).
Explanation
With "[^"]*", we match complete "double-quoted strings"
or |
we match [^,]+ any characters that are not a comma.
A possible refinement is to improve the string side of the alternation to allow the quoted strings to include escaped quotes.
Building upon #zx81's answer, cause matching idea is really nice, I've added Java 9 results call, which returns a Stream. Since OP wanted to use split, I've collected to String[], as split does.
Caution if you have spaces after your comma-separators (a, b, "c,d"). Then you need to change the pattern.
Jshell demo
$ jshell
-> String so = "123,test,444,\"don't split, this\",more test,1";
| Added variable so of type String with initial value "123,test,444,"don't split, this",more test,1"
-> Pattern.compile("\"[^\"]*\"|[^,]+").matcher(so).results();
| Expression value is: java.util.stream.ReferencePipeline$Head#2038ae61
| assigned to temporary variable $68 of type java.util.stream.Stream<MatchResult>
-> $68.map(MatchResult::group).toArray(String[]::new);
| Expression value is: [Ljava.lang.String;#6b09bb57
| assigned to temporary variable $69 of type String[]
-> Arrays.stream($69).forEach(System.out::println);
123
test
444
"don't split, this"
more test
1
Code
String so = "123,test,444,\"don't split, this\",more test,1";
Pattern.compile("\"[^\"]*\"|[^,]+")
.matcher(so)
.results()
.map(MatchResult::group)
.toArray(String[]::new);
Explanation
Regex [^"] matches: a quote, anything but a quote, a quote.
Regex [^"]* matches: a quote, anything but a quote 0 (or more) times , a quote.
That regex needs to go first to "win", otherwise matching anything but a comma 1 or more times - that is: [^,]+ - would "win".
results() requires Java 9 or higher.
It returns Stream<MatchResult>, which I map using group() call and collect to array of Strings. Parameterless toArray() call would return Object[].
You can do this very easily without complex regular expression:
Split on the character ". You get a list of Strings
Process each string in the list: Split every string that is on an even position in the List (starting indexing with zero) on "," (you get a list inside a list), leave every odd positioned string alone (directly putting it in a list inside the list).
Join the list of lists, so you get only a list.
If you want to handle quoting of '"', you have to adapt the algorithm a little bit (joining some parts, you have incorrectly split of, or changing splitting to simple regexp), but the basic structure stays.
So basically it is something like this:
public class SplitTest {
public static void main(String[] args) {
final String splitMe="123,test,444,\"don't split, this\",more test,1";
final String[] splitByQuote=splitMe.split("\"");
final String[][] splitByComma=new String[splitByQuote.length][];
for(int i=0;i<splitByQuote.length;i++) {
String part=splitByQuote[i];
if (i % 2 == 0){
splitByComma[i]=part.split(",");
}else{
splitByComma[i]=new String[1];
splitByComma[i][0]=part;
}
}
for (String parts[] : splitByComma) {
for (String part : parts) {
System.out.println(part);
}
}
}
}
This will be much cleaner with lambdas, promised!
Please see the below code snippet. This code only considers happy flow. Change the according to your requirement
public static String[] splitWithEscape(final String str, char split,
char escapeCharacter) {
final List<String> list = new LinkedList<String>();
char[] cArr = str.toCharArray();
boolean isEscape = false;
StringBuilder sb = new StringBuilder();
for (char c : cArr) {
if (isEscape && c != escapeCharacter) {
sb.append(c);
} else if (c != split && c != escapeCharacter) {
sb.append(c);
} else if (c == escapeCharacter) {
if (!isEscape) {
isEscape = true;
if (sb.length() > 0) {
list.add(sb.toString());
sb = new StringBuilder();
}
} else {
isEscape = false;
}
} else if (c == split) {
list.add(sb.toString());
sb = new StringBuilder();
}
}
if (sb.length() > 0) {
list.add(sb.toString());
}
String[] strArr = new String[list.size()];
return list.toArray(strArr);
}
Is there a way to increment a value in a map without needing a second variable?
for example, this doesn't work:
counts = new Map<string,integer>();
counts.put('month_total',0);
counts.put('month_total',counts.get['month_total']++);
it returns "Initial term of field expression must be a concrete SObject: MAP"
instead I needed to do:
counts = new Map<string,integer>();
counts.put('month_total',0);
integer temp = 0;
temp++;
counts.put('month_total',temp);
is there any way to increment without needing an extra variable?
Replace
counts.put('month_total',counts.get['month_total']++);
with
counts.put('month_total',counts.get('month_total')++);
List<String> lststrings = new List<String>{'key','wewe','sdsd','key','dfdfd','wewe'};
Map<String,Integer> mapval = new Map<String,Integer>();
Integer count = 1;
for(string str : lststrings){
IF(mapval.containsKey(str)){
mapval.put(str,mapval.get(str)+1);
}
else{
mapval.put(str,count);
}
}
system.debug('value of mapval'+mapval);
I have been working on sorting Arraycollection like ascending , descending the numeric list. Total length of my collection will go up to 100. Now I want to preform sort to nested data like this
Data Structure
Name : String
Categories : Array ["A","x or y or z","C"]
Categories array will have maximum 3 items , out of that three items the second item can have 3 different values either X or Y or Z. My result data looks like here
{"Mike" , ["A","x","C"]}
{"Tim" , ["A","y","C"]}
{"Bob" , ["A","x","C"]}
{"Mark" , ["A","z","C"]}
{"Peter" , ["A","z","C"]}
{"Sam" , ["A","y","C"]}
anyone please explain how to sort this type of data in a way showing all "x" first , "y" next and "z" at the last and vice a versa. Any help is really appreciated. Thanks Anandh. .
You can specify a compare function in your SortField like this:
var sortfield:SortField = new SortField("Categories");
sortfield.compareFunction = myCompare;
var sort:Sort = new Sort();
sort.fields = [sortfield];
yourCollection.sort = sort;
and your compare function:
function myCompare(a:Object, b:Object):int {
/*
return -1, if a before b
return 1, if b before a
return 0, otherwise
*/
}
or something like that.. and it's untested code :)
I have created a new property to the data structure called categoryOrder In the setter I did the following and Am using the categoryOrder for sorting - sortBy = categoryOrder;. I understand little hard coding is needed but still I believe this will reduce the number of comparisons when I use compareFunction. Anyone please valid this idea. Thanks!
public function set categories(data:ArrayCollection) :void
{
if(data != null)
{
_categories = data;
for each(var categorie:Object in data)
{
switch(categorie.categoryName)
{
case "x":{categoryOrder = 1;break;}
case "y":{categoryOrder = 2;break;}
case "z":{categoryOrder = 3;break;}
}
}
}
}
Data Structure
Name : String
Categories : Array ["A","x or y or z","C"]
categoryOrder : Number
can you please help me with this issue the String class does not have insert method it has only replace :( .
what I need is:
- if I have string "I stackoverflow"
- I need to insert "love " at index 2 to have "I love stackoverflow"
so what I need is insertAt(index, String)
thanks
You can build one of your own. Split the string and concatenate all characters before the index position with the characters of the string you want to insert and with the characters after the index.
eg:
String s = "I stackoverflow";
int index = s.indexOf(" ");
String toInsert = "love ";
String mys = s.substring(0, index) + toInsert + s.substring(index, s.length);
var s:String = "I StackOverflow";
var t:String = s.split(" ").join(" love ");
HI
I have a URL
var str:String = "conn=rtmp://server.com/service/&fileId=myfile.flv"
or
var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/"
The str might be like this, But i need to get the value of "conn" and "fileId" from the string.
how can i write a function for that.
I'm guessing that you're having trouble with the second '=' in the string. Fortunatly, ActionScript's String.Split method supports splitting on strings, so the following code should work:
var str:String = "conn=rtmp://server.com/service/&fileId=myfile.flv";
var conn:String = (str + "&").Split("conn=")[1].Split("&")[0];
and
var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/";
var fileId:String = (str + "&").Split("fileId=")[1].Split("&")[0];
Note: I'm appending a & to the string, in case the string didn't contain any url parameters beyond the one we're looking for.
var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/"
var fa:Array = str.split("&");
for(var i:uint=0;i<fa.length;i++)
fa[i] = fa[i].split('=');
That's how the "fa" variable be in the end:
fa =
[
["fileId","myfile.flv"],
["conn","rtmp://server.com/service/"]
]
var url:String = "fileId=myfile.flv&conn=rtmp://server.com/service/";
var strArray:Array = url.split(/=/);
trace(strArray[0]) //Just to test
returns an array, with the word 'conn or fileid' in index 0 - 2 (anything even), alternatives of 1, 3 is the information within.
Or was it something else you needed?