package org.kodejava.util;
import java.util.LinkedList;
import java.util.List;
public class LinkedListToArray {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("Blue");
list.add("Green");
list.add("Purple");
list.add("Orange");
// Converting LinkedList to array can be done by calling the toArray()
// method of the List;
String[] colors = new String[list.size()];
list.toArray(colors);
for (String color : colors) {
System.out.println("color = " + color);
}
}
}
Tag Archives: List
How do I retrieve a list of Hibernate’s persistent objects?
In this example we add the function to read a list of records in our LabelService class. This function will read all Label persistent object from database. You can see the other functions such as saveLabel, getLabel and deleteLabel in the related example section of this example.
package org.kodejava.hibernate.service;
import org.hibernate.Session;
import org.kodejava.hibernate.SessionFactoryHelper;
import org.kodejava.hibernate.model.Label;
import java.util.List;
public class LabelService {
public List<Label> getLabels() {
Session session =
SessionFactoryHelper.getSessionFactory().getCurrentSession();
session.beginTransaction();
// We read labels record from database using a simple Hibernate
// query, Hibernate Query Language (HQL).
List<Label> labels = session.createQuery("from Label", Label.class)
.list();
session.getTransaction().commit();
return labels;
}
public void saveLabel(Label label) {
// To save an object we first get a session by calling
// getCurrentSession() method from the SessionFactoryHelper class.
// Next we create a new transaction, save the Label object and
// commit it to database,
Session session = SessionFactoryHelper.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
session.save(label);
session.getTransaction().commit();
}
}
package org.kodejava.hibernate;
import org.kodejava.hibernate.model.Label;
import org.kodejava.hibernate.service.LabelService;
import java.util.Date;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
LabelService service = new LabelService();
// Creates a Label object we are going to store in the database.
// We set the name, modified by and modified date information.
Label newLabel = new Label();
newLabel.setName("PolyGram");
newLabel.setCreated(new Date());
// Call the LabelManager saveLabel method.
service.saveLabel(newLabel);
List<Label> labels = service.getLabels();
for (Label label : labels) {
System.out.println("Label = " + label);
}
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
How do I convert Array to Collection?
package org.kodejava.util;
import java.util.Arrays;
import java.util.List;
public class ArrayToListExample {
public static void main(String[] args) {
// Creates an array of object, in this case we create an
// Integer array.
Integer[] numbers = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};
// Convert the created array above to collection, in this
// example we convert it to a List.
List<Integer> list = Arrays.asList(numbers);
// We've got a list of our array here and iterate it.
for (Integer number : list) {
System.out.print(number + ", ");
}
}
}
How do I convert a collection object into an array?
To convert collection-based object into an array we can use toArray() or toArray(T[] a) method provided by the implementation of Collection interface such as java.util.ArrayList.
package org.kodejava.util;
import java.util.List;
import java.util.ArrayList;
public class CollectionToArrayExample {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
words.add("Kode");
words.add("Java");
words.add("-");
words.add("Learn");
words.add("Java");
words.add("by");
words.add("Examples");
String[] array = words.toArray(new String[0]);
for (String word : array) {
System.out.println(word);
}
}
}
Our code snippet result is shown below:
Kode
Java
-
Learn
Java
by
Examples
How do I convert an array into a collection object?
To convert array based data into List / Collection based we can use java.util.Arrays class. This class provides a static method asList(T... a) that converts array into List / Collection.
package org.kodejava.util;
import java.util.Arrays;
import java.util.List;
public class ArrayAsListExample {
public static void main(String[] args) {
String[] words = {"Happy", "New", "Year", "2021"};
List<String> list = Arrays.asList(words);
for (String word : list) {
System.out.println(word);
}
}
}
The results of our code are:
Happy
New
Year
2021
How do I know if an ArrayList contains a specified item?
In this example we are going to learn how to find out if an ArrayList object contains the specified element. To check if an ArrayList object contains the specified element we can use the contains(Object o) method. This method returns a boolean true when the specified element is found in the ArrayList, otherwise return false. The method returns true if and only if the list contains at least one element, where the element equals to the item in the list.
Let’s see the code snippet below to demonstrate it.
package org.kodejava.util;
import java.util.ArrayList;
import java.util.List;
public class ArrayListContainsExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
// Check to see if the list contains "Item 1".
String itemToFind = "Item 1";
System.out.println("contains(" + itemToFind + "): " + list.contains(itemToFind));
// Check to see if the list contains "Item 20".
itemToFind = "Item 20";
System.out.println("contains(" + itemToFind + "): " + list.contains(itemToFind));
}
}
The output of the code snippet above are:
contains(Item 1): true
contains(Item 20): false
