Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Monday, October 1, 2018
How to Find Multiple Missing Integers in Given Array With Duplicates in Java?
I found this interesting problem here:
https://javarevisited.blogspot.com/2014/11/how-to-find-missing-number-on-integer-array-java.html
but I think the solution is too long, see mine below:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MissingIntegers {
public static void main(String[] args) {
Set<Integer> s1 = new HashSet<>(Arrays.asList(new Integer[]{1, 2, 3, 4, 9, 8}));
Set<Integer> s2 = new HashSet<>(IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList()));
s2.removeAll(s1);
System.out.println(s2);
}
}
In fact we don't need the intermediate List in the case of Set 2, we can use the toCollection method of Collectors:
Set<Integer> s2 = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toCollection(HashSet::new));
Next day I came up with another solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MissingIntegers {
public static void main(String[] args) {
List<Integer> L1 = Arrays.asList(new Integer[]{1, 2, 3, 4, 9, 2, 8});
List<Integer> L2 = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toCollection(ArrayList::new));
L2.removeAll(L1);
System.out.println(L2);
}
}
Friday, August 31, 2018
Using Apache Ant in Netbeans
The following ant code is run inside the -post-jar target in a Netbeans Java project.
<!--
Copy the .jar file and the zipped directory dist
to deploy folder in Z: drive
-->
<echo message="Zip and copy: ${application.title}" />
<!--
A file name property for the zip file we are going to create
-->
<local name="zipfile" />
<property name="zipfile" value="${application.title}-dist.zip" />
<!-- The creation of the zip file -->
<zip destfile="${zipfile}">
<fileset dir="${dist.dir}/" />
</zip>
<!-- Copying the 2 files -->
<copy todir="Z:/deploy" overwrite="true">
<resources>
<file file="${dist.jar}" />
<file file="${zipfile}" />
</resources>
</copy>
<delete file="${zipfile}" />
<!-- End of the copy -->
<!--
Copy the .jar file and the zipped directory dist
to deploy folder in Z: drive
-->
<echo message="Zip and copy: ${application.title}" />
<!--
A file name property for the zip file we are going to create
-->
<local name="zipfile" />
<property name="zipfile" value="${application.title}-dist.zip" />
<!-- The creation of the zip file -->
<zip destfile="${zipfile}">
<fileset dir="${dist.dir}/" />
</zip>
<!-- Copying the 2 files -->
<copy todir="Z:/deploy" overwrite="true">
<resources>
<file file="${dist.jar}" />
<file file="${zipfile}" />
</resources>
</copy>
<delete file="${zipfile}" />
<!-- End of the copy -->
Tuesday, March 27, 2018
Style Guidelines for Local Variable Type Inference in Java
Stuart W. Marks just published a very useful guidelines for the use of the newly introduced local variable type inference in Java 10:
var list = List.of("One", "Two", 3);
http://openjdk.java.net/projects/amber/LVTIstyle.html
Tuesday, January 9, 2018
Thursday, June 16, 2016
Wednesday, March 2, 2016
Implementing equals in Java
public class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
}
public boolean equals(Object o) {
boolean result = false;
if (o instanceof Employee) {
Employee other = (Employee) o;
if (this.employeeId == other.employeeId
&& this.firstName.equals(other.firstName)
&& this.lastName.equals(other.lastName))
{
result = true;
}
}
return result;
}
The following program should print All good!:
package test; /** * * @author janeiros@j4neiros.us
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Equals e = new Equals(3, "jjjj", "eee");
if (e.equals(null)) {
System.out.println("Not good!");
} else {
System.out.println("All good!");
}
}
}
Wednesday, September 9, 2015
Java Queues - poll, offer, etc
| Throws ex. | Special v. | Blocks | Times out
--------+------------+------------+--------+---------------------
Insert | add(e) | offer(e) | put(e) | offer(e, time, unit)
Remove | remove() | poll() | take() | poll(time, unit)
Examine | element() | peek() | N/A | N/A
Tuesday, August 18, 2015
JavaFX for dummies
Reading JavaFX for Dummies these last summer days!
In the beginning there was AWT, the Abstract Window Toolkit. AWT was Java’s first system for displaying window-based user interfaces in Java. AWT begat Swing, which soon became the preferred way to create user-friendly applications in Java.
In the beginning there was AWT, the Abstract Window Toolkit. AWT was Java’s first system for displaying window-based user interfaces in Java. AWT begat Swing, which soon became the preferred way to create user-friendly applications in Java.
But then there was JavaFX, the worthy successor to the GUI throne. JavaFX is designed to create stunning user interfaces that can run on a wide variety of devices, including traditional desktop and portable computers, tablets, smartphones, TV set-top boxes, game consoles, and many other types of devices.
Until recently, JavaFX was the red-headed stepchild of the Java world. It co-existed with Java, but wasn’t an official part of Java. But beginning with Java version 8, JavaFX is now fully integrated into Java. And while JavaFX and Swing coexist today, Oracle has made it clear that Swing is in its twilight and JavaFX represents the future of user-interface programming.
Monday, October 27, 2014
Guava Files class
Thursday, September 25, 2014
Java 8 installation on Fedora 14
- Download Java SDK/JRE from Oracle: http://www.oracle.com/technetwork/java/javase/downloads/server-jre8-downloads-2133154.html. (I selected the server version).
- Extract the files into /usr/local/java. (I created the java directory).
- Use the alternatives program to create the necessary links:
- [root@Fedora-test ~]# alternatives --install /usr/bin/java java /usr/local/java/jdk1.8.0_20/bin/java 20000
- Run the alternatives config command:
[root@Fedora-test ~]# alternatives --config java
Select the option for Java 8
- Test it:
[root@Fedora-test ~]# java -versionThe following link was useful: http://www.dafoot.co.uk/index.php/menuitemcomputing/linux/101-fedora-12-alternatives-program-to-manage-java-runtimes-jdkjre
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
Friday, July 6, 2012
How to Initialise a static Map in Java
private static final Map<String, String> channelTypes =
new HashMap<String, String>();
static {
channelTypes.put("I", "Incremental");
channelTypes.put("N", "Instrument");
channelTypes.put("S", "Snapshot");
}
private static final Map<String, String> channelTypes =
new HashMap<String, String>() {
{
put("I", "Incremental");
put("N", "Instrument");
put("S", "Snapshot");
}
};
new HashMap<String, String>();
static {
channelTypes.put("I", "Incremental");
channelTypes.put("N", "Instrument");
channelTypes.put("S", "Snapshot");
}
private static final Map<String, String> channelTypes =
new HashMap<String, String>() {
{
put("I", "Incremental");
put("N", "Instrument");
put("S", "Snapshot");
}
};
Thursday, May 24, 2012
Five Fridays in July every 823 years == false
package pkg5fridaysinjuly;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
private static final int NUMBER_OF_YEARS = 823;
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
int firstYear = c.get(Calendar.YEAR);
int lastYear = firstYear - NUMBER_OF_YEARS;
c.set(firstYear, Calendar.JULY, 1);
System.out.println("Initial: " + firstYear);
System.out.println("Last: " + lastYear);
for (int y = firstYear; y >= lastYear; y--) {
c.set(y, Calendar.JULY, 1);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
System.out.println(y + " Found!");
}
}
}
}
Friday, June 24, 2011
AWK and Java CLASSPATH
From http://unix-simple.blogspot.com/2008/12/dynamically-building-java-classpaths.html
for line in $java_dir/*.jar
do
CLASSPATH="$CLASSPATH:$line"
done
Lets put AWK to iterate:
CLASSPATH=$CLASSPATH:$(ls *.jar | awk 'BEGIN { ORS = ":" } { print }')
Maybe a shellish way of do it:
CLASSPATH=$CLASSPATH:`echo *.jar | sed 's/ /:/g'`
for line in $java_dir/*.jar
do
CLASSPATH="$CLASSPATH:$line"
done
Lets put AWK to iterate:
CLASSPATH=$CLASSPATH:$(ls *.jar | awk 'BEGIN { ORS = ":" } { print }')
Maybe a shellish way of do it:
CLASSPATH=$CLASSPATH:`echo *.jar | sed 's/ /:/g'`
Tuesday, June 23, 2009
The Thread Control Escape Rule
When trying to determine if your code's access of a certain resource is thread safe you can use the thread control escape rule:
If a resource is created, used and disposed within
the control of the same thread,
and never escapes the control of this thread,
the use of that resource is thread safe.
Resources can be any shared resource like an object, array, file, database connection, socket etc. In Java you do not always explicitly dispose objects, so "disposed" means losing or null'ing the reference to the object.
Taken from: http://tutorials.jenkov.com/java-concurrency/thread-safety.html
Subscribe to:
Posts (Atom)


