package org.kodejava.util;
import java.util.ArrayList;
import java.util.LinkedList;
public class CollectionToArrayList {
public static void main(String[] args) {
// We create LinkedList collection type at put some values
// in it. Here we put A, B, C and D letter into it.
LinkedList<String> linkedList = new LinkedList<>();
linkedList.push("A");
linkedList.push("B");
linkedList.push("C");
linkedList.push("D");
// Let say you want to convert it to other type of collection,
// for instance here we convert it into ArrayList. To do it
// we can pass the collection created above as a parameter to
// ArrayList constructor.
ArrayList<String> arrayList = new ArrayList<>(linkedList);
// Now we have converted the collection into ArrayList and
// printed what is inside.
for (String s : arrayList) {
System.out.println("s = " + s);
}
}
}
Category Archives: Util Package
How do I get number of days in a month?
Let say you want to know the number of days in a month, or we can say it as the last date of a month. The example below shows you how to obtain the number of days or the date.
package org.kodejava.util;
import java.util.Calendar;
public class MonthDaysExample {
public static void main(String[] args) {
// First get an instance of calendar object.
Calendar calendar = Calendar.getInstance();
// We'll set the date of the calendar to the following
// date. We can use constant variable in the calendar
// for months value (JANUARY - DECEMBER). Be informed that
// month in Java started from 0 instead of 1.
int year = 2021;
int month = Calendar.FEBRUARY;
int date = 1;
// We have a new date of 2021-02-01
calendar.set(year, month, date);
// Here we get the maximum days for the date specified
// in the calendar. In this case we want to get the number
// of days for february 2021
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Max Day: " + maxDay);
// Here we want to see what is the days for february on
// a leap year.
calendar.set(2020, Calendar.FEBRUARY, 1);
maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Max Day: " + maxDay);
}
}
The result of the code snippet above:
Max Day: 28
Max Day: 29
In JDK 8 you can use the new Date Time API to get the number of days in a month. Here an example that show you how to do it: How do I get the length of month represented by a date object?.
How do I create an empty collection object?
Sometimes you need to return an empty collection from your Java methods. The java.util.Collections utility class have three different static constants for creating empty List, Set and Map.
Collections.EMPTY_LISTCollections.EMPTY_SETCollections.EMPTY_MAP
There are also methods when you want to create type-safe empty collections.
Collections.emptyList()Collections.emptySet()Collections.emptyMap()
Bellow it the code example.
package org.kodejava.util;
import java.util.*;
public class EmptyCollectionDemo {
public static void main(String[] args) {
List list = Collections.EMPTY_LIST;
System.out.println("list.size() = " + list.size());
Set set = Collections.EMPTY_SET;
System.out.println("set.size() = " + set.size());
Map map = Collections.EMPTY_MAP;
System.out.println("map.size() = " + map.size());
// For the type-safe example use the following methods.
List<String> strings = Collections.emptyList();
System.out.println("strings.size = " + strings.size());
Set<Long> longs = Collections.emptySet();
System.out.println("longs.size() = " + longs.size());
Map<String, Date> dates = Collections.emptyMap();
System.out.println("dates.size() = " + dates.size());
}
}
The output are:
list.size() = 0
set.size() = 0
map.size() = 0
strings.size = 0
longs.size() = 0
dates.size() = 0
How do I sort an array of objects?
In this example we are going to learn how to sort an array of objects. We start by using an array of String objects as can be seen in the code snippet below. We sort the contents of the array using Arrays.sort() method and print the sorted result. It was really simple.
String names[] = {"Bob", "Mallory", "Alice", "Carol"};
Arrays.sort(names);
System.out.println("Names = " + Arrays.toString(names));
Next, we will sort an array of our own object. It is a bit different compared to sorting an array of primitives. The first rule is we need our object to implements the Comparable interface. This interface have one contract we need to implement, the compareTo() contract.
The basic rule of the compareTo() method is to return 0 when objects value are equals, 1 if this object value is greater and -1 if this object value is smaller. In the Person class below we simply call the String object compareTo() method. See the Person class below for more details.
package org.kodejava.util.support;
public class Person implements Comparable<Person> {
private String name;
public Person(String name) {
this.name = name;
}
public int compareTo(Person person) {
return this.name.compareTo(person.name);
}
public String toString() {
return name;
}
}
In the snippet below we create four Person objects. We sort the Person object based on their name using the Arrays.sort() method and print out the array values.
Person persons[] = new Person[4];
persons[0] = new Person("Bob");
persons[1] = new Person("Mallory");
persons[2] = new Person("Alice");
persons[3] = new Person("Carol");
Arrays.sort(persons);
System.out.println("Persons = " + Arrays.toString(persons));
This is the main class where you can run all the snippet above:
package org.kodejava.util;
import org.kodejava.util.support.Person;
import java.util.Arrays;
public class ObjectSortExample {
public static void main(String[] args) {
String[] names = {"Bob", "Mallory", "Alice", "Carol"};
Arrays.sort(names);
System.out.println("Names = " + Arrays.toString(names));
Person[] persons = new Person[4];
persons[0] = new Person("Bob");
persons[1] = new Person("Mallory");
persons[2] = new Person("Alice");
persons[3] = new Person("Carol");
Arrays.sort(persons);
System.out.println("Persons = " + Arrays.toString(persons));
}
}
This snippet will print the following output:
Names = [Alice, Bob, Carol, Mallory]
Persons = [Alice, Bob, Carol, Mallory]
How do I search specific value in an array?
package org.kodejava.util;
import java.util.Arrays;
public class ArraySearchExample {
public static void main(String[] args) {
// We create an array of ints where the search will be done.
int[] items = {9, 5, 14, 6, 11, 28, 9, 16, 37, 3, 2};
// The Arrays.binarySearch() require us to sort the array
// items before we call the method. We can utilize the
// Arrays.sort() method to do this. If we did not sort the
// array the result will be undefined, the search process
// will return a negative result.
Arrays.sort(items);
// To search we use Arrays.binarySearch() methods which accept
// parameters of any array type. To search we passed the array
// to be searched and the key to be searched for.
//
// When the search item exist more than one in the array, this
// method gives no guarantee which one will be returned.
int needle = 9;
int index = Arrays.binarySearch(items, needle);
// Print out where the 9 number is located in the array.
System.out.println("Items: " + Arrays.toString(items));
System.out.println("Item " + needle + " is at index " + index);
}
}
There result of the code snippet:
Items: [2, 3, 5, 6, 9, 9, 11, 14, 16, 28, 37]
Item 9 is at index 5
