notes
  • computer-networking
    • extend-wifi-with-router
    • how-the-internet-works
    • idk
    • networking-devices
    • osi-model
    • tcp-ip
    • Types of VPN
  • databases
    • Foreign Keys
    • Redis
    • simple-queries
  • devops
    • ansible
    • Manual deployment
    • docker
    • Workflow file
    • nginx
    • promethues-grafana
    • terraform
  • hardware
    • Power
  • home-server
    • proxmox-basics
    • proxmox-setup
    • storage
  • languages-frameworks
    • programming-paradigms
    • programming-languages
      • Regex Notes
      • c
        • basics
        • pointers-memory
      • cpp
        • basics
        • running-cpp
      • php
        • basics
        • choizez
        • frameworks
          • laravel
      • python
        • venv
        • concepts
          • Using lambda
        • frameworks
          • django
            • django
            • start
      • java
        • advanced
          • functional-programming
          • reactive-programming
        • concepts
          • how-java-works
          • serialization
          • sockets
          • threads
        • extra
          • collection-framework
          • generics-and-wildcards
          • Regular Expressions (Regex)
          • streams
        • frameworks
          • orm
        • fundamentals
          • OOP
          • conditionals
          • data-structures
          • data-types
          • exceptions
          • files
          • Functions (aka method)
          • Loops
          • packages
          • type-casting
      • javascript
        • frameworks
          • morgan
          • Using Sequelize with PostgreSQL in JavaScript
  • operating-system
    • basics
    • linux-directories
    • Basic Unix Terminal Commands
  • others
    • dark-web
    • piracy
  • system-design
    • system-design
  • web-dev
    • full-stack
  • books
    • sicp
      • Recursion thought process
      • 1
        • 1.1
        • 1.2
        • 1.3
      • 2
        • 2.1
  • certifications
    • aws-certified-cloud-practitioner
      • core-services
      • other-services
    • comptia-a+
      • Cloud
      • hardware
      • Important terms
      • Important terms
      • Troubleshooting
  • cloud
    • aws
      • aws-cli
      • aws-ec2-deployment
  • dsa
    • algorithms
      • back-tracking
      • bfs
      • Binary Search
      • bit-manipulation
      • Bubble sort
      • bucket-sort
      • counting-sort
      • dfs
      • Divide & Conquer
      • djikstras-algorithm
      • dynamic-programming
      • external-sorting
      • greedy-algorithm
      • Heap sort
      • Insertion sort
      • kadanes-algorithm
      • Merge sort
      • Permutation
      • quick-sort
      • Radix Sort
      • recurrence-relation
      • recursion
      • Selection sort
      • sliding-window
      • subset
      • time-space-complexity
      • topological-sort
      • tree-traversals
      • Two Pointers Technique
    • data-structures
      • data-structures
  • security
    • authentication
      • What is JWT (JSON Web Token)?
    • software-architecture-design
      • design-patterns
Powered by GitBook
On this page
  • Generics
  • Popular generic type parameter naming convention
  • Wildcard
  • Unbounded wildcard
  • Upper-bounded wildcard
  • Lower-bounded wildcard
  • Bounded wildcards illustration
  • When to use generic or wildcards?
  1. languages-frameworks
  2. programming-languages
  3. java
  4. extra

generics-and-wildcards

Generics

Generics in Java provide a way to create classes, methods, and interfaces that can work with different data types

Mostly used for :

  • class / interface definition

  • method signature

  • variables

  • collections

Here we create a generic container which can store value of any data type

public class GenericContainer<T> {
    private T value;

    public GenericContainer(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public static void main(String[] args) {
        // Create a GenericContainer for an Integer
        GenericContainer<Integer> intContainer = new GenericContainer<>(42);
        int intValue = intContainer.getValue();
        System.out.println("Integer Value: " + intValue);

        // Create a GenericContainer for a String
        GenericContainer<String> strContainer = new GenericContainer<>("Hello, Generics!");
        String strValue = strContainer.getValue();
        System.out.println("String Value: " + strValue);
    }
}

Popular generic type parameter naming convention

Type Parameter
When to use?

T

Stands for "Type" and is a widely used identifier for a generic type.

E

Typically used for elements in collections (e.g., elements in a List or Set).

K,V

Commonly used for key and value types in key-value pair data structures like maps.

N

Often used to represent a number (e.g., Number in mathematics or numerical operations).

S,U,V

Generic type parameters used when you have more than one type parameter in a class or method.

Wildcard

Only used for :

  • collections (because they are designed to store multiple elements of various types)

Unbounded wildcard

public static void printList(List<?> list) {
  for (Object item : list) {
      System.out.print(item + " ");
  }
  System.out.println();
}

public static void main(String[] args) {
  List<Integer> integerList = List.of(1, 2, 3);
  List<String> stringList = List.of("Hello", "World");
}

Upper-bounded wildcard

Upper bounded means the wildcard ? which extends Numbers can only be a subtype of Numbers for eg. Numbers / Integer / Double


upper bounded by Number (is a subType , extends Number) , examples:

  • Number (itself)

  • Integer

  • Double

  • Float

public void method(List <? extends Number> list){ ... }


public static double sumOfList(List<? extends Number> numbers){
  double sum = 0;
  for (Number num : numbers){
    sum += num;
  }
  return sum;
}
public static void main(String[] args) {
  List<Integer> integers = Arrays.asList(1, 2, 3);
  List<Double> doubles = Arrays.asList(2.5, 4.8, 6.2);
}

Lower-bounded wildcard

Lower bounded means the wildcard ? which is a super to Integer can only be a supertype of Integer for eg. Integer / Object / Number


lower bounded by Integer (is a superType, super Integer) , examples (Integer's parents):

  • Object

  • Number

  • Integer (itself)

public void method(List <? super Integer> list){ ... }


public static void addNumbers(List<? super Integer> list) {
  // This method accepts a list of any type that is a supertype of Integer.

  list.add(1);
  list.add(2);
  list.add(3);
}

public static void main(String[] args) {
  List<Object> objectList = new ArrayList<>();
  List<Number> numberList = new ArrayList<>();
  List<Integer> integerList = new ArrayList<>();

  addNumbers(objectList);
  addNumbers(numberList);
  addNumbers(integerList);

  System.out.println("Objects: " + objectList);
  System.out.println("Numbers: " + numberList);
  System.out.println("Integers: " + integerList);
}

Bounded wildcards illustration

When to use generic or wildcards?

  • Use generics if you do not care about the type

  • Use wildcards you do care about the type (i.e. you need to perform an operation that only the T type has then you need to specify the restriction on the type [e.g. <T extends String>])

Previouscollection-frameworkNextRegular Expressions (Regex)

Last updated 1 month ago

Bounded Wildcards