Just to summarize all the ways of list creation in Java.
Taken from: http://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java
JDK
1. List listA = new ArrayList<string>();
2. List listB = Arrays.asList("A", "B", "C")
Guava
1. List names = Lists.newArrayList("Mike", "John", "Lesly");
2. List chars = Lists.asList("A","B", new String [] {"C", "D"});
Immutable List
1. Collections.unmodifiableList(new ArrayList<string>(Arrays.asList("A","B")));
2. ImmutableList.builder() // Guava
.add("A")
.add("B").build();
3. ImmutableList.of("A", "B"); // Guava
4. ImmutableList.copyOf(Lists.newArrayList("A", "B", "C")); // Guava
Empty immutable List
1. Collections.emptyList();
2. Collections.EMPTY_LIST;
List of Characters
1. Lists.charactersOf("String") // Guava
2. Lists.newArrayList(Splitter.fixedLength(1).split("String")) // Guava
List of Integers
Ints.asList(1,2,3); // Guava
A little java collections memo:
Showing posts with label Simple way. Show all posts
Showing posts with label Simple way. Show all posts
Wednesday, November 12, 2014
Thursday, July 17, 2014
Simple jUnit which compares two files
I have a piece of code that generates some file an I need to cover this code with unit-tests. With a sample file I may check my code whether the file was generated correctly. Maven creates src/test/resources directory automatically, there is no better place to put the sample file.
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way
Don't need to compare files line-by-line, just use commons-io util
Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way
URL url = this.getClass().getResource("/tables/sample.xml"); File sampleFile = new File(url.getFile());
Don't need to compare files line-by-line, just use commons-io util
FileUtils.contentEquals(file1, file2);
Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:
private boolean isFileDataEqual(File target) throws IOException { URL url = this.getClass().getResource("/tables/sample.xml"); File sampleFile = new File(url.getFile()); return FileUtils.contentEquals(target, sampleFile); }
Thursday, June 12, 2014
Java Stack Implementation
This is a the simplest example of stack implementation in Java. 'Must-do' exercise for a junior software guys
public class Stack<T> { T[] items; int index = 0; @SuppressWarnings("unchecked") public Stack(int size) { items = (T[]) new Object[size]; } public void push(T item) { if (index < items.length) { items[index] = item; ++index; } else { throw new StackOverflowError( "Element couldn't be added: stack is full. "); } } public T pop() { if (index > 0) { --index; T value = items[index]; items[index] = null; return value; } else { return null; } } public boolean isFull() { return index == items.length; } public boolean isEmpty() { return index == 0; } public String toString() { /* Optional */ StringBuilder sb = new StringBuilder(); for (int iter = 0; iter < items.length; ++iter) { sb.append(items[iter] + " "); } return sb.toString(); } }
Wednesday, March 19, 2014
Override toString() method with ToStringBuilder
One fancy way of toString method implementation is in using of ToStringBuilder class.
With this class you don't need to specify all the objects for output and you get a flexible mechanism of building and formatting your object's string representaion.
Just a couple of strings:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
import org.apache.commons.lang.builder.ToStringBuilder;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
Got it!
See more information on official reference documntation: http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/builder/ToStringBuilder.html
Another very good article about ToStringBuilder by Lokesh Gupta (with nice samples)
http://howtodoinjava.com/2013/06/03/how-to-override-tostring-effectively-with-tostringbuilder/
With this class you don't need to specify all the objects for output and you get a flexible mechanism of building and formatting your object's string representaion.
Just a couple of strings:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
import org.apache.commons.lang.builder.ToStringBuilder;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
Got it!
See more information on official reference documntation: http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/builder/ToStringBuilder.html
Another very good article about ToStringBuilder by Lokesh Gupta (with nice samples)
http://howtodoinjava.com/2013/06/03/how-to-override-tostring-effectively-with-tostringbuilder/
Tuesday, December 17, 2013
Converting m/s to km/h
One of the easiest way to convert speed from m/s to km/h.
tmp = v * 4
result = tmp - (tmp * 0.1)
Where v - speed given (meters per a second)
For example: need to convert 65 m/s to km/h. 65 *4 = 260; 260 - 26 = 234 km/h. 65 m/s = 234 km/h
Let's check it! 1 m/s = 60 meters per minute = 3600 meters per hour = 3.6 km/h. Given speed is 65 m/s, 65 * 3.6 = 234 km/h. Our result is correct.
tmp = v * 4
result = tmp - (tmp * 0.1)
Where v - speed given (meters per a second)
For example: need to convert 65 m/s to km/h. 65 *4 = 260; 260 - 26 = 234 km/h. 65 m/s = 234 km/h
Let's check it! 1 m/s = 60 meters per minute = 3600 meters per hour = 3.6 km/h. Given speed is 65 m/s, 65 * 3.6 = 234 km/h. Our result is correct.
Subscribe to:
Posts (Atom)

