DEV Community

smolthing
smolthing

Posted on

2

Almost all the types in Java

Java Data Types - group of objects

Image description

Basic Examples

// example.java

import java.lang.Boolean;
import java.lang.Double;
import java.lang.Float;
import java.lang.Integer;
import java.lang.Long;
import java.lang.String;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class Example {
    public static void main(String[] args) {
        // Primitive types
        byte myByte = 127; // 8-bit, 2^8, -128 to 127
        short myShort = 32767; // 16-bit, 2^16. -32768 to 32767
        int myInt = 2147483647; // 32-bit, 2^32, -2147483648 to -2147483647
        long myLong = 9223372036854775807L; // 64-bit, 2^64, -9223372036854775808 to 9223372036854775807L, L otherwise
        float myFloat = 3.14f; // 32-bit, 2^32, 
        double myDouble = 3.141592653589793; // 64-bit, 2^64
        char myChar = 'A'; // 16-bit, 2^16, 
        boolean myBoolean = true; // 1-bit, 2^1

        // Reference types
        String myString = "Hello, world!";
        Class myClass = String.class;
        Object myObject = new Object();

        // Collection data types
        ArrayList<String> myArrayList = new ArrayList<>();
        myArrayList.add("Apple");
        myArrayList.add("Orange");
        myArrayList.add("Banana");

        LinkedList<String> myLinkedList = new LinkedList<>();
        myLinkedList.add("Apple");
        myLinkedList.add("Orange");
        myLinkedList.add("Banana");

        Set<String> mySet = new HashSet<>();
        mySet.add("Apple");
        mySet.add("Orange");
        mySet.add("Banana");

        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("Apple", 1);
        myMap.put("Orange", 2);
        myMap.put("Banana", 3);

        // Print out the result
        System.out.println("myByte: " + myByte);
        System.out.println("myShort: " + myShort);
        System.out.println("myInt: " + myInt);
        System.out.println("myLong: " + myLong);
        System.out.println("myFloat: " + myFloat);
        System.out.println("myDouble: " + myDouble);
        System.out.println("myChar: " + myChar);
        System.out.println("myBoolean: " + myBoolean);

        System.out.println("myString: " + myString);
        System.out.println("myClass: " + myClass);
        System.out.println("myObject: " + myObject);

        System.out.println("myArrayList: " + myArrayList);
        System.out.println("myLinkedList: " + myLinkedList);
        System.out.println("mySet: " + mySet);
        System.out.println("myMap: " + myMap);
    }
}
Enter fullscreen mode Exit fullscreen mode

Results

$ java ./example.java
myByte: 127
myShort: 32767
myInt: 2147483647
myLong: 9223372036854775807
myFloat: 3.14
myDouble: 3.141592653589793
myChar: A
myBoolean: true
myString: Hello, world!
myClass: class java.lang.String
myObject: java.lang.Object@3745e5c6
myArrayList: [Apple, Orange, Banana]
myLinkedList: [Apple, Orange, Banana]
mySet: [Apple, Orange, Banana]
myMap: {Apple=1, Orange=2, Banana=3}
Enter fullscreen mode Exit fullscreen mode

1. Set <-- HashSet

In 3Sum, we use hashset to improve lookup time for duplicate values.

...
Set<List<Integer>> set = new HashSet<>();
set.add(Arrays.asList(1, 2, 3)); 
return new ArrayList<>(set);
...
Enter fullscreen mode Exit fullscreen mode

HashSet is a class that implements the Set interface in Java

Methods

Set<List<Integer>> set = new HashSet<>(); // slow, less memory used
HashSet<List<Integer>> set = new HashSet<>(); // fast, more memory
Enter fullscreen mode Exit fullscreen mode

Set = abstract class with basic functionality
HashSet = concrete class that implements Set interface

set.iteractor()/toArray()/add()/remove()/contains()/size()/isEmpty()
hashset.add()/remove()/contains()/size()/isEmpty()

Convert ArrayList to/from HashSet

HashSet<String> hashSet = new HashSet<>();
hashSet.add("why so many types");
ArrayList<String> arrayList = new ArrayList<>(hashSet);
Enter fullscreen mode Exit fullscreen mode
HashSet<String> hashSet = new HashSet<>();
hashSet.add("help my brain");
ArrayList<String> arrayList = (ArrayList<String>) hashSet.toArray();
Enter fullscreen mode Exit fullscreen mode

Others

I will update them in the future. My brain is on max CPU at the moment.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more