Last active
March 4, 2022 16:21
-
-
Save cesarvasconcelos/3b2f1bd2def7ccf47d60b842ffee5756 to your computer and use it in GitHub Desktop.
Revisions
-
cesarvasconcelos revised this gist
Apr 8, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -72,7 +72,8 @@ public static void main( String[] args ) // problema: trim não conhece os unicode whitespace code points String strEspacos2 = "\n\t\t java \u2005"; strEspacos2.trim().equals( "java" ); // false! ==> "java " há espaço "\u2005" que sobrou (use strip() :) ) strEspacos2.isBlank(); // false!, mas "\n\t\t \u2005".isBlank(); // true! // Java 11 -> String::strip() (is unicode whitespace aware) // Returns a string whose value is this string, with all leading and trailing white space removed. -
cesarvasconcelos revised this gist
Apr 8, 2019 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,6 +62,22 @@ public static void main( String[] args ) joiner.merge( joiner2 ); // joiner original irá receber novos itens e ajustar sufixo automati/te System.out.println( joiner ); // {Recife,Campina,Patos,Natal,Salvador,Aracajú} // Java 11 -> String::repeat() String repeat = "+ ".repeat( 16 ); // repeat ==> "+ + + + + + + + + + + + + + + + " // Java 11 -> String::isBlank() // true if the string is empty or contains only whitespace codepoints, otherwise false String strEspacos = " "; strEspacos.trim().equals( "" ); // true, mas trim é limitado... veja problema abaixo // problema: trim não conhece os unicode whitespace code points String strEspacos2 = "\n\t\t java \u2005"; strEspacos2.trim().equals( "java" ); // false! ==> "java " há espaço "\u2005" que sobrou (use strip() :) ) strEspacos2.isBlank(); // true! :) // Java 11 -> String::strip() (is unicode whitespace aware) // Returns a string whose value is this string, with all leading and trailing white space removed. strEspacos2.strip(); // ==> "java" strip conhece os unicode whitespace code points :) // Java 11 -> String::lines() String multilines = "Primeira linha\nSegunda linha\nTerceira linha"; multilines.lines().forEach( System.out::println ); -
cesarvasconcelos renamed this gist
Mar 20, 2019 . 1 changed file with 22 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import java.util.StringJoiner; import java.util.stream.IntStream; public class StringMethodsJava { public static void main( String[] args ) { // no Java SE 8, a classe String recebeu atenção especial. @@ -61,5 +61,26 @@ public static void main( String[] args ) joiner.merge( joiner2 ); // joiner original irá receber novos itens e ajustar sufixo automati/te System.out.println( joiner ); // {Recife,Campina,Patos,Natal,Salvador,Aracajú} // Java 11 -> String::lines() String multilines = "Primeira linha\nSegunda linha\nTerceira linha"; multilines.lines().forEach( System.out::println ); // Java 12 -> String::indent String mensagem = "Uma linha".indent( 4 ); // "____Uma linha\n" note o \n adicionado no final multilines.indent( 3 ).lines().forEach( System.out::println ); // "Primeira linha\n" // "Segunda linha\n" // "Terceira linha\n" // " Primeira linha\n" // " Segunda linha\n" // " Terceira linha\n" // Java 12 -> String::transform // transforma a String em um novo objeto mediante função // transform(Function<String,R> function) multilines.transform( str -> str.replace( "\n", "#" ) ) .transform( String::toUpperCase ) .lines() .forEach( System.out::println ); // PRIMEIRA LINHA#SEGUNDA LINHA#TERCEIRA LINHA } } -
cesarvasconcelos revised this gist
Dec 30, 2018 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,14 +29,16 @@ public static void main( String[] args ) // alternativa 2: usar StringBuilder do Java 5, que NÃO é synchronized +rápida :) StringBuilder sbu = new StringBuilder( ); s3 = sbu.append( s1 ).append( " " ).append( s2 ).toString(); // a partir do Java 7, a JVM ficou esperta e compila todas as concatenações com ´+´ // nos bastidores usando StringBuilder, para fins de otimização // para desenvolvedor não se preocupar com concatenações com ´+´ // no Java SE 8, ficou ainda mais fácil: pode-se agrupar valores String // com delimitadores mais facilmente com o método // estático String.join() ou usando a classe StringJoiner StringJoiner stringJoiner = new StringJoiner( " " ); s3 = stringJoiner.add( s1 ).add( s2 ).toString(); s3 = String.join( " ", s1, s2 ); // faz a mesma coisa que o anterior // outro exemplo: List<String> lista = Arrays.asList( "Recife", "Campina", "Patos", "Natal" ); -
cesarvasconcelos revised this gist
Dec 30, 2018 . 1 changed file with 32 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,44 @@ import java.util.Arrays; import java.util.List; import java.util.StringJoiner; import java.util.stream.IntStream; public class StringJoinerJava8 { public static void main( String[] args ) { // no Java SE 8, a classe String recebeu atenção especial. // é possível fazer bridge entre String e Stream API // para criar uma Stream a partir de uma String String s = "Java 8 rocks!"; IntStream stream = s.chars(); // IntFunction accepts an int-valued argument and produces a result // sintaxe: mapToObj(IntFunction<? extends T> mapper) stream.mapToObj( value -> (char) value ) .map( Character::toUpperCase ) .forEach( System.out::print ); // antes do Java 7, concatenação de Strings era uma tarefa onerosa // havia criação/remoção de múltiplas strings intermediárias, String s1 = "Hello"; String s2 = "World"; String s3 = s1 + " " + s2; // não era eficiente antes do Java 7 // alternativa 1: usar StringBuffer---mas StringBuffer é synchronized +lenta :( StringBuffer sb = new StringBuffer( ); s3 = sb.append( s1 ).append( " " ).append( s2 ).toString(); // alternativa 2: usar StringBuilder do Java 5, que NÃO é synchronized +rápida :) StringBuilder sbu = new StringBuilder( ); s3 = sbu.append( s1 ).append( " " ).append( s2 ).toString(); // a partir do Java 7, a JVM ficou esperta e compilará as concatenações com ´+´ // nos bastidores usando StringBuilder, para fins de otimização // no Java SE 8, ficou ainda mais fácil: pode-se agrupar valores String // com delimitadores mais facilmente com o método // estático String.join() ou usando a classe StringJoiner StringJoiner stringJoiner = new StringJoiner( " " ); s3 = stringJoiner.add( s1 ).add( s2 ).toString(); // outro exemplo: List<String> lista = Arrays.asList( "Recife", "Campina", "Patos", "Natal" ); String agrupada = String.join( "#", lista ); System.out.println( agrupada ); // Recife#Campina#Patos#Natal @@ -30,4 +60,4 @@ public static void main( String[] args ) joiner.merge( joiner2 ); // joiner original irá receber novos itens e ajustar sufixo automati/te System.out.println( joiner ); // {Recife,Campina,Patos,Natal,Salvador,Aracajú} } } -
cesarvasconcelos created this gist
Nov 26, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ import java.util.Arrays; import java.util.List; import java.util.StringJoiner; public class StringJoinerJava8 { public static void main( String[] args ) { // no Java SE 8, pode-se agrupar valores String // com delimitadores mais facilmente com o método // estático String.join() ou usando a classe StringJoiner List<String> lista = Arrays.asList( "Recife", "Campina", "Patos", "Natal" ); String agrupada = String.join( "#", lista ); System.out.println( agrupada ); // Recife#Campina#Patos#Natal // StringJoiner class. Veja delimitador: "," prefixo: "{" sufixo: "}" StringJoiner joiner = new StringJoiner( ",", "{", "}" ); joiner.setEmptyValue( "{}" ); // o que será impresso se StringJoiner estiver vazia System.out.println( joiner ); // vai sair {} // adicionando itens na StringJoiner // joiner.add( "Recife" ).add( "Campina" ).add( "Patos" ).add( "Natal" ); lista.forEach( str -> joiner.add( str ) ); System.out.println( joiner ); // {Recife,Campina,Patos,Natal} // merging StringJoiners StringJoiner joiner2 = new StringJoiner( "," ); joiner2.add( "Salvador" ).add( "Aracajú" ); joiner.merge( joiner2 ); // joiner original irá receber novos itens e ajustar sufixo automati/te System.out.println( joiner ); // {Recife,Campina,Patos,Natal,Salvador,Aracajú} } }