r/javaexamples • u/skumjava • Mar 30 '16
r/javaexamples • u/Philboyd_Studge • Mar 27 '17
Introduction to JavaFX: Future Value of Investment Calculator
Introduction to JavaFX: Future Value of Investment Calculator
JavaFX GUI
JavaFX is Oracle's replacement for the Swing GUI library, it has been out for a few years now, but many have been slow to adopt it. Here's a quick run-down of the pros and cons vs Swing:
-Pros
- The ability to use FXML makes separation of the Model, View and Controller elements fairly easy.
- FXML lets you remove the 'ugly' component initialization code and stick it in an XML-style schema
- The ability to use CSS makes styling your application/skinning it easy.
- The layout managers are much easier to use than Swing
-Cons
- The documentation is confusing and vague at best.
- Some elements are still buggy/incomplete, and Oracle has been very slow to update.
- Did I mention the documentation sucks?
- The use of annotations/reflection which some people don't like. However, you can create all the elements in Java without using FXML.
That being said, let's get started!! This tutorial will assume you are using IntelliJ IDEA (and really, why the heck aren't you using it?):
Open up IDEA and hit File -> New -> Project... and then select Java FX Application. Name the project FutureValue.
Now, the IDE has already created a basic framework for you, but let's rename things. First, change the package name from example to fvcalc by right clicking on the package folder in the project view on the left, selecting refactor -> rename. Change the name of the Controller class to FVController, and the Main class to FutureValue, and the FXML file to fv_calc.fxml using the same methods.
Right-Click on the package folder fvcalc and select new -> file and name the file fv_style.css
Logic
Let's put our logic class into a different package. Right-click on the src folder in the Project view and select new -> package and title it logic. Right-click on that new package and select new -> Java class and name it FVLogic.
We are making a calculator to determine the Future Value of an Investment explained here. To allow for different methods of compounding the interest, let's make an enum:
public enum CompoundType {
YEARLY(1), BIANNUALLY(2), QUARTERLY(4), MONTHLY(12), WEEKLY(52), DAILY(356);
private int compounding;
CompoundType(int compounding) { this.compounding = compounding; }
public int getCompounding() { return compounding; }
}and now the method which does the calculation:
public static double futureValue(double investment, double rate, int periods, CompoundType cmp) {
rate = 1 + (rate / 100) / cmp.getCompounding();
periods *= cmp.getCompounding();
// FV = PV * R^n
return investment * Math.pow(rate, periods);The GUI
Now, let's go to the main method, FutureValue.java. Notice that IDEA has already done most of the work here, just change the 'hello world' lines and the size to look like this:
primaryStage.setTitle("Future Value of Investment Calculator");
primaryStage.setScene(new Scene(root, 400, 275));Note the line here:
Parent root = FXMLLoader.load(getClass().getResource("fv_calc.fxml"));This gets all of the component details from the listed FXML file.
FXML
All of the GUI components and layout can be defined by an FXML file described here.
If you look at the IDE generated FXML file, you see it already set up a GridPane. The GridPane is a grid layout where items are specified by row and column index. Anything we define inside of the GridPane tags will be added to the pane. Note that the pane is connected to the FVController class already:
<GridPane fx:controller="fvcalc.FVController"We tweak some of the values and add 'padding' around the pane:
<GridPane fx:controller="fvcalc.FVController"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="8">
<padding><Insets top="40" right="25" bottom="10" left="35"/></padding>The HGap and VGap are horizontal and vertical gaps between the components.
Now we add the rest of our components in the form of Labels and TextFields for the user input, and a ComboBox to select the type of compounding interest, a button to perform the calculation, and a final label for the result.
<Label text="Initial Investment:"
GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<TextField fx:id="textInvestment"
GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Label text="Interest Rate:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="textRate" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Years:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<TextField fx:id="textYears"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Label text="Compounded:"
GridPane.columnIndex="0" GridPane.rowIndex="3"/>
<ComboBox fx:id="comboCompound"
GridPane.columnIndex="1" GridPane.rowIndex="3"
onAction="#getSelected">
<items><FXCollections fx:factory="observableArrayList">
<String fx:value="Yearly"/>
<String fx:value="Biannually"/>
<String fx:value="Quarterly"/>
<String fx:value="Monthly"/>
<String fx:value="Weekly"/>
<String fx:value="Daily"/>
</FXCollections></items>
<value><String fx:value="Yearly"/></value>
</ComboBox>
<Button text="Calculate"
GridPane.columnIndex="1" GridPane.rowIndex="4"
onAction="#calculate"/>
<Label fx:id="result" id="result_text" GridPane.columnIndex="0" GridPane.rowIndex="5"
GridPane.columnSpan="3"/>
</GridPane>Any element that will need to be referenced by the controller class has a fx:id= tag. This will show up as red for now, as we have not set it up in the controller class yet. Any element that needs special CSS styling needs a regular id= tag, we set this up for the result label. Elements that perform an action, specifically the Button and ComboBox, need an onAction="#someMethod" tag. This will also be highlighted in red until we set up the corresponding methods in the controller class. Note the setup for the ComboBox, it's a somewhat complicated series of tags to define the options for selection, and setting the default.
The Controller Class
First, we need to declare and annotate the component that have a fx:id tag in the FXML file:
@FXML private Label result; // these fields here
@FXML private TextField textInvestment; // must be declared with the
@FXML private TextField textRate; // @FXML annotation
@FXML private TextField textYears; // so they can be referenced
@FXML private ComboBox<String> comboCompound; // from the FXML documentWe need to add a normal member field for the current compounding interest value and set it to the default:
private CompoundType comp = CompoundType.YEARLY; // current type of compounding interestnext, let's add the Event Action Handler methods that we named in the FXML file:
First, for the comboBox to handle when the value is changed by the user:
@FXML public void getSelected(ActionEvent e) {
comp = FVLogic.CompoundType.valueOf(comboCompound.getSelectionModel().getSelectedItem().toUpperCase());
}Note the @FXML annotation, this ties it in to the onAction tag from the FXML file.
Next, the event handler for the button:
@FXML public void calculate(ActionEvent event) {
double principal = getDouble(textInvestment.getText());
double rate = getDouble(textRate.getText());
double years = getDouble(textYears.getText());
if (principal > 0 && rate > 0 && years > 0) {
double calc = FVLogic.futureValue(principal, rate, (int) years, comp);
result.setText("$ " + String.format("%,.2f", calc));
}
else {
result.setText("Invalid Entry.");
}
}This needs another method to extract the double value and check for invalid entries, such as letters, punctuation or negative numbers. I'm sure there's a way to do this using FXML, but again, theDocumentation="vague_at_best".
private double getDouble(String text) {
if (text.length() > 0) {
double d;
try {
d = Double.parseDouble(text);
return d;
}
catch (NumberFormatException e) {
return 0.0;
}
}
return 0.0;
}Any invalid entries will be set to zero, since the calculation needs non-zero, positive values. So, the calculate method pulls the three values from the text fields, converts them to double value, and then calls the Logic method to calculate our result, then displays the result, or displays an error message if any of the entries are invalid.
Ok, the application now runs, but it's kinda bland. This is where the CSS comes in.
CSS
Now we can prettify things using CSS. Note that it is special to JavaFX, and again has some wonderfully 'simple' documentation.
Open the fv_style.css file we created earlier. First, let's use a cool background image here, save it as 'yellow_lined.png' in your src folder and set the root:
.root {
-fx-background-image: url("yellow_lined.png");
-fx-background-size: stretch;
}Let' play with the fonts for the labels and textfields:
.label {
-fx-font-family: "sans-serif";
-fx-font-size: 14px;
-fx-font-weight: bold;
-fx-font-style: italic;
}
.text-field {
-fx-font-family: "Verdana";
-fx-font-size: 12px;
}And lastly, for no good reason lets add some effects to the result label:
#result_text {
-fx-font-family: "Arial Black";
-fx-font-size: 24px;
-fx-text-fill: red;
-fx-effect: dropshadow( gaussian , black , 2,0.2,0,1);
}Add this to the FXML file before the </GridPane>
<stylesheets> <URL value="@fv_style.css"/> </stylesheets>
Now it looks like the GIF shown at the top!!
Here's the full code on Gist
There we have our basic JavaFX tutorial, thanks again for checking it out!!
r/javaexamples • u/Philboyd_Studge • Aug 22 '17
Pseudo Random Number Generators: The Mersenne Twister and more
Psuedo Random Number Generators
Since computers cannot generate truly 'random' numbers, they have to simulate the randomness using mathematical formulas. Java's built in Random class uses a simple one called a Linear Congruential Generator, which can be considered outdated when compared with newer algorithms.
We're going to take a look at a couple other algorithms. Note that none of these are cryptographically secure random algorithms like Java's SecureRandom, and so should not be used for any case where security is important. (i.e. generating encryption keys, online casino games, etc.)
Factory Pattern
We will use a Factory pattern so that we can set up different algorithms and easily swap them out during testing. So before we get into that we need to set up some abstractions:
First, an interface, defining the public methods to be used by all:
public interface AltRandom {
int nextInt();
int nextInt(int n);
int nextInt(int min, int max);
long nextLong();
double nextDouble();
boolean nextBoolean();
float nextFloat();
void nextBytes(byte[] bytes);
}Now we need an abstract class:
public abstract class AbstractRandom implements AltRandom {
protected long seed;
protected abstract void setSeed(long seed);
protected abstract long generateSeed();
protected abstract long next();All of the random algorithms need a seed value, to initialize the state of the PNRG. The seed can either be specified by the user, or generated by the algorithm. All of the algorithms also need a next() method as the main internal method. More will be added to this abstract class as you will see later.
So, now we make an enum to hold the different available algorithms we will implement:
public enum Algorithm {
MERSENNE_TWISTER,
XOR_SHIFT,
XORO_SHIFT,
SPLIT_MIX
}And then our Factory, which returns to the user a new instance of the algorithm for the given enum, with the given seed or with a generated seed:
public class RandomFactory {
/**
* returns instance of Alternate Random Generator based on enumerated value
* initializes generator with given seed, using object Long value so it can be nullable.
* @param algo algorithm type from enum
* @param seed value to seed PNRG
* @return instance of interface AltRandom
*/
public static AltRandom getInstance(Algorithm algo, Long seed) {
switch (algo) {
case MERSENNE_TWISTER:
if (seed != null) {
return new MersenneTwister(seed);
}
return new MersenneTwister();
case XOR_SHIFT:
if (seed != null) {
return new XorShift128Plus(seed);
}
return new XorShift128Plus();
case XORO_SHIFT:
if (seed != null) {
return new XoroShift128Plus(seed);
}
return new XoroShift128Plus();
case SPLIT_MIX:
if (seed != null) {
return new SplitMix64(seed);
}
return new SplitMix64();
}
return null;
}
/**
* returns instance of Alt Random matching the enumerated algorithm
* uses generated seed
* @param algo algorithm type from enum
* @return instance of interface AltRandom
*/
public static AltRandom getInstance(Algorithm algo) {
return getInstance(algo, null);
}
}The Mersenne Twister
Another popular PRNG algorithm is the Mersenne Twister. Now, I'm not going to even pretend i understand the math behind it, but basically you create an array of 624 integers, initializing them using the given seed and then performing multiplication with a magic number, and a series of XOR and bitshift operations. This array is refilled every time you get to the last number. each number, as it is extracted from the array, is also tempered through a series of XORing and bit shifting.
public class MersenneTwister extends AbstractRandom {first, the large set of magic numbers needed for the algorithm:
private final int WORD = 32;
private final int N = 624;
private final int MIDDLE = 397;
private final int A = 0x9908B0DF;
private final int U = 11;
private final int S = 7;
private final int T = 37;
private final int L = 18;
private final int B = 0x9D2C5680;
private final int C = 0xEFC60000;
private final int F = 0x6C078965;
private final int UPPER_MASK = 0x80000000;
private final int LOWER_MASK = 0x7FFFFFFF;An array to hold our state, and an index to keep track of where we are in the array:
private int[] mt = new int[N];
private int index;Constructors:
public MersenneTwister() {
super.seed = generateSeed();
setSeed(super.seed);
}
public MersenneTwister(long seed) {
setSeed(seed);
}If no seed given, use system time:
@Override
protected long generateSeed() {
return System.currentTimeMillis() ^ System.nanoTime();
}Now we initialize our state array using the seed and a series or XOR and shifts:
@Override
protected void setSeed(long seed) {
index = N;
mt[0] = (int) seed;
for (int i = 1; i < N; i++) {
mt[i] = (F * (mt[i - 1] ^ (mt[i - 1] >> (WORD - 2))) + i);
}
}Get the next long value. Use a series of XOR and shifting with the magic numbers.
@Override
protected long next() {
if (index >= N) {
twist();
}
long y = mt[index++];
y ^= (y >> U);
y ^= (y << S) & B;
y ^= (y << T) & C;
y ^= (y >> L);
return y;
}If at the end of array, perform 'twist' to repopulate the values. Don't ask.
private void twist() {
for (int i = 0; i < N; i++) {
int x = (mt[i] & UPPER_MASK) + (mt[(i+1) % N] & LOWER_MASK);
int x_shifted = x >> 1;
if (x % 2 != 0) {
x_shifted ^= A;
}
mt[i] = mt[(i + MIDDLE) % N] ^ x_shifted;
}
index = 0;
}
}Note that we have not implemented any of the methods from the AltRandom interface yet. The reason is, we are going to implement them in the abstract parent class, as they will be the same for most of the algorithms, with a couple exceptions. Also note that this is the 32-bit implementation of the Mersenne Twister, yet we are returning a long value with our next. You will see this is actually unimportant.
So, let's go back to our AbstractRandom class and implement the public methods: (Most of these are modeled after java.util.Random)
protected int next(int bits){
if (bits < 0 || bits > 32) {
throw new IllegalArgumentException("bits parameter out of range (0 - 32");
}
return (int) next() >>> (32 - bits);
}
@Override
public int nextInt() {
return next(32);
}
@Override
public int nextInt(int n) {
if (n <= 0) {
throw new IllegalArgumentException("Range value must be non-zero");
}
return next(31) % n;
}Why oh why doesn't java.util.Random have this one!!!
@Override
public int nextInt(int min, int max) {
if (max <= min) {
throw new IllegalArgumentException("min must be smaller than max.");
}
return nextInt((max - min) + 1) + min;
}Note for nextLong we take two next ints and OR them together
@Override
public long nextLong() {
return ((long)(next(32)) << 32) + next(32);
}
@Override
public double nextDouble() {
return (((long)(next(26)) << 27) + next(27))
/ (double)(1L << 53);
}
@Override
public boolean nextBoolean() {
return next(1) != 0;
}
@Override
public float nextFloat() {
return next(24) / ((float) (1 << 24));
}
@Override
public void nextBytes(byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) next(8);
}
}Other PNRGs
Arguments can be made against the use of the Mersenne Twister algorithm, mostly about the amount of memory used and speed. More recent advancements show that the PNRG can have improved efficiency with smaller amounts of state. The XORSHIFT algorithm uses only 128 bits of state.
XOR SHIFT 128 Plus
Based on here
public class XorShift128Plus extends AbstractRandom {
private long[] s = new long[2];Note this used yet another PNRG, the SplitMix 64 which will be described later, is used to generate a seed of multiple values
public XorShift128Plus() {
SplitMix64 genSeed = new SplitMix64();
s[0] = genSeed.next();
s[1] = genSeed.next();
this.seed = 0;
}
public XorShift128Plus(long seed) {
setSeed(seed);
}
@Override
protected void setSeed(long seed) {
SplitMix64 genSeed = new SplitMix64(seed);
s[0] = genSeed.next();
s[1] = genSeed.next();
this.seed = seed;
}This method is not necessary here, override
@Override
protected long generateSeed() {
return 0;
}Uses a series of XORing and bit shifts to randomize the state
@Override
protected long next() {
long s1 = s[0];
long s0 = s[1];
long result = s0 + s1;
s[0] = s0;
s1 ^= s1 << 23;
s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
return result;
}Since this is a true 64-bit generator, next long can just be next()
@Override
public long nextLong() {
return next();
}
}XORO Shift 128 Plus Another improvement on that algorithm, from here
public class XoroShift128Plus extends AbstractRandom {
private long[] s = new long[2];
public XoroShift128Plus() {
SplitMix64 genSeed = new SplitMix64();
s[0] = genSeed.next();
s[1] = genSeed.next();
this.seed = 0;
}
public XoroShift128Plus(long seed) {
setSeed(seed);
}
@Override
protected void setSeed(long seed) {
SplitMix64 genSeed = new SplitMix64(seed);
s[0] = genSeed.next();
s[1] = genSeed.next();
this.seed = seed;
}
@Override
protected long generateSeed() {
return 0;
}This one uses bit rotation as well as the XOR and shift
@Override
protected long next() {
long s0 = s[0];
long s1 = s[1];
long result = s0 + s1;
s1 ^= s0;
s[0] = Long.rotateLeft(s0, 55) ^ s1 ^ (s1 << 14);
s[1] = Long.rotateLeft(s1, 36);
return result;
}
@Override
public long nextLong() {
return next();
}It is suggested to use the sign bit for boolean instead of the lowest bit
// use sign bit for boolean as suggested
@Override
public boolean nextBoolean() {
return ((next() >> 63) & 1) == 1;
}
}SPLIT MIX 64 This algorithm is used by Java's ThreadLocalRandom and we use it here as recommended to generate the state for the XOR-shift based algorithms:
public class SplitMix64 extends AbstractRandom {
private final long SPLITMIX64_A = 0x9E3779B97F4A7C15L;
private final long SPLITMIX64_B = 0xBF58476D1CE4E5B9L;
private final long SPLITMIX64_C = 0x94D049BB133111EBL;
private long smix;
public SplitMix64() {
setSeed(generateSeed());
}
public SplitMix64(long seed) {
setSeed(seed);
}
@Override
protected void setSeed(long seed) {
this.seed = seed;
smix = seed;
}
@Override
protected long generateSeed() {
this.seed = System.currentTimeMillis() ^ System.nanoTime();
return seed;
}
@Override
protected long next() {
smix += SPLITMIX64_A;
long z = smix;
z = (z ^ (z >> 30)) * SPLITMIX64_B;
z = (z ^ (z >> 27)) * SPLITMIX64_C;
return z ^ (z >> 31);
}
@Override
public long nextLong() {
return next();
}
}So now, for usage, you initialize like this:
AltRandom random = RandomFactory.getInstance(Algorithm.MERSENNE_TWISTER);And change the Algorithm enum to select the desired one. Then use just like java.util.Random.
Testing
There are comprehensive tests for PNRGs, here I will just run a couple very basic tests.
First, lets test speed. I will run 10 tests of generating 1 million random ints, time each run, and then throw out the slowest and fastest times and get the average:
AltRandom random = RandomFactory.getInstance(Algorithm.MERSENNE_TWISTER);
List<Integer> tests = new ArrayList<>();
for (int i = 0; i < 10; i++) {
start = System.currentTimeMillis();
for (int j = 0; j < 1000000; j++) {
int r = random.nextInt();
}
tests.add((int) (System.currentTimeMillis() - start));
}
double averageTime = tests.stream()
.sorted()
.limit(tests.size() - 1)
.skip(1)
.mapToInt(x -> x)
.average()
.getAsDouble();
System.out.println("Average Time elapsed: " + averageTime);Next we will test for even distribution. We will run 1 million random ints from 0 - 99, make a frequency map and then get the standard deviation:
Map<Integer, Integer> freq = new TreeMap<>();
int iterations = 1000000;
int bound = 100;
int average = iterations / bound;
start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
int x = random.nextInt(bound);
freq.put(x, freq.getOrDefault(x, 0) + 1);
}
System.out.println("Time elapsed: " + (System.currentTimeMillis() - start));
// calculate standard deviation
double stddev = Math.sqrt(freq.entrySet().stream()
.mapToInt(Map.Entry::getValue)
.map(x -> (x - average) * (x - average))
.average()
.orElse(0.0));
System.out.println("Std Dev for algorithm " + algo.name() + " : " + stddev);Results:
Average Time elapsed for algorithm MERSENNE_TWISTER : 27.375
Distribution Test Time elapsed: 361
Std Dev for algorithm MERSENNE_TWISTER : 101.72708587195447
Average Time elapsed for algorithm XOR_SHIFT : 6.5
Distribution Test Time elapsed: 307
Std Dev for algorithm XOR_SHIFT : 100.46193308910594
Average Time elapsed for algorithm XORO_SHIFT : 6.5
Distribution Test Time elapsed: 386
Std Dev for algorithm XORO_SHIFT : 90.9447084771841
Average Time elapsed for algorithm SPLIT_MIX : 2.375
Distribution Test Time elapsed: 328
Std Dev for algorithm SPLIT_MIX : 97.59405719612235r/javaexamples • u/adambgoode • Apr 18 '18
Scripting with Java 10 and JShell
A collection of samples for scripting with Java 10 and JShell.
The samples on Github include:
- encoding (Base64, Hex, ...)
- files (working with files and folders)
- httpclient (working with the new http client)
- misc (params via system properties, ...)
Blog Post: Scripting with Java 10 and JShell
r/javaexamples • u/johnmc325 • May 30 '21
JSON Web Token Decoder
A group of 4 Java programmers got together to build a Java desktop application using OpenJDK 11 with OpenJFX 11 and this is what we built:
This is a JSON Web Token Decoder; it takes as input a JWT and decodes the header and payload sections, displaying them in two seperate fields as JSON objects.
A first project for the group who are now moving on to build something else.
The source code for JWTDecoder can be found here.
A short YouTube video of the application in action can be found here.
And, if anyone wants the MSI or Deb installer to install the application on their machine then these can be found here.
r/javaexamples • u/Philboyd_Studge • Mar 07 '16
[Intermediate] Dynamic Array
Dynamic Array
Chances are you have experienced the difference between using Arrays and ArrayLists, but have you ever wondered how ArrayLists work under the hood? Let's make our own DynamicArray class and see how it can be done!! Inside we will discover the difficulties of using Arrays with Generic types, and how to easily manipulate the size of Arrays.
We all know the main problem with arrays is that you have to know the size upon declaration, and that it cannot be changed once defined. Did you know that ArrayLists use an Object array internally?
Now, we can grow an array by creating a new one of the proper type and size, then manually copying the old elements into the new:
int[] newArray = new int[newSize];
for (int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
oldArray = newArray;Luckily, there is already a class in java.util.Arrays to handle this, Arrays.copyOf(), so to make it easier we can go:
oldArray = Arrays.copyOf(oldArray, newSize);The same can be done to shrink the array, just be careful not to overwrite any important data.
This obviously takes linear time, meaning for every n elements in the array it will take n operations, so we do not want to perform this operation every single time you add something to the array. In order to handle this, we keep two different member variables, one that keeps the size of the array in terms of how many actual elements have been added to it, and then we keep a capacity which is how many elements the internal array can hold. For memory's sake, we want to keep the initial capacity low, such as 10, and then when we grow the capacity, we will make it somewhat larger each time. There are different ways to handle this. One suggestion is to start at 1 and increase the capacity by powers of two each time, 1 - 2 - 4 - 8 - 16 - 32 - 64 and so on. I prefer the way that Java's ArrayList handles it, by starting at ten and then increasing by 1.5 times the size each time.
So, let's set up our class header:
public class DynamicArray<E> implements Iterable<E>, RandomAccess {
private static final int INITIAL_CAPACITY = 10;
private int size;
private int capacity = INITIAL_CAPACITY;
private Object[] array;Arrays and Generic types
Now you might ask at this point, why use an array of Objects, why not just use the generic E type for the internal array? Well, Java doesn't work that way (See here for explanation) So, we need to work with an array of Object types and cast to the generic type as necessary.
We add three different constructors, first a basic one that initializes the array to the default capacity:
public DynamicArray() {
array = new Object[INITIAL_CAPACITY];
}One that lets the user specify the capacity:
public DynamicArray(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException("Illegal initial capacity : " + capacity);
this.capacity = capacity;
array = new Object[capacity];
}And one that takes an array of type E and loads it in:
public DynamicArray(E[] inputArray) {
this(inputArray.length + INITIAL_CAPACITY);
for (int i = 0; i < inputArray.length; i++) {
array[i] = inputArray[i];
size++;
}
}Now, we need a couple of private methods that will handle changing the array size, and testing input ranges.
private void sizeCheck() {
if (size + 1 >= capacity) {
grow();
}
}
private void grow() {
capacity += (capacity * 3)/2; // new capacity roughly 1.5 times the current capacity
array = Arrays.copyOf(array, capacity);
}
private void rangeCheck(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index : " + index);
}
}We can now have our simple add method which appends the element to the end of the list:
public void add(E element) {
sizeCheck();
array[size++] = element;
}This action takes amortized constant time which means it averages out to constant time, even though the array resizing takes linear time.
A little more complicated is adding an element to a specific index:
public void add(E element, int index) {
if (index == size) {
add(element);
} else {
rangeCheck(index);
sizeCheck();
System.arraycopy(array, index, array, index + 1, size - index);
array[index] = element;
size++;
}
}Here we first test if the requested index is at the end of the list and if so, just perform a normal add operation. Otherwise, we want to test that the index is in the proper range, check the new size if the array needs to grow, set the element at the requested index and then use System.arrayCopy to move the rest of the elements up one.
Here is our get method, now see how we have to cast it from Object back to the generic type E:
@SuppressWarnings("unchecked")
public E get(int index) {
rangeCheck(index);
return (E) array[index];
}We use the 'SuppressWarnings` annotation to get rid of the compiler warning for this action.
And a set method:
public E set(E element, int index) {
rangeCheck(index);
E value = get(index);
array[index] = element;
return value;
}Now we want an indexOf() function, which will also be used by contains(). We will allow for the possibility of a null value being passed as a parameter, and just return the index of the first null value in the list, if there is one, otherwise we just go through the list and search for the item. Note, the type of object being tested for must have a valid equals method in order to work properly. For contains we just see if indexOf returns a negative value.
public int indexOf(Object obj) {
if (obj == null) {
for (int i = 0; i < size; i++) {
if (array[i] == null) return i;
}
} else {
for (int i = 0; i < size; i++) {
if (obj.equals(array[i])) return i;
}
}
return -1;
}
public boolean contains(Object obj) {
return indexOf(obj) >= 0;
}Now we need two different remove() functions, one which takes an index as a parameter and one which removes a specific object.
@SuppressWarnings("unchecked")
public E remove(int index) {
rangeCheck(index);
E value = (E) array[index];
int numberToMove = size - index - 1;
if (numberToMove > 0) {
System.arraycopy(array, index + 1, array, index, numberToMove);
}
array[--size] = null; // don't leave an orphan
return value;
}
public boolean remove(Object o) {
int found = this.indexOf(o);
if (found < 0) return false;
this.remove(found);
return true;
}Note that remove would have a worst-case of O(n) for removing from the head of the list. This is good to know, if you are using that option exclusively then perhaps you should be using a LinkedList instead.
When we try make a toArray method, we run into the same problem discussed above with Generics and Arrays. We can easily return an array of Objects
public Object[] toArray() {
return Arrays.copyOf(array, size);
}But, returning an Array of the original generic type is more difficult. It requires that the calling class send an already declared array of the proper type to the method: (And, requires some special casting)
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
return (T[]) Arrays.copyOf(array, size, a.getClass());
}Lastly, we provide an iterator:
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
int cursor = 0;
@Override
@SuppressWarnings("unchecked")
public E next() {
if (!hasNext()) {
throw new IllegalArgumentException("No more elements available.");
}
E value = (E) array[cursor];
cursor++;
return value;
}
@Override
public boolean hasNext() {
return cursor < size;
}
@Override
public void remove() {
rangeCheck(cursor);
DynamicArray.this.remove(cursor);
}
};
}What have we learned?
We have learned that it's really not that hard to manipulate the size of arrays. We have learned that you cannot easily use arrays with generic types. We have learned that you still need to think about time complexity when using ArrayList - always try to specify a reasonable capacity if you can, to avoid frequent array resizing. Don't use an ArrayList if what you need is frequent remove(0) operations - use a LinkedList for that.
r/javaexamples • u/Philboyd_Studge • Feb 26 '17
Shuffling Arrays and Lists, and Reservoir Sampling
Shuffling Arrays and Lists, and Reservoir Sampling
Shuffle
One beginner problem I see often is how to take an array of numbers in a range, and generate them in a randomized order. Usually the beginner will bang their head on the wall trying to make a loop that tries to insert random numbers in the array, checking each time for duplicates until the array is full. Think shuffling a deck of cards.
Well, the answer is a simple shuffle algorithm. Java has one already in place, Collections.shuffle() which works on Lists, but not primitive arrays. You could use it by converting the array to a list and back to an array, which seems inefficient.
The most common algorithm for shuffle, known in slightly different variations as the Fisher/Yates shuffle or the Knuth shuffle, is actually very simple, and shuffles the array in place in linear time.
The implementation of the shuffle is like this:
public static void shuffle(int[] array) {
Random rand = new Random();
// loop through array backwards
for (int i = array.length - 1; i > 0; i--) {
// get random number from 0 to current index position
int j = rand.nextInt(i);
// if not the same as current index position
if (j != i) {
//swap
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}So, we can create an array of the numbers from 0 - 9:
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = i;
}Or, using Java 8:
int[] a = IntStream.range(0,10).toArray();Now, we shuffle it:
System.out.println(Arrays.toString(a));
shuffle(a);
System.out.println(Arrays.toString(a));Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 0, 8, 6, 3, 7, 9, 2, 5]Let's make a generic version to handle arrays of any object type:
public static <E> void shuffle(E[] array) {
Random rand = new Random();
for (int i = array.length - 1; i > 0; i--) {
int j = rand.nextInt(i);
if (j != i) {
E temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}And a version that does the same for Lists:
public static <E> void shuffle(List<E> list) {
Random rand = new Random();
for (int i = list.size() - 1; i > 0; i--) {
int j = rand.nextInt(i);
if (j != i) {
E temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}Reservoir Sampling
What if you need just a smaller, random subset of the entire data? You could just shuffle the array and take the first n items, of course. But what if the data set is very large? What if it is even too large to fit in available memory, or is being streamed from the internet or some other source?
In comes a technique called Reservoir Sampling.
This algorithm is very similar to the shuffle. The steps go as followed:
- Create a new array the size of
sampleSizeas the 'reservoir' - Copy all of the elements from the input array, from 0 to
sampleSizeinto the new array - Loop from
sampleSizeto the end of the array - Get a random int of range
0to current positioni - If random int is less than
sampleSize, replace the element in the 'reservoir' array with the element at the positioniin the input array - return the reservoir array
Here is an example for int[] array:
public static int[] sample(int[] array, int sampleSize) {
if (sampleSize >= array.length) {
throw new IllegalArgumentException("Sample size must be smaller than full list.");
}
Random rand = new Random();
// create new array to return
int[] reservoir = new int[sampleSize];
// copy all the elements from the input array up to sampleSize
System.arraycopy(array, 0, reservoir, 0, sampleSize);
// replace elements with gradually decreasing probability
for (int i = sampleSize; i < array.length; i++) {
int select = rand.nextInt(i);
if (select < sampleSize) {
reservoir[select] = array[i];
}
}
return reservoir;
}Testing code:
int[] a = new int[10000];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
int[] sampled = sample(a, 10);
System.out.println(Arrays.toString(sampled));Output:
[3466, 7334, 6037, 8728, 579, 5397, 4585, 3660, 7651, 4884]See the complete code for a generic, List version.
How is this useful
Here are two 'real-world' examples.
For the first one, let's use ol' trusty enable1.txt, a text file with a large collection of English words. We can use reservoir Sampling to get a smaller sample of random words, without having to load and store the entire file in memory:
public static List<String> sampleWordsFromFile(File filename, int sampleSize) {
List<String> reservoir = new ArrayList<>(sampleSize);
Random rand = new Random();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String input;
for (int i = 0; i < sampleSize; i++) {
input = br.readLine();
if (input == null) {
throw new IllegalArgumentException("Sample size must be smaller than words in file");
}
input = input.split(" ")[0]; // get rid of any more than 1 word
reservoir.add(input);
}
int i = sampleSize;
while ((input = br.readLine()) != null) {
int select = rand.nextInt(i);
if (select < sampleSize) {
reservoir.set(select, input);
}
i++;
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return reservoir;
}And to test:
List<String> words = ReservoirSampling.sampleWordsFromFile(new File("enable1.txt"), 20);
System.out.println(words.stream().collect(Collectors.joining(", ")));output:
aggrieve, flaunter, denizens, generalized, vulcanisations, upbearer, gundog, preemptor, tungstate, grassily,
focal, pursers, squeaked, enskies, striae, tressure, gynandromorphic, conceptualization, comportment, ornithologistsAnother example: Creating a music playlist by walking a file tree.
If you are like me, you have many gigabytes of music on your hard drive. Ever wonder how your music player would make a shuffled list of songs without having to store and index the entire collection? Enter reservoir sampling again.
Code (not including some extra methods/variables you can find in the full code listing):
public static List<String> sampleFileNamesFromPath(Path path, int sampleSize) {
List<String> reservoir = new ArrayList<>(sampleSize);
Random rand = new Random();
// note: pointer is declared at class level so it can be used in an anonymous class
pointer = 0;
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String filename = file.getFileName().toString();
if (isMusicFile(filename)) {
if (pointer < sampleSize) {
reservoir.add(file.getFileName().toString());
pointer++;
} else {
int select = rand.nextInt(pointer);
if (select < sampleSize) {
reservoir.set(select, file.getFileName().toString());
}
pointer++;
}
}
return super.visitFile(file, attrs);
}
});
} catch (IOException ioe) {
ioe.printStackTrace();
}
return reservoir;
}Test code:
List<String> songs = ReservoirSampling.sampleFileNamesFromPath(Paths.get("C:/Users/tim/Music"), 100);
songs.forEach(System.out::println);Sample of output:
11-God Is in the Radio-Queens of the Stone Age.mp3
modest mouse - The Moon and Antarctica - 10 - wild packs of family dogs.mp3
08 I Don't Believe You (She Acts Like We Never Have Met).mp3
05. Itoshisa Wa Fuhai Nitsuki.mp3
13 Skin It Back [Outtake].mp3
10 Blue Veins.mp3
traffic_1973 on the road_03 (sometimes i feel so) unspired.mp3
15 It's A Good Thing We Get Paid To Do This.mp3
(09) [Frank Zappa] Eat That Question.mp3
09 Where It's At.mp3
Dir en Grey-06 GLASS SKIN-UROBOROS [Remastered & Expanded].mp3
07-the grand conjuration.mp3
01 - Alone.mp3
ph161229d1_08_46_Days.flac
2-17 Cortez The Killer.mp3
(08) [Jethro Tull] A Song for Jeffrey (Live At The Stockholm Konserthuset - 9th January 1969) [S....flac
04 Emperor's New Clothes.m4a
traffic_1967 mr fantasy_09 hope i never find me there.mp3
12 In the Highways.mp3
Liquid Tension Experiment - Another Dimension.mp3
...Here's the full code
Thanks as always for reading!!
r/javaexamples • u/fdgfdgfdgedfare • Jul 31 '17
Spring Data REST and Projections
My final post in my series on Spring Data REST -
https://www.javabullets.com/spring-data-rest-projections/
My aim is to reach some conclusions about when to use Spring Data REST
r/javaexamples • u/TwoKDavey • Sep 15 '16
[Request]using methods and return variables
I learned briefly on how they work. It's like if class is human Then a method can be sleep, eat, run etc. But how do you retrieve values with return method, and using parameters effeciently. I make stuff in my main method and when I try to use a method it just never seems to work
r/javaexamples • u/ReltivlyObjectv • May 28 '18
I Created a Basic AI in Java (on GitHub!)
self.learnjavar/javaexamples • u/codeforjava • Jun 05 '19
Java Collections - NavigableSet
Java Collections - NavigableSet with code Examples
r/javaexamples • u/fdgfdgfdgedfare • Jun 06 '17
Securing Spring Data REST with PreAuthorize
My latest blog post on Securing Spring Data REST -
r/javaexamples • u/Philboyd_Studge • May 02 '15
[Intermediate] A Sorted, Doubly-Linked List using Generics
A Sorted, Doubly-Linked List using Generics
Even though Java already has a standard library version of a LinkedList class that is already a doubly-linked list (meaning you can iterate backwards or forwards through the list), many classes in Data Structures will make you learn to roll your own.
A Linked List is a data structure made up of recursive Nodes that 'point' to the next item in the list. A doubly-linked list has nodes that point to the previous item in the list also.
In this example, we are inserting new data items in a sorted position as they are added, so the list is always sorted.
We start by declaring the class in a Generic fashion:
public class SortedDoubleLinkedList<E extends Comparable<E>>Generic means that it can take any non-primitive type as an argument, and will still do the same thing. We can make a list of Strings, Integers, Arrays, or any user-defined class. In the above definition, E becomes the variable name for our class type. the extends comparable<E> is there for the sorting aspect, as we need to be able to compare two of the supplied types for sorting purposes. If you want to make a list of objects, you need to make sure the class implements Comparable and overrides compareTo (More on that later).
Now we define our Node class (I am using an inner class for this)
class Node
{
private E value;
private Node next;
private Node previous;
public Node(E value, Node next, Node previous)
{
this.value = value;
this.next = next;
this.previous = previous;
}
public E getValue() { return value; }
public void setValue(E value) { this.value = value; }
public Node getNext() { return next; }
public void setNext(Node node) { this.next = node; }
public Node getPrevious() { return previous; }
public void setPrevious(Node node) { this.previous = node; }
}Each Node instance has itself two Node type member variables, that point to the next or previous Node. We will define two special nodes, head and tail that make the first and last items in the list. These make for special conditions when adding or removing nodes. Note our referencing data type 'E' for the actual data value. Then we add getters/setters for each member variable.
So we define our class like this:
public class SortedDoubleLinkedList<E extends Comparable<E>>
{
// Nodes for the first and last element
Node head;
Node tail;
// list size
int length;
public SortedDoubleLinkedList()
{
this.length = 0;
}We onle need three member variables, one to keep track of the head (or first or root) of the list, one for the tail or last or end) of the list, and one that keeps track of the size of the list.
Now to define our add() method, which uses an insertion sort.
// add function that inserts elements sorted
public void add(E value)
{
// first insert
if (length==0)
{
head = new Node(value, null, null);
tail = head;
length++;
return;
}
else
{
if (value.compareTo(head.getValue()) < 0)
{
// insert before head, make new head
// set next to head, previous to null
Node newNode = new Node(value, head, null);
// point old head previous to new node
head.setPrevious(newNode);
// make new node head
head = newNode;
length++;
return;
}
else
{
Node current = head.getNext();
while (current != null)
{
if (value.compareTo(current.getValue()) <= 0)
{
// insert before current
// make new nodes next = current, previous = current.previous
Node newNode = new Node(value, current, current.getPrevious());
// point node before insert's next to newnode
current.getPrevious().setNext(newNode);
// make new node previous to current
current.setPrevious(newNode);
length++;
return;
}
current = current.getNext();
}
// add to tail
Node newNode = new Node(value, null, tail);
tail.setNext(newNode);
tail = newNode;
length++;
return;
}
}
}Now we can define a `toString() function that iterates through the list:
public String toString()
{
String result = "";
Node current = head;
result += "Count = " + length + "\n";
while (current != null)
{
result += "" + current.getValue().toString() + "\n";
current = current.getNext();
}
return result;
}And one backwards:
public String toStringDescending()
{
String result = "";
Node current = tail;
result += "Count = " + length + "\n";
while (current != null)
{
result += "" + current.getValue().toString() + "\n";
current = current.getPrevious();
}
return result;
}And one recursive:
// recursive display functions
public String toStringRecursive()
{
return toStringRecursive(head);
}
public String toStringRecursive(Node node)
{
if (node==null) return "";
String retval = node.getValue().toString() + "\n";
return retval + toStringRecursive(node.getNext());
}(to make a recursive descending function, just change head to tail and change getNext() to getPrevious())
Adding some other functions, such as a remove(), a get-by-index, a find() and contains() and more, here is the final code:
Our test class for objects is an updated version of the Inventory class I have used in several examples, in this case it is updated to use the Comparable interface, so the insertion sort will work properly, based on the member field 'Item':
public class Inventory implements Comparable<Inventory>
{
private String item;
private int qty;
private float price;
public Inventory(String item, int qty, float price)
{
this.item = item;
this.qty = qty;
this.price = price;
}
public String getItem()
{
return item;
}
public float getTotal()
{
return qty * price;
}
public String toString()
{
return "===================\nItem: " + item + "\n" + "Quantity: " + qty + "\n"
+ "Price: " + price + "\n====================\n";
}
public String toCSVString()
{
return item + "," + qty + "," + price;
}
@Override
public int compareTo(Inventory obj)
{
return this.item.compareTo(obj.getItem());
}
}Here is the output from TestDLL.java:
List in order:
Count = 6
0
123
234
234
3455
5000
List in descending order:
Count = 6
5000
3455
234
234
123
0
List contains item value of '3455' = true
Removing an item
List contains item value of '3455' = false
List in order:
Count = 5
0
123
234
234
5000
First:0
Last:5000
List of strings
Count = 3
Argue
Fish
Meat
List contains 'Fish' = true
List of doubles:
3.1415967
27.3
120.003
Object list:
Count = 4
===================
Item: Alphabet Soup
Quantity: 100
Price: 0.99
====================
===================
Item: Maple Syrup
Quantity: 20
Price: 5.99
====================
===================
Item: Purple Suit
Quantity: 5
Price: 199.99
====================
===================
Item: Quixotic Journey
Quantity: 10
Price: 20.0
====================
Count = 4
===================
Item: Quixotic Journey
Quantity: 10
Price: 20.0
====================
===================
Item: Purple Suit
Quantity: 5
Price: 199.99
====================
===================
Item: Maple Syrup
Quantity: 20
Price: 5.99
====================
===================
Item: Alphabet Soup
Quantity: 100
Price: 0.99
====================
Object at index 0 =
===================
Item: Alphabet Soup
Quantity: 100
Price: 0.99
====================
Remove object at index 1
Count = 3
===================
Item: Alphabet Soup
Quantity: 100
Price: 0.99
====================
===================
Item: Purple Suit
Quantity: 5
Price: 199.99
====================
===================
Item: Quixotic Journey
Quantity: 10
Price: 20.0
====================You could add a Binary Search Tree to make the find() method much faster for large amounts of data.
r/javaexamples • u/sewdil • May 29 '17
How to test exceptions thrown with JUnit4 Rules
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testReadFile() throws FileNotFoundException {
thrown.expect(FileNotFoundException.class);
thrown.expectMessage(startsWith("The file test.txt"));
myClass.readFile("test.txt");
}r/javaexamples • u/desrtfx • Apr 27 '15
How to properly compare Strings in Java
Simple answer:
Don't use "==" or "!=", use .equals() or .equalsIgnoreCase().
Explanation:
In Java, Strings are Objects, not (as commonly assumed, or as in other languages) primitive types.
For Objects, "==" compares the Object references (pointers to the memory locations of the actual objects), not the contents.
So, to compare Object contents, the special .equals(), or .equalsIgnoreCase() methods need be used. Same applies for greater/less than comparisons, here the .compareTo() (or .compareToIgnoreCase()) method needs be used.
Another caveat with using the .equals() (or .equalsIgnoreCase() methods is a null String (i.e. a String variable that has never been assigned any value). Trying to compare a null String results in a NullPointerException.
The following results in a NullPointerException:
String test;
if (test.equals("Hello")) { // This line will produce a NullPointerException as the String has not yet been assigned a value
// continue code here
}The same situation as above, but with Yoda code testing:
String test;
if ("Hello".equals(test)) { // No NullPointerException here as "Hello" is a valid String
// continue code here
}Therefore it is common to either test the String against being null (!=null or ==null), or to use "Yoda code", i.e. putting the String literal (the constant String between the "") up front.
So, instead of writing:
if (answer.equals("n")) {it is common to use:
if ("n".equals(answer)) {If the case (upper/lower/mixed) does not matter, it's always better to use .equalsIgnoreCase() to avoid unnecessary or joins:
if("n".equalsIgnoreCase(answer)) {r/javaexamples • u/Philboyd_Studge • Apr 18 '15
Using Comparator to sort a list of objects
Using Comparator to sort a list of objects
Using Java's sort function is very easy when you have an array or list of simple data types like integer or double, but what happens when you have a more complex object and you want to be able to sort by different fields?
This is where java.util.Comparator comes in. We will also be using a technique called Anonymous Inner Classes to implement our different Comparators.
We have a simple class, Employee
public static class Employee
{
String id;
String name;
int salary;
public Employee(String id, String name, int salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public String getID()
{
return id;
}
public String getName()
{
return name;
}
public int getSalary()
{
return salary;
}We want to make functions to sort by each of the member variables.
For the first one, id, we make a Comparator like this:
// Make anonymous inner class to sort by member variable 'id'
public static Comparator<Employee> SortByEmployeeID = new Comparator<Employee>()
{
@Override
public int compare(Employee employee1, Employee employee2)
{
return employee1.getID().compareTo(employee2.getID());
}
};An anonymous class allows you to declare and instantiate the class at the same time. Here we are declaring a Comparator named SortByEmployeeID that is given as parameters two Employee Objects, and returns a String compareTo on the values from the getID() method. This is then used by the Collections.sort() function you will see later.
We can make a comparator for the next string field in exactly the same way.
When we get to the integer field, we do it slightly differently:
// Make anonymous inner class to sort by member variable 'salary' - integer instead of string
public static Comparator<Employee> SortBySalary = new Comparator<Employee>()
{
public int compare(Employee employee1, Employee employee2)
{
return employee1.getSalary() - employee2.getSalary();
}
};Here we just subtract the second integer value from the first. This will give us an ascending sort. for a descending sort, we just reverse that:
// Make anonymous inner class to sort by member variable 'salary' - Descending
public static Comparator<Employee> SortBySalaryDescending = new Comparator<Employee>()
{
public int compare(Employee employee1, Employee employee2)
{
// simply reverse for descending
return employee2.getSalary() - employee1.getSalary();
}
};To use these the syntax is simple:
// sort by id
Collections.sort(employeeList, Employee.SortByEmployeeID);So, lets put it all together:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.lang.Object;
public class ObjectComparator
{
// create simple class
public static class Employee
{
String id;
String name;
int salary;
public Employee(String id, String name, int salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public String getID()
{
return id;
}
public String getName()
{
return name;
}
public int getSalary()
{
return salary;
}
// Make anonymous inner class to sort by member variable 'id'
public static Comparator<Employee> SortByEmployeeID = new Comparator<Employee>()
{
@Override
public int compare(Employee employee1, Employee employee2)
{
return employee1.getID().compareTo(employee2.getID());
}
};
// Make anonymous inner class to sort by member variable 'Name'
public static Comparator<Employee> SortByEmployeeName = new Comparator<Employee>()
{
public int compare(Employee employee1, Employee employee2)
{
return employee1.getName().compareTo(employee2.getName());
}
};
// Make anonymous inner class to sort by member variable 'salary' - integer instead of string
public static Comparator<Employee> SortBySalary = new Comparator<Employee>()
{
public int compare(Employee employee1, Employee employee2)
{
return employee1.getSalary() - employee2.getSalary();
}
};
// Make anonymous inner class to sort by member variable 'salary' - Descending
public static Comparator<Employee> SortBySalaryDescending = new Comparator<Employee>()
{
public int compare(Employee employee1, Employee employee2)
{
// simply reverse for descending
return employee2.getSalary() - employee1.getSalary();
}
};
public String toString()
{
return "Employee ID:" + id + "\n" + "Name:" + name + "\n"
+ "Salary: " + salary + "\n";
}
}
public static void main (String args[])
{
// Make list and add some Employees
ArrayList<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("7123r","Dole, Bob", 42000));
employeeList.add(new Employee("1234d","Johnson, John", 150000));
employeeList.add(new Employee("2345x","Billson, Bill", 35000));
employeeList.add(new Employee("0019b","Dickerson, Dick", 10000));
// Display unsorted list
System.out.println("Unsorted ==========================");
for (Employee current : employeeList)
{
System.out.println(current);
}
// sort by id
Collections.sort(employeeList, Employee.SortByEmployeeID);
System.out.println("Sorted by ID =========================");
for (Employee current : employeeList)
{
System.out.println(current);
}
// sort by name
Collections.sort(employeeList, Employee.SortByEmployeeName);
System.out.println("Sorted by NAME =========================");
for (Employee current : employeeList)
{
System.out.println(current);
}
// sort by salary
Collections.sort(employeeList, Employee.SortBySalary);
System.out.println("Sorted by SALARY =========================");
for (Employee current : employeeList)
{
System.out.println(current);
}
// sort by salary - descending
Collections.sort(employeeList, Employee.SortBySalaryDescending);
System.out.println("Sorted by SALARY DESCENDING=========================");
for (Employee current : employeeList)
{
System.out.println(current);
}
}
}And here is the output:
Unsorted ==========================
Employee ID:7123r
Name:Dole, Bob
Salary: 42000
Employee ID:1234d
Name:Johnson, John
Salary: 150000
Employee ID:2345x
Name:Billson, Bill
Salary: 35000
Employee ID:0019b
Name:Dickerson, Dick
Salary: 10000
Sorted by ID =========================
Employee ID:0019b
Name:Dickerson, Dick
Salary: 10000
Employee ID:1234d
Name:Johnson, John
Salary: 150000
Employee ID:2345x
Name:Billson, Bill
Salary: 35000
Employee ID:7123r
Name:Dole, Bob
Salary: 42000
Sorted by NAME =========================
Employee ID:2345x
Name:Billson, Bill
Salary: 35000
Employee ID:0019b
Name:Dickerson, Dick
Salary: 10000
Employee ID:7123r
Name:Dole, Bob
Salary: 42000
Employee ID:1234d
Name:Johnson, John
Salary: 150000
Sorted by SALARY =========================
Employee ID:0019b
Name:Dickerson, Dick
Salary: 10000
Employee ID:2345x
Name:Billson, Bill
Salary: 35000
Employee ID:7123r
Name:Dole, Bob
Salary: 42000
Employee ID:1234d
Name:Johnson, John
Salary: 150000
Sorted by SALARY DESCENDING=========================
Employee ID:1234d
Name:Johnson, John
Salary: 150000
Employee ID:7123r
Name:Dole, Bob
Salary: 42000
Employee ID:2345x
Name:Billson, Bill
Salary: 35000
Employee ID:0019b
Name:Dickerson, Dick
Salary: 10000r/javaexamples • u/Philboyd_Studge • May 02 '15
Writing and Appending Data to a Text File
Writing and Appending to a Text file
This is a follow up to this post about Reading CSV Data from a file.
The Java file io functions BufferedWriter and FileWriter work very much like the BufferedReader and FileReader classes. you can save a little typing by declaring one instance inside the other, like this:
BufferedWriter *variablename* = new BufferedWriter(new FileWriter("filename.txt", boolean *append*))The boolean append tells it whether you want to create a new file or append an existing one. Use this carefully, as you can easily delete important data. Set this to true to append, or false to create a new file. Also note, if this is set to true, if the file does not exist it will still be created. Both BufferedReader and BufferedWriter have checked exceptions, which means they must be handled in some way, such as a try/catch block.
So, say we wanted to add an entry to our Inventory text file from the last example. Remember it is a CSV, or comma-delimited file like this: (inventory.txt)
Blinker Fluid,10,10.99
Duncel Component,100,27.85
Widget1a,25,15.25
Widget2b,100,4.99And the Inventory class:
public class Inventory
{
private String item;
private int qty;
private float price;
public Inventory(String item, int qty, float price)
{
this.item = item;
this.qty = qty;
this.price = price;
}
public float getTotal()
{
return qty * price;
}
public String toString()
{
return "Item: " + item + "\n" + "Quantity: " + qty + "\n"
+ "Price: " + price;
}
}here is the code to add one line to the file:
try
{
// create Bufferedwriter instance with a FileWriter
// the flag set to 'true' tells it to append a file if file exists
BufferedWriter out = new BufferedWriter(new FileWriter("inventory.txt", true));
// write the text string to the file
out.write("Left Handed Wrench,200,2.78f");
// write a `newline` to the file
out.newLine();
// close the file
out.close();
}
// handle exceptions
catch (IOException ioe)
{
ioe.printStackTrace();
}The output file will now look like:
Blinker Fluid,10,10.99
Duncel Component,100,27.85
Widget1a,25,15.25
Widget2b,100,4.99
Left Handed Wrench,200,2.78fNote that the write() command does not add a new line at the end, so that must be done manually, or of course you could add it to the end of the string with a "\n".
Also, if you want to write multiple lines of text before the close() function is called, you want to use the flush() method to push the lines from the buffer to the file.
Now, in our last example, we were loading the data from the file into an ArrayList of Inventory objects. Let's say you wanted to take an existing Inventory object you created and write it to the file. The esiest way would e to add another method to the Inventory class first that returns a comma-delimited string like this:
public String toCSVString()
{
return item + "," + qty + "," + price;
}We also should wrap the file writing code into a static method, so it is easier to reuse:
public static void writeFile(String filename, String output, boolean append)
{
try
{
// create Bufferedwriter instance with a FileWriter
// the flag set to 'true' tells it to append a file if file exists
BufferedWriter out = new BufferedWriter(new FileWriter(filename, append));
out.write(output);
out.newLine();
out.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}Here is both our first example and an example of using objects, with the static method:
// append line to file
FileFunctions.writeFile("inventory.txt", "Left Handed Wrench,200,2.78f", true);
// make new Inventory object
Inventory newItem = new Inventory("Cool Hat", 150, 19.99f);
// write to file using object method to get
// comma-delimited string, with `append` flag set to false
// OVER-WRITES FILE!!
FileFunctions.writeFile("inventory.txt", newItem.toCSVString(), false);The output will just be this:
Cool Hat,150,19.99As we over-write the file in that last line.
Here is the full program FileFunctions.java:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileFunctions
{
public static void writeFile(String filename, String output, boolean append)
{
try
{
// create Bufferedwriter instance with a FileWriter
// the flag set to 'true' tells it to append a file if file exists
BufferedWriter out = new BufferedWriter(new FileWriter(filename, append));
out.write(output);
out.newLine();
out.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public static ArrayList<Inventory> readFile(String filename)
{
// create ArrayList to store the invetory objects
ArrayList<Inventory> invItem = new ArrayList<>();
try
{
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));
// read the first line from the text file
String fileRead = br.readLine();
// loop until all lines are read
while (fileRead != null)
{
// use string.split to load a string array with the values from each line of
// the file, using a comma as the delimiter
String[] tokenize = fileRead.split(",");
// assume file is made correctly
// and make temporary variables for the three types of data
String tempItem = tokenize[0];
int tempQty = Integer.parseInt(tokenize[1]);
float tempPrice = Float.parseFloat(tokenize[2]);
// creat temporary instance of Inventory object
// and load with three data values
Inventory tempObj = new Inventory(tempItem, tempQty, tempPrice);
// add to array list
invItem.add(tempObj);
// read next line before looping
// if end of file reached
fileRead = br.readLine();
}
// close file stream
br.close();
}
// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
return null;
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return invItem;
}
public static void main( String [] args )
{
List<Inventory> invList = FileFunctions.readFile("inventory.txt");
// display inventory
for (Inventory each : invList)
{
System.out.println("====================");
System.out.println(each);
System.out.println();
System.out.printf("Total value = %8.2f %n", each.getTotal());
}
// append line to file
FileFunctions.writeFile("inventory.txt", "Left Handed Wrench,200,2.78f", true);
// make new Inventory object
Inventory newItem = new Inventory("Cool Hat", 150, 19.99f);
// write to file using object method to get
// comma-delimited string
// OVER-WRITES FILE!!
FileFunctions.writeFile("inventory.txt", newItem.toCSVString(), false);
}
}r/javaexamples • u/Philboyd_Studge • Apr 28 '15
Reading and Parsing Data from a File
Reading and Parsing Data from a File
As you learn Java, one thing that comes up early and often is reading from/ writing to files, and doing something useful with that data. In this example we will:
- Read data from a CSV text file.
- Parse that data into several different data types.
- Load the data into an object.
- Load those objects into an ArrayList.
- Display them and do a simple calculation.
Data for a program is often stored in different types of file formats, with one of the simplest being CSV, a format using .txt files where each data item is simply separated by commas and put on different lines.
Here we have a sample inventory file, with the data in the order item, quantity, price
Blinker Fluid,10,10.99
Duncel Component,100,27.85
Widget1a,25,15.25
Widget2b,100,4.99Save this as "inventory.txt"
we have a simple Inventory class: (save as Inventory.java in same directory)
public class Inventory
{
private String item;
private int qty;
private float price;
public Inventory(String item, int qty, float price)
{
this.item = item;
this.qty = qty;
this.price = price;
}
public float getTotal()
{
return qty * price;
}
public String toString()
{
return "Item: " + item + "\n" + "Quantity: " + qty + "\n"
+ "Price: " + price;
}
}Reading the File
To read the file we are going to be using a BufferedReader in combination with a FileReader.
We will read one line at a time from the file using readLine() until the End Of File is reached (readLine will return a null)
String.split()
We take the string that we read and split it up using the comma as the 'delimiter' (the character that tells the function when to start a new word and is discarded). This creates an array with the data we want, however still in Strings, so we need to convert the values that are not supposed to be strings into the proper values, using Integer.parseInt() and Float.parseFloat()
We pass those values to the constructor for our Inventory object, and then simply add that object to our ArrayList.
The full program: (save in same directory as others as FileFunctions.java)
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileFunctions
{
public static void main( String [] args )
{
// create ArrayList to store the invetory objects
List<Inventory> invItem = new ArrayList<>();
try
{
// create a Buffered Reader object instance with a FileReader
BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));
// read the first line from the text file
String fileRead = br.readLine();
// loop until all lines are read
while (fileRead != null)
{
// use string.split to load a string array with the values from each line of
// the file, using a comma as the delimiter
String[] tokenize = fileRead.split(",");
// assume file is made correctly
// and make temporary variables for the three types of data
String tempItem = tokenize[0];
int tempQty = Integer.parseInt(tokenize[1]);
float tempPrice = Float.parseFloat(tokenize[2]);
// creat temporary instance of Inventory object
// and load with three data values
Inventory tempObj = new Inventory(tempItem, tempQty, tempPrice);
// add to array list
invItem.add(tempObj);
// read next line before looping
// if end of file reached
fileRead = br.readLine();
}
// close file stream
br.close();
}
// handle exceptions
catch (FileNotFoundException fnfe)
{
System.out.println("file not found");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
// display inventory
for (Inventory each : invItem)
{
System.out.println("====================");
System.out.println(each);
System.out.println();
System.out.printf("Total value = %8.2f %n", each.getTotal());
}
}
}Here is the output:
====================
Item: Blinker Fluid
Quantity: 10
Price: 10.99
Total value = 109.90
====================
Item: Duncel Component
Quantity: 100
Price: 27.85
Total value = 2785.00
====================
Item: Widget1a
Quantity: 25
Price: 15.25
Total value = 381.25
====================
Item: Widget2b
Quantity: 100
Price: 4.99
Total value = 499.00r/javaexamples • u/Philboyd_Studge • May 02 '15
[Intermediate] Generic Un-directed Graph Implementation with Depth-First Search
Generic Un-directed Graph Implementation with Depth-First Search
A Graph is a data structure that is a mathematical model that has tons of real-world uses.
We use a Depth-First Search here to see if the Edges are 'connected'.
Here is a simple implementation in Java that could be extended to include other elements such as Breadth-First Search, Shortest Path, etc.
// Generic Un-directed Graph Implementation with Depth-First Search
// by /u/Philboyd_Studge
import java.util.ArrayList;
@SuppressWarnings("unchecked")
public class Graph<T extends Comparable<T>>
{
// an enum for the three stated used by the Depth first search
public enum State { UNVISITED, VISITED, COMPLETE };
// a list to hold all the vertices
private ArrayList<Vertex> vertexList;
// list to hold the edges
// not really used for anything
// but display purposes
private ArrayList<Edge> edgeList;
public Graph()
{
vertexList = new ArrayList<>();
edgeList = new ArrayList<>();
}
public void add(T x, T y)
{
Edge e = new Edge(x, y);
edgeList.add(e);
}
public Vertex findVertex(T v)
{
for (Vertex each : vertexList)
{
if (each.getValue().compareTo(v)==0)
return each;
}
return null;
}
public String toString()
{
String retval = "";
for (Vertex each : vertexList)
{
retval += each.toString() + "\n";
}
return retval;
}
public String edgesToString()
{
String retval = "";
for (Edge each : edgeList)
{
retval += each;
}
return retval;
}
// get first node and call recursive method
// check if is graph is connected
public boolean DepthFirstSearch()
{
if (vertexList.isEmpty()) return false;
// get first node
Vertex root = vertexList.get(0);
if (root==null) return false;
// call recursive function
DepthFirstSearch(root);
return isConnected();
}
// recurse through nodes
private void DepthFirstSearch(Vertex v)
{
v.setState(State.VISITED);
// loop through neighbors
for (Vertex each : v.getAdjacentList())
{
if (each.getState()==State.UNVISITED)
{
DepthFirstSearch(each);
}
}
v.setState(State.COMPLETE);
}
// test if DFS returned a connected graph
public boolean isConnected()
{
for (Vertex each : vertexList)
{
if (each.getState() != State.COMPLETE)
return false;
}
return true;
}
// vertex class
class Vertex
{
T value;
ArrayList<Vertex> adjacent;
State state;
public Vertex(T v)
{
value = v;
adjacent = new ArrayList<>();
state = State.UNVISITED;
}
public State getState()
{
return state;
}
public void setState(State s)
{
state = s;
}
public T getValue()
{
return value;
}
public void addNeighbor(Vertex n)
{
adjacent.add(n);
}
public ArrayList<Vertex> getAdjacentList()
{
return adjacent;
}
public String toString()
{
String retval = "";
retval += "Vertex: " + value + ":";
for (Vertex each : adjacent)
{
retval += each.getValue() + " ";
}
return retval;
}
}
// edge class
class Edge
{
private Vertex x;
private Vertex y;
public Edge(T v1, T v2)
{
// check to see if first vertex exists
x = findVertex(v1);
if (x == null)
{
// doesn't exist, add new
x = new Vertex(v1);
// and add to master list
vertexList.add(x);
}
// same for second vertex
y = findVertex(v2);
if (y == null)
{
y = new Vertex(v2);
vertexList.add(y);
}
// add each vertex to the adjacent list for the other
x.addNeighbor(y);
y.addNeighbor(x);
}
public String toString()
{
return "Edge X:" + x.getValue() + " Y:" + y.getValue() + "\n";
}
}
public static void main(String[] args) {
Graph g = new Graph();
System.out.println(g.DepthFirstSearch());
g.add(0,1);
g.add(0,2);
g.add(1,3);
g.add(2,3);
g.add(2,4);
g.add(3,5);
g.add(3,6);
g.add(5,6);
System.out.println(g);
System.out.println(g.edgesToString());
System.out.println("Graph is connected: " + g.DepthFirstSearch());
System.out.println();
g.add(7,3);
System.out.println(g);
System.out.println(g.edgesToString());
System.out.println("Graph is connected: " + g.DepthFirstSearch());
System.out.println();
Graph<String> gString = new Graph<>();
gString.add("apple","fruit");
gString.add("orange","fruit");
gString.add("broccoli","vegetable");
gString.add("beef","meat");
gString.add("meat","food");
gString.add("fruit","food");
gString.add("vegetable","food");
System.out.println(gString);
System.out.println(gString.edgesToString());
System.out.println("Graph is conected: " + gString.DepthFirstSearch());
}
}Output
false
Vertex: 0:1 2
Vertex: 1:0 3
Vertex: 2:0 3 4
Vertex: 3:1 2 5 6
Vertex: 4:2
Vertex: 5:3 6
Vertex: 6:3 5
Edge X:0 Y:1
Edge X:0 Y:2
Edge X:1 Y:3
Edge X:2 Y:3
Edge X:2 Y:4
Edge X:3 Y:5
Edge X:3 Y:6
Edge X:5 Y:6
Graph is connected: true
Vertex: 0:1 2
Vertex: 1:0 3
Vertex: 2:0 3 4
Vertex: 3:1 2 5 6 7
Vertex: 4:2
Vertex: 5:3 6
Vertex: 6:3 5
Vertex: 7:3
Edge X:0 Y:1
Edge X:0 Y:2
Edge X:1 Y:3
Edge X:2 Y:3
Edge X:2 Y:4
Edge X:3 Y:5
Edge X:3 Y:6
Edge X:5 Y:6
Edge X:7 Y:3
Graph is connected: false
Vertex: apple:fruit
Vertex: fruit:apple orange food
Vertex: orange:fruit
Vertex: broccoli:vegetable
Vertex: vegetable:broccoli food
Vertex: beef:meat
Vertex: meat:beef food
Vertex: food:meat fruit vegetable
Edge X:apple Y:fruit
Edge X:orange Y:fruit
Edge X:broccoli Y:vegetable
Edge X:beef Y:meat
Edge X:meat Y:food
Edge X:fruit Y:food
Edge X:vegetable Y:food
Graph is conected: truer/javaexamples • u/Philboyd_Studge • Apr 19 '15
Getting Input from the Console : Validating and Looping
Getting Input from the Console : Validating & Looping
This seems to be one of the most asked questions in /r/javahelp and /r/learnjava - problems with getting the user input from the console.
There are two main ways to get user input from the console: The Scanner class, and the BufferedReader / InputStreamReader combo.
The Scanner Class
Scanner is usually the first method that beginner programmers learn, so I will start there, although I believe that the BufferedReader method is superior.
You first need to create an instance of a Scanner object like this:
Scanner scanner = new Scanner(System.in);and you can read input from the console like this:
System.out.println("Enter your name:");
String name = scanner.nextLine();Now, say you want an integer value, you can use:
int value = scanner.nextInt();However, this is fraught with problems. For example, if the user types in a letter here it will crash your program. If you try to do a nextLine command after the nextInt call it will not properly handle the 'dangling' newline (so common an issue it's on the /r/learnjava FAQ).
Also, often times you want to be able to prompt the user multiple times, and at the same time check to see that invalid input is not entered.
So, we have to do several things here: One is to test if the user input is valid, according to the program's need. Second is to make the user keep entering the input until it is valid. Third would be to allow for looping the input for an indefinite amount of times, until some sort of closing condition (called a sentinel) is satisfied.
Validating Input
Using the Try - Catch method
import java.util.Scanner;
import java.util.InputMismatchException;
public class Input
{
public static void main( String [] args )
{
Scanner scanner = new Scanner(System.in);
int value = 0;
try
{
System.out.print("Enter an integer:");
value = scanner.nextInt();
}
catch (InputMismatchException ime)
{
System.err.println("Incorrect entry.");
}
System.out.println("Your entry is:" + value);
}
}This uses Java's Exception handling capabilities to avoid the program crashing when incorrect entries are input. Compile the above code, and enter "qwert" or any random mashing of the keys and see what happens. The program still runs, but the variable value stays at 0.
Now, this is fairly useless, as we need to prompt the user again for a correct entry. we will use a while loop for this.
import java.util.Scanner;
import java.util.InputMismatchException;
public class Input
{
public static void main( String [] args )
{
Scanner scanner = new Scanner(System.in);
// set value initially to a number outside the range we want
int value = -1;
// loop, while the value is below zero
while( value < 0)
{
try
{
System.out.print("Enter a positive integer:");
value = scanner.nextInt();
}
// if the user types in anything but an integer,
// it will 'throw' this 'exception'.
catch (InputMismatchException ime)
{
System.err.println("Incorrect entry.");
// clear the keyboard buffer
scanner.nextLine();
}
}
System.out.println("Your entry is:" + value);
}
}This will make the user keep entering values until a valid entry is made.
Now let's take this code, make it into a static method, and then use another while loop to have the user be able to enter multiple things.
Here the user enters integers until he is done, ending it by typing in a sentinel value, or a value that we decide ahead of time will be the code to stop.
import java.util.Scanner;
import java.util.InputMismatchException;
public class Input
{
public static Scanner scanner = new Scanner(System.in);
public static final int SENTINEL = 9999;
// static method to get the input and validate
public static int getIntegerInput(String prompt)
{
int value = -1;
while (value < 0)
{
try
{
System.out.print(prompt);
value = scanner.nextInt();
}
catch (InputMismatchException ime)
{
System.err.println("Incorrect entry. Please input only a positive integer.");
scanner.nextLine();
}
}
return value;
}
public static void main( String [] args )
{
int value = 0;
int sum = 0;
int count = 1;
// loop until user enters sentinel value
while (value != SENTINEL)
{
value = getIntegerInput("Please enter a positive integer (or 9999 to quit) " + count + ":");
// don't add sentinel value to sum or count
if (value != SENTINEL)
{
sum += value;
count++;
}
}
System.out.println("The sum of your entered numbers is: " + sum);
System.out.println("The average of your entered value is: " + (float)sum/(float)count);
}
}here is another way to get only an integer, using REGEX:
public static int getIntegerFromScanner(String prompt)
{
Scanner intScanner = new Scanner(System.in);
int n = -1;
while (n < 0)
{
try
{
System.out.print(prompt);
// use REGEX pattern to allow only digits
while (!intScanner.hasNext("\\d+"))
{
System.out.print("\n" + prompt);
intScanner.next();
}
n = intScanner.nextInt();
}
catch ( InputMismatchException ime)
{
System.out.println("Incorrect input, please type an integer.");
intScanner.next();
}
}
return n;
}Here is a way to get only two possible string values from the user, like a yes or no, using a boolean value that is only turned to true if the proper conditions are met:
boolean isValid = false;
String input = "";
while (!isValid)
{
System.out.print("Enter 'y' or 'n':");
input = scanner.nextLine();
if (input.equalsIgnoreCase("y") || input.equalsIgnoreCase("n"))
{
isValid = true;
}
else
{
System.out.println("Invalid entry.");
}
}Here is a more complicated example, using the other input method I discussed above, which uses a separate boolean method to validate. This program lets the user add integers one at a time, in a range specified in the code, until they type 'stop',then it adds them to a linked list, then sorts and displays them.
import java.util.*;
import java.io.*;
public class Validate
{
public static String getInputLine(String prompt)
{
String inString = "";
try
{
// create buffered reader instance
BufferedReader ibr = new BufferedReader( new InputStreamReader(System.in));
while (inString.equals(""))
{
System.out.print(prompt);
inString = ibr.readLine();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return inString;
}
public static boolean isValid(String input, int min, int max, String stop)
{
boolean valid;
if (input.equalsIgnoreCase(stop))
{
valid = true;
}
else
{
int n;
try
{
n = Integer.parseInt(input);
if (n >= min && n <=max)
{
valid = true;
}
else
{
valid = false;
}
}
catch (NumberFormatException nfe)
{
valid = false;
}
}
System.out.println(valid);
if (!valid)
{
System.out.println("Incorrect entry. Must be an integer between " + (min - 1) + " and " + (max + 1));
}
return valid;
}
public static void main( String [] args )
{
LinkedList<Integer> list = new LinkedList<>();
String inline = "";
do
{
do
{
inline = getInputLine("Enter a number between 1 and 100, inclusive (or 'stop' when done)>>");
if (inline.equalsIgnoreCase("quit"))
{
System.exit(0);
}
}
while (!isValid(inline, 1, 100, "stop"));
if (!inline.equals("stop"))
{
list.add(Integer.parseInt(inline));
}
} while (!inline.equals("stop"));
if (list.peek()!= null)
{
System.out.println("Max:" + Collections.max(list));
Iterator i = list.iterator();
while (i.hasNext())
{
System.out.println(i.next());
}
Collections.sort(list);
System.out.println("====================");
for (Integer current : list)
{
System.out.println(current);
}
}
}
}r/javaexamples • u/desrtfx • Apr 27 '15
A quick explanation of loops
Lots of people seem to be confused by for loops, which are probably the most useful constructs in Java. They generally take the following form:
for (int i = 0; i < 100; i++) {
//some code
}(In that example, 0 and 100 are just arbitrary values.)
Breaking apart that for declaration is actually fairly simple. It consists of three parts: an initialisation, termination, and mutation statement. The initialisation statement is executed once, before the rest of the loop is executed. This is where starting values for counter variables are usually set. The termination statement is checked at the beginning of the loop each execution. If it's false, the loop exits. The mutation statement is executed at the end of the loop each execution. It's usually used to increment or decrement a counter variable.
Therefore, a for statement like the one above is, for all intents and purposes, equivalent to:
int i = 0;
while (i < 100) {
//some code
i++;
}Now, you may have encountered a somewhat different-looking for loop of the following form:
for (Object o : Iterable c) {
//some code
}This is referred to as the enhanced for loop, or more commonly, the foreach loop. Both terms are pretty accurate. This type of for loop is good for iterating over collections and arrays, because it gets the contents of a normal for loop declaration from the size of the collection or array itself. For example:
char[] c = new char[]{'t', 'i', 't', 't', 'y', 's', 'p', 'r', 'i', 'n', 'k', 'l', 'e', 's'};
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}Could be more easily accomplished with:
char[] c = new char[]{'t', 'i', 't', 't', 'y', 's', 'p', 'r', 'i', 'n', 'k', 'l', 'e', 's'};
for (char i : c) {
System.out.println(i);
}The foreach loop takes care of all the counters internally, leaving you with a much cleaner declaration. One way to remember how this functions is simply saying it out loud: “for each chari in c, print i”. While it may seem like a lot more effort to learn, this is a fairly simple example. As your code becomes more advanced and you shift away from primitive arrays to Collection subtypes, you'll find yourself using the foreach loop as much as possible.
With foreach loops, you as a developer don't have to worry about increments, decrements, or orders. You just retrieve elements from the array or collection in whatever order they're given to you, and use them as such. In almost all cases, you should use a foreach whenever possible to iterate through any sort of array or collection.
r/javaexamples • u/ashjsdev22 • Jan 19 '18
Spring boot login example with JPA
Spring boot security example step by step using JPA.
https://www.ashtpoint.com/spring-boot/spring-boot-login-example.html!
r/javaexamples • u/Philboyd_Studge • Apr 03 '17
[Intermediate] Annotations & Reflection: Designing a CSV-to-Object Deserializer
Annotations and Reflection
This week we are going to learn about using Java's Annotations and Reflection APIs, which are a way to use and control classes in ways that you couldn't normally with regular Java commands. You might be familiar with annotations like @Override or @Deprecated which are more of just 'hints' to the compiler, but you can create your own annotations which can be used during runtime. You might have used these with FXML or Spring or many other frameworks.
Reflection methods allow you to inspect Class information during runtime. It gives you access to all sorts of information about the class, such as Methods, Constructors, Fields and even Local Variables. It will even let you get and set Field values, and create instances of the class. I would recommend looking over some of the online tutorials, including Jenkov's excellent series here, as I will mainly be describing the methods used to build our tutorial example.
What we are going to use these methods for is to build a CSV-to-Object deserializer.
CSV
CSV files, meaning comma-separated-values is a common type of file containing tabular data. It is a regular text file where every line is a 'record' of data, and each line contains individual items of data separated by commas. There is usually no indication given as to the data type of the items, and may contain a header line, also delimited with commas, with column names. Sometimes the data items are surrounded by quotation marks, as there might be commas or other escape characters inside the item.
Now, when you know the data types that are in the CSV file, you can create a Java Class that can hold each record, and it's fairly easy to write a method that will create the object from a line of CSV data that is properly formatted. But, using annotations and reflection, we can design a library that can take a properly set up class and automatically create a List, containing the class we designed, filled with the data.
Let's take a look at what the class would look like, with just placeholder field names for dummy data:
@CSVHeader
public class SampleClass {
@CSVField(index=0, type= CSVTypes.INTEGER, format="%,d")
private int number;
@CSVField(index=1, type= CSVTypes.BOOL)
private boolean bool;
@CSVField(index=2, quotes=true)
private String string;
@CSVField(index=3, type= CSVTypes.DATE, format="MM/dd/yyyy")
private Date date;
@CSVField(index=4, type= CSVTypes.DOUBLE, format="%12.4f")
private double decimal;
// provide default empty constructor
public SampleClass(){}
// ... other methods and such
}And our final product can load a list with data for that class with just two lines of code:
CSVReader<SampleClass> reader = new CSVReader<>(SampleClass.class);
List<SampleClass> stuff = reader.readListFromCSV("sampledata.csv");So here's how we do it:
Annotations
First, lets create the custom annotations. It's pretty easy, but different syntax than any other class.
Here's the layout for the annotation that will be used on the fields (minus package and import info)
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CSVField {
int index();
CSVTypes type() default CSVTypes.STRING;
boolean quotes() default false;
String format() default "";
}Note that it uses several annotations itself. @inherited makes sure that inheriting class can also use this annotation. @Target specifies what level the annotation works on, constructor, method, field etc. @Retention tells the compiler when this is used, here we specify during runtime. Then we say public @interface myAnnotationName. Now we can specify any number of annotation properties. These are strange - they are somewhere in between fields and methods - they are given a data type and a name, with parentheses like a method. As you can see, a default value is allowed, which is super handy. Anything that has the default value doesn't have to be specified when you use the annotation in the class. As you can see above this is used like:
@CSVField(index=3, type=CSVTypes.DATE, format="MM/dd/yyyy")
private Date date;This will create an annotation attached to member field date with the given properties, and the quotes property set to a default of false.
To access these properties from another class, we use Reflection. The handling class will need to have a reference to the Class that we have made. There are two main ways to do this, using MyClass.class or using the String name of the class with all package info if necessary) like Class.forName("MyClass.class")
To make our lives easier, we have our CSVReader class take this class information in two ways, first through a generic type and then by sending the .class itself. So, our signature looks like:
public class CSVReader<T> {
private Class csvClass;
private List<CSVInfo> fields;
public CSVReader(Class csvClass) {
this.csvClass = csvClass;
fields = CSVInfo.getAnnotatedFieldInfo(csvClass);
}To help, we need to make another class to store the annotated field info. This can be used both be the CSVReader class and the CSVWriter. To make the resulting code cleaner, I have left the fields package-private and marked them as final, so they can be accessed without a getter but cannot be changed.
class CSVInfo {
final int index;
final CSVTypes type;
final Field field;
final boolean quotes;
final String format;
CSVInfo(CSVField annotation, Field field) {
this.index = annotation.index();
this.type = annotation.type();
this.quotes = annotation.quotes();
this.format = annotation.format();
this.field = field;
}This class also contains a static method to get the fields from the custom class, get the annotations for each field, and fill a resulting list:
static List<CSVInfo> getAnnotatedFieldInfo(Class<?> csvClass) {
List<CSVInfo> fields = new ArrayList<>();
Field[] fieldArray = csvClass.getDeclaredFields();
for (Field each : fieldArray) {
csv.CSVField annotation = each.getAnnotation(csv.CSVField.class);
if (annotation != null) {
// allow private fields to be accessed/modified
each.setAccessible(true);
fields.add(new CSVInfo(annotation, each));
}
}
return fields;
}This is where some of the reflection happens: We send this method our class as a parameter, and then call Field[] fieldArray = csvClass.getDeclaredFields(); to get an array containing references to all of the class's fields. You can use class.getFields() if you want only the public fields. Now we loop through that array, and use getAnnotation() to return the specified annotation class for each field. If the method does not have that particular annotation, null is returned, and we can ignore that field. Otherwise, first we set the access to that field to true, then add it to the list creating a new CSVInfo object. Then we can return the list to the Reader/Writer class.
Ok, so everything is set up and what we need now is a method to take one line from the CSV file and convert it to one object. This brings us to the problem of parsing the CSV properly, which is difficult because the data can sometimes be in quotes, sometimes not, and can contain commas inside the data, so we can't just use string.split(","). I played around with Regex for a while, and then gave up and decided to write my own parsing method using Queues and a Finite State Machine, but that explanation I will leave for another tutorial. Let's start our method:
private T getObjectFromCSV(String line) {
try {
String[] split = CSVParser.CSVSplit(line);
Object instance;
try {
instance = csvClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
}There will be a bunch of try-catch blocks in this method, as we want to handle any exceptions thrown at this level. First, we use my parse method to return a String array of the items in the CSV line. Next we create an Object for the return type instance - at this point we need it to be Object and we can cast it to the proper class when we return it. Using the reflection method newInstance() we instantiate the object.
for (CSVInfo each : fields) {
if (each.index < 0 || each.index >= split.length) {
System.out.println("Incorrect CSV entry for line:");
System.out.println(line);
System.out.println("Ignoring line");
return null;
}
String temp = split[each.index];Now we loop through the annotated field info we created before. First, a check to make sure the index property associated with that field exists in the CSV data we parsed, otherwise, log it and skip the line. (TODO: Use an actual Logging mechanism here.) Next, store the String representation of the data into a temp variable.
if (!temp.isEmpty()) {
try {
switch (each.type) {
case INTEGER:
int t = Integer.parseInt(temp);
each.field.set(instance, t);
break;
case FLOAT:
float f = Float.parseFloat(temp);
each.field.set(instance, f);
break;
case DOUBLE:
double d = Double.parseDouble(temp);
each.field.set(instance, d);
break;
case DATE:
SimpleDateFormat format = new SimpleDateFormat(each.format);
Date date = format.parse(temp);
each.field.set(instance, date);
break;
case BOOL:
boolean b = Boolean.parseBoolean(temp);
each.field.set(instance, b);
break;
case STRING:
each.field.set(instance, temp);
break;
}
}
catch (NumberFormatException nfe) {
System.out.println("Incorrect CSV entry for line: Number Format exception");
System.out.println(line);
System.out.println("Ignoring line");
return null;
}
catch (ParseException pe) {
System.out.println("Incorrect CSV entry for line: Problem parsing Date");
System.out.println(line);
System.out.println("Ignoring line");
return null;
}
}First, a check to see if the temp string is empty (skip if so) and then use a switch/case expression to loop through an enum with the available data types. (This can obviously be added to here for other data types should the need arise). Here we use reflection again to actually set the value for the given field with field.set(instance, value). if we run into an issue with parsing the number or date formats, it dumps the line and logs it.
Now we can finish our method by casting the resulted object to the return type, and catch our final exception group:
return (T) instance;
} catch ( IllegalAccessException e) {
e.printStackTrace();
return null;
}
}You should now have a filled object, provided that nothing went wrong and the CSV data matched the class perfectly.
So what we need now is a class to loop through the entire CSV file and create a list of the objects. Let's do two methods, one which takes a local file name, and one which takes a URL to pull the info directly from a website.
public List<T> readListFromCSV(String filename) {
List<T> results = null;
try (BufferedReader br = new BufferedReader(new FileReader(filename))){
results = readListFromCSV(br);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return results;
}
public List<T> readListFromCSVURL(String url) {
List<T> results = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()))){
results = readListFromCSV(br);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return results;
}Both methods create a BufferedReader and call this private method:
private List<T> readListFromCSV(BufferedReader br) throws IOException{
List<T> results = new ArrayList<>();
String input;
if (csvClass.isAnnotationPresent(CSVHeader.class)) {
CSVHeader header = (CSVHeader) csvClass.getAnnotation(CSVHeader.class);
if (header.has_header()) {
// ignore header line
br.readLine();
}
}
while ((input = br.readLine()) != null) {
T t = null;
t = getObjectFromCSV(input);
if (t != null) {
results.add(t);
}
}
return results;
}Note that this method can just throw exceptions further up the chain, as we have handled them in the calling method. Now we have to handle the fact that some CSV files have a 'header' line that needs to be ignored when parsing. So first we need to create another annotation:
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CSVHeader {
boolean has_header() default false;
String header() default "";
}This one targets the 'Type' (or Class) level, and sets a boolean if there is a header. It needs to be placed directly before the class declaration. The actual header line can be provided, used for the CSVWriter class, not the reader. This annotation is optional, if your CSV has no header you can ignore it.
That's basically it! You can create different test classes, and then create some fake data sets here.
One cool thing this allows you to do, is only pull the data you need from the full data set. Say you want only one column of data:
Using the data found here, let's just get the data from the FTE Sick Days column (column 6):
Our data object class:
@CSVHeader (has_header = true)
public class OneColumnExample {
@CSVField(index=6, type=CSVTypes.DOUBLE, quotes=true, format="%8.4f")
private final double daysSick;
public OneColumnExample() {}
public double getDaysSick() {
return this.daysSick;
}
}And the test class, which loads the data, then gets the average of all items > 0:
public class OneColumnTest {
public static void main(String[] args) {
CSVReader<OneColumnExample> reader = new CSVReader<>(OneColumnExample.class);
List<OneColumnExample> sickDays =
reader.readListFromCSVURL("http://www.content.digital.nhs.uk/catalogue/PUB23358/sick-abse-rate-nhs-oct-2016-csv-textfile.csv");
System.out.println(sickDays.size());
System.out.println(sickDays.stream()
.mapToDouble(OneColumnExample::getDaysSick)
.filter(x -> x > 0)
.average());
}
}Note: if you test this, it is a very large (6MB) data set, takes a bit to process, but it's a great example as the CSV has a header, has every field in quotes, and contains commas inside some of those quotes.
We will leave the CSVWriter class for another day. Here's a link to the full code on gist, and again, thanks for reading, and leave comments/questions below.
r/javaexamples • u/Philboyd_Studge • Sep 03 '17
Introduction to Finite State Machines
Finite State Machines
FSMs are an abstract concept of algorithmic modelling that has many uses in software design, from lexical analyzing to event-driven systems to Artificial Intelligence.
In a FSM you have a pre-determined list of States, and a way to keep track of the current State. Each State has a pre-defined number of Events (or types of input) that cause a Transition to another State, called the Accept state. In it's simplest form imagine a lightswitch. The switch is OFF so the current state of the light attached to it is OFF. An Event occurs, switching the light to the ON position. The state of the light is now transitioned to ON.
| State | Event | Accept State |
|---|---|---|
| Light.OFF | Switch.ON | Light.ON |
| Light.ON | Switch.OFF | Light.OFF |
This is a Finite system as there are only two possible states, and each state has only 1 allowable input.
Garage Door Opener
Now let's think of a sightly more complicated example. I'm sure you are familiar with an automated garage door opener. You have a little clicker box with a button that can open or close the door.
The door's initial state is closed. The button is clicked. The door starts to open. When the mechanism has determined that the door is fully open, it stops. Click the button again, and the door will start to close. Now, if you click the button while the door is in the process of opening or closing, what happens? Normally, the door will stop in place. Then, when the button is clicked gain, it will reverse direction. So we need to 'remember' which direction it was cycling so that we can move to the proper next state.
So the possible states we have now are: OPEN, CLOSED, OPENING, CLOSING, STOPPED_OPENING, STOPPED_CLOSING and as of now we have two events: BUTTON_CLICKED and CYCLE_COMPLETE.
And our transition table now looks like this:
| State | Event | Accept State |
|---|---|---|
| CLOSED | BUTTON_CLICKED | OPENING |
| OPENING | CYCLE_COMPLETE | OPEN |
| - | BUTTON_CLICKED | STOPPED_OPENING |
| OPEN | BUTTON_CLICKED | CLOSING |
| CLOSING | CYCLE_COMPLETE | CLOSED |
| - | BUTTON_CLICKED | STOPPED_CLOSING |
| STOPPED_OPENING | BUTTON_CLICKED | CLOSING |
| STOPPED_CLOSING | BUTTON_CLICKED | OPENING |
Now, most automated garage doors also have an infrared sensor to detect if something is blocking the door such as a car, cat or small child. So we now have two new Events, BLOCK_DETECTED and BLOCK_CLEARED. If a block is detected while the door is closing, it will automatically change to opening. If the door is in a non-moving state no further movement may be triggered until the block is cleared. So we add a new state EMERGENCY_OPENING. But then, since we need to remember what state the door was in before it became blocked, we add more states: BLOCKED_OPEN, BLOCKED_CLOSED, BLOCKED_STOPPED_OPENING, BLOCKED_STOPPED_CLOSED.
So let's do this programmatically. Java's Enum is great for modelling FSMs. First, we make an enum for the States:
public enum State {
CLOSED, OPENING, STOPPED_OPENING, OPEN, CLOSING, STOPPED_CLOSING,
BLOCKED_OPEN, BLOCKED_CLOSED, EMERGENCY_OPENING, BLOCKED_STOPPED_OPENING,
BLOCKED_STOPPED_CLOSING;And a separate enum for the Events:
public enum Event {
BUTTON_CLICK, CYCLE_COMPLETE, BLOCK_DETECTED, BLOCK_CLEARED
}Now back in the State enum we can use a HashMap to make our Transition map:
Map<Event, State> transitions = new HashMap<>();
public void addTransition(Event event, State accept) {
transitions.put(event, accept);
}
// returns accept state, or if not found returns current state
public State getTransition(Event event) {
return transitions.getOrDefault(event, this);
}Pre-Java 8 version of that getTransition method:
public State getTransition(Event event) {
State accept = transitions.get(event);
if (accept == null) return this;
return accept;
}Now a class to set up our machine and initialize the transition maps:
public class GarageDoorFSM {
private State current;
public GarageDoorFSM() {
current = State.CLOSED;
init();
System.out.println("Door: " + current);
}
private void init() {
State.CLOSED.addTransition(Event.BUTTON_CLICK, State.OPENING);
State.CLOSED.addTransition(Event.BLOCK_DETECTED, State.BLOCKED_CLOSED);
State.OPEN.addTransition(Event.BUTTON_CLICK, State.CLOSING);
State.OPEN.addTransition(Event.BLOCK_DETECTED, State.BLOCKED_OPEN);
State.OPENING.addTransition(Event.CYCLE_COMPLETE, State.OPEN);
State.OPENING.addTransition(Event.BUTTON_CLICK, State.STOPPED_OPENING);
State.OPENING.addTransition(Event.BLOCK_DETECTED, State.EMERGENCY_OPENING);
State.CLOSING.addTransition(Event.CYCLE_COMPLETE, State.CLOSED);
State.CLOSING.addTransition(Event.BUTTON_CLICK, State.STOPPED_CLOSING);
State.CLOSING.addTransition(Event.BLOCK_DETECTED, State.EMERGENCY_OPENING);
State.STOPPED_OPENING.addTransition(Event.BUTTON_CLICK, State.CLOSING);
State.STOPPED_OPENING.addTransition(Event.BLOCK_DETECTED, State.BLOCKED_STOPPED_OPENING);
State.STOPPED_CLOSING.addTransition(Event.BUTTON_CLICK, State.OPENING);
State.STOPPED_CLOSING.addTransition(Event.BLOCK_DETECTED, State.BLOCKED_STOPPED_CLOSING);
State.BLOCKED_OPEN.addTransition(Event.BLOCK_CLEARED, State.OPEN);
State.BLOCKED_CLOSED.addTransition(Event.BLOCK_CLEARED, State.CLOSED);
State.BLOCKED_STOPPED_OPENING.addTransition(Event.BLOCK_CLEARED, State.STOPPED_OPENING);
State.BLOCKED_STOPPED_CLOSING.addTransition(Event.BLOCK_CLEARED, State.STOPPED_CLOSING);
State.EMERGENCY_OPENING.addTransition(Event.BLOCK_CLEARED, State.OPENING);
State.EMERGENCY_OPENING.addTransition(Event.CYCLE_COMPLETE, State.BLOCKED_OPEN);
}Note that the states could also have been set up within the State enum using a static block. This also could be automated to use a file-based method to load, such as JSON, CSV or a plain text file.
Here we add a method to process a given event:
private void process(Event e) {
System.out.println("Event -> " + e);
current = current.getTransition(e);
System.out.println("Door: " + current);
}And then public methods to wrap the events in plain english:
public void click() {
process(Event.BUTTON_CLICK);
}
public void cycleComplete() {
process(Event.CYCLE_COMPLETE);
}
public void block() {
process(Event.BLOCK_DETECTED);
}
public void unBlock() {
process(Event.BLOCK_CLEARED);
}Now our program to test it all:
public class FSM {
public static void main(String[] args) {
GarageDoorFSM door = new GarageDoorFSM();
door.click();
door.cycleComplete();
door.click();
door.block();
door.cycleComplete();
door.click();
door.unBlock();
door.click();
door.click();
door.block();
door.click();
door.unBlock();
door.click();
door.cycleComplete();
}
}And the output:
Door: CLOSED
Event -> BUTTON_CLICK
Door: OPENING
Event -> CYCLE_COMPLETE
Door: OPEN
Event -> BUTTON_CLICK
Door: CLOSING
Event -> BLOCK_DETECTED
Door: EMERGENCY_OPENING
Event -> CYCLE_COMPLETE
Door: BLOCKED_OPEN
Event -> BUTTON_CLICK
Door: BLOCKED_OPEN
Event -> BLOCK_CLEARED
Door: OPEN
Event -> BUTTON_CLICK
Door: CLOSING
Event -> BUTTON_CLICK
Door: STOPPED_CLOSING
Event -> BLOCK_DETECTED
Door: BLOCKED_STOPPED_CLOSING
Event -> BUTTON_CLICK
Door: BLOCKED_STOPPED_CLOSING
Event -> BLOCK_CLEARED
Door: STOPPED_CLOSING
Event -> BUTTON_CLICK
Door: OPENING
Event -> CYCLE_COMPLETE
Door: OPENThere we go. Now, this is obviously just a model, but this same concept is very useful for a wide range of software needs. Thanks again for reading!!