Convert Python to Java 8 -


i’m working in project use java 8, project geographic information , work this. have done part of work in python, , i’m translating part did in python java 8, in python use lines bellow convert coordinates in google format postgis format:

s1 = tuple(value.split(" ")) s2 = zip(s1[1::2], s1[::2]) 

for example:

i have entrance like: value = "11.12345679 12.987655 11.3434454 12.1223323" , on python code above changes de entrance to: s2 = "12.987655 11.12345679 12.1223323" , on.

changing position of each coordinate pair, each entrance have thousands of coordinates. same effect java (before java 8): using knowledge of java (acquired before java 8) need that:

try {     string result = "", right = "", left = "";     string[] txt = str.split(" ");     (int = 0; < txt.length; += 2) {         right = txt[i];         left = txt[i + 1];         result += "," + left + " " + right;     }     return result.substring(1); } catch (arrayindexoutofboundsexception e) {     return null; } 

i execute java code above thousands of times, question is: java 8 has new way code above more python ?

my motivation ask question because came across news java 8:

list<string> somelist = new arraylist<>(); // add elements somelist.add("generic (1.5)"); somelist.add("functional (8)");  // open stream somelist.stream() // turn texts in upper case .map(string::touppercase) // loop elemnst in upper case .foreach(system.out::println); 

updating:

the solution of jean-françois savard perfect using java 8 asked, thank jean-francois savard

string str = "11.12345679 12.987655 11.3434454 12.1223323 11.12345679 12.987655 11.3434454 12.1223323"; string[] strs = str.split(" ");  str = intstream.range(0, strs.length) .filter(i -> % 2 == 0) .maptoobj(i -> strs[i + 1] + " " + strs[i]) .collect(collectors.joining(",")); system.out.println(str); >> 12.987655 11.12345679,12.1223323 11.3434454,12.987655 11.12345679,12.1223323 11.3434454 

the solution shown vampire , tukayi fit in problem, lot guys

string str = "11.12345679 12.987655 11.3434454 12.1223323 11.12345679 12.987655 11.3434454 12.1223323"; str = str.replaceall("([^\\s]+) ([^\\s]+)(?: |$)", ",$2 $1").substring(1); system.out.println(str); 

define following in class precompile regex pattern

private static final pattern pattern = pattern.compile("([^ ]++) ([^ ]++)(?: |$)"); 

then in method use

if ((new stringtokenizer(str, " ").counttokens() % 2) == 1) {     return null; } return pattern.matcher(str).replaceall(",$2 $1").substring(1); 

to same result in original code.


if depend on using streams why-o-ever, here streams solution

string[] strs = str.split(" "); return intstream.range(0, strs.length)                 .filter(i -> % 2 == 0)                 .maptoobj(i -> strs[i + 1] + " " + strs[i])                 .collect(collectors.joining(",")); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -