When we are working with Java from a newbie, we’re always to think about some common tasks such as convert list to array, … In order to reduce the time of these problems, we can sum up all solutions to this article. It is convenient that we can see them again.

Let’s start.

Table of contents

Initialize List/ArrayList

  1. Initialization with List interface

    • Use the factory methods of Stream.

        Student obama =new Student(12,"Bill Gate");
        Student billgate = new Student(22, "Obama");
        List<Student> list = Stream.of(obama, billgate).collect(Collectors.toList());		
    • Use with Array.

        List<String> list = Arrays.asList("Obama", "Bill Gate");
    • Use List.of() method

        List<String> lst = new ArrayList<>(
            List.of("Hello", "World", "!")
    • Use another collection

        List<String> names = new ArrayList<>(lst);

Convert List to Array

  • First way - Use List.toArray().

      public class Student {
          private int age;
          private String name;
      // This way that we need to know about the size of array.
      List<Student> lst = new ArrayList<Student>();
      Student[] arr = lst.toArray(new Student[lst.size()]);
      // But we can not exactly be aware of this size of list. JVM can support with us.
      Student[] arrStudent=lst.toArray(new Student[0]);
  • Second way: Use Stream API to convert list to array.

    • convert List to Stream using List.stream().
    • use Stream.toArray() method to return an array that contains the elements of the stream.
      // Use method reference
      Student[] arr = lst.stream.toArray(Student[]::new);

Convert List to Set

Set<Foo> new HashSet<Foo>(list);

Convert List to Map

Assuming that we have a class Student with two attributes: id and name.

class Student {
    private int id;

    private String name;

At the moment, we want to convert list of Student object to map that contains id and name of each Student.

  • Use normal stream in Java 8

      List<Student> students = ...;
      Map<Integer, String> mp = students.stream()
                                          .collect(Collectors.toMap(Student::getId(), Student::getName()));

    When our list have many ids that are same, exception Duplicate key will be thrown.

  • Catch exception Duplicate key

      List<Student> students = ...;
      Map<Integer, String> mp = students.stream()
                                          .collect(Collectors.toMap(Student::getId, Student::getName, 
                                                      (oldValue, newValue) -> oldValue));     // (oldValue, newValue) -> newValue)
  • Keeping order of elements

      List<Student> students = ...;
      Map<Integer, String> mp = students.stream()
                                          .collect(Collectors.toMap(Student::getId, Student::getName, 
                                                      (oldValue, newValue) -> oldValue),     // (oldValue, newValue) -> newValue)

Find max/min value from an array

  1. Find max

    • With integer:

        List<Integer> listOfIntegers = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);
        Integer expectedResult = 89;
        Integer maxItem = listOfIntegers
                        .mapToInt(v -> v)
        // Or we can specify the natural order comparator
        Optional<Integer> maxItem = listOfIntegers.stream().max(Comparator.naturalOrder());
    • With Objects:

        public class Student {
            private String name;
            private int score;
        List<Student> students = new ArrayList<>(
            List.of(new Student("Mark", 8), 
                    new Student("Bill Gate", 10), 
                    new Student("Obama", 7), 
                    new Student("Johnson", 9))
    • Use reduce operation:

        Optional<Integer> maxItem = listOfIntegers.stream().reduce(Integer::max);
    • Use Collector:

        Optional<Integer> maxItem = listOfIntegers.stream().collect(Collectors.maxBy(Comparator.naturalOrder()));
    • Use IntSummaryStatistics:

        int maxItem = listOfIntegers.collect(Collectors.summarizingInt(Integer::intValue)).getMax();
  2. Find min

    With Integer: convert the stream to IntStream.

     List<Integer> listOfIntegers = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);
     Integer expectedResult = 89;
     Integer minItem = listOfIntegers
                     .mapToInt(v -> v)

    With Objects:


Convert Map to List

  • Convert Map<String, Double> to List<Pair<String, Double>>

    It means that we need to map Stream<Map.Entry<String, Double>> into a Stream<Pair<String, Double>>.

      List<Pair<String, Double>> mostRelevantTitles = 
                                          .sorted(Comparator.comparing(e -> -e.getValue()))
                                          .map(e -> new Pair<>(e.getKey(), e.getValue()))

    We can replace the comparator Comparator.comparing(e -> -e.getValue()) by Map.Entry.comparingByValue(Comparator.reverseOrder()).

  • Convert Map<String, Double> to List<String> or List<Double>

    There are some ways to convert them such as:

      Map<String, Double> map = new HashMap<>();
      List<String> lstKeys = new ArrayList(map.keySet());
      List<Double> lstValues = new ArrayList(map.values);
      List<String> lstKeys = map.keySet().stream().collect(Collectors.toList());
      List<Double> lstValues = map.values().stream().collect(Collectors.toList());

Get the earlies date of a list

In java.util.Date, there is available comparator operation. So, we can get it by the following:

Date minDate = Collections.min(listOfDates);

Convert String to Int

  • Use Integer.parseInt() method

    Because parseInt() method will throw a NumberFormatException, so, we have to handle it.

      int foo;
      try {
          foo = Integer.parseInt(str);
      } catch(NumberFormatException e) {
          foo = 0;

    Integer.parseInt() method returns a primitive int.

  • Use Ints method from Guava library

      import com.google.common.primitives.Ints;
      int foo = Optional.ofNullable(myString)
  • Use Integer.valueOf() method

      Integer result = Integer.valueOf(str);

    Integer.valueOf() method returns a new Integer() object.

Some ways to loop Map

Refer link.

Check whether an object is instance of which class

Use instanceof operator to check this condition. An additional benefit of using instanceof is that when used with a null reference, instanceof will return false, while a.getClass() would throw a NullPointerException.

if (a instanceof X) {
    // do something

Convert Java Date format to Javascript Date format and vice versa

Because Javascript does not have a built in date formatting ability.

And if we want to pass Date from Java to Javascript, it would be to convert Java Date to milliseconds using date.getTime(), create a Javascript Date initialized with this milliseconds value with new Date(milliseconds), then format the date with the means of Javascript Date object, like date.toLocaleString().

How to use equals() method and == operator

equals() method and == operator is used to compare objects to check equality.

  • To primitive value, equals() method and == operator has the same effection.
  • To objects:
    • == operator will compare the addresses of two objects. It does not compare values in objects.
    • Therefore, equals() method happens to solve this problem. equals() method is defined in Object class. So, in our objects, we have to override equals() method, and hashCode() method is always to accompany with equals() method. As per the API, the result returned from the hashCode() method for two objects must be the same if our equals() methods show that they are equivalent. The converse is not necessarily true.

      In default implementation of equals() method that provided in Object class, equals() method is as same as === operator.

So, to compare String objects, we should use equals() method rather than == operator.

And, we need to remember about some primitive data types in Java: byte, char, short, int, long, float, double, boolean, void.

Wrapping up

  • The background of data structure in Java: