Tag: java
How to repeat string n times in java?
Suppose you have the string ‘qwe’ and you need to repeat string ‘n’ times. Here’s how to do it: Using java 8 stream api
1 2 3 4 5 |
String newString = Collections.nCopies(3, "qwe").stream().collect(Collectors.joining("")); String newString = Stream.generate(() -> "qwe").limit(3).collect(Collectors.joining()); String newString = String.join("", Collections.nCopies(n, s)); |
Using String.format
1 2 3 4 |
String s = "qwe"; int n = 3; //this line print 'qweqweqwe' String newString = String.format("%0" + n + "d", 0).replace("0", s); |
Let us examine …
Read MoreHow to convert ‘ArrayList to String array in Java
Using Collection.toArray method First way to convert arraylist is to use built-in for every java collection method toArray(T[] ts) For example:
1 2 3 4 5 6 |
List<String> animals = new ArrayList<String>(); //add some elements animals.add("cat"); animals.add("dog"); String[] stringArray = animals.toArray(new String[0]); System.out.println(Arrays.toString(stringArray)); |
The toArray() method without any argument returns Object[]. So you have …
Read MoreHow to compare strings in Java?
Test strings equality == tests object references, .equals() tests the string values. Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical …
Read MoreJShell – Java 9 interpreter (REPL) – Getting Started
What is REPL Many languages include tools (sometimes called REPL) for statements interpretation. Using these tools you can test code snippets quickly without creating full project. For example python. Creating …
Read MoreHow to create a simple Java project with Maven
In this article we will learn how to create a simple java project with Maven. We also review project structure and the commands for building the project.
Read MoreConnect to MySQL database with JDBC
In this article, we’ll look at connecting the Mysql database, executing queries, and getting / processing the result.
Read More