Answers for "generic function in java"

9

java generic type method

// generic methods

public <T> List<T> fromArrayToList(T[] a) {   
	    return Arrays.stream(a).collect(Collectors.toList());
	}

public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
	    return Arrays.stream(a)
	      .map(mapperFunction)
	      .collect(Collectors.toList());
	}

// bounded generics

public <T extends Number> List<T> fromArrayToList(T[] a) {
	    ...
	}

//multiple bounds

<T extends Number & Comparable>

// upper bound wildcards

public static void paintAllBuildings(List<? extends Building> buildings) {
	    ...
	}
    
// lower bound wildcard

<? super T>
Posted by: Guest on October-31-2020
9

java generic type method

// generic methods

public <T> List<T> fromArrayToList(T[] a) {   
	    return Arrays.stream(a).collect(Collectors.toList());
	}

public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
	    return Arrays.stream(a)
	      .map(mapperFunction)
	      .collect(Collectors.toList());
	}

// bounded generics

public <T extends Number> List<T> fromArrayToList(T[] a) {
	    ...
	}

//multiple bounds

<T extends Number & Comparable>

// upper bound wildcards

public static void paintAllBuildings(List<? extends Building> buildings) {
	    ...
	}
    
// lower bound wildcard

<? super T>
Posted by: Guest on October-31-2020
0

Create Generic Method

public static T GetQueryString<T>(string key, T defaultValue) {...}
Posted by: Guest on June-30-2021
0

Create Generic Method

public static T GetQueryString<T>(string key, T defaultValue) {...}
Posted by: Guest on June-30-2021
0

generics Interface in java

/*We can define our own classes with generics type. A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter.

To understand the benefit, let’s say we have a simple class as:*/
package com.journaldev.generics;

public class GenericsTypeOld {

	private Object t;

	public Object get() {
		return t;
	}

	public void set(Object t) {
		this.t = t;
	}

        public static void main(String args[]){
		GenericsTypeOld type = new GenericsTypeOld();
		type.set("Pankaj"); 
		String str = (String) type.get(); //type casting, error prone and can cause ClassCastException
	}
}
Posted by: Guest on December-06-2020
0

generics Interface in java

/*We can define our own classes with generics type. A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter.

To understand the benefit, let’s say we have a simple class as:*/
package com.journaldev.generics;

public class GenericsTypeOld {

	private Object t;

	public Object get() {
		return t;
	}

	public void set(Object t) {
		this.t = t;
	}

        public static void main(String args[]){
		GenericsTypeOld type = new GenericsTypeOld();
		type.set("Pankaj"); 
		String str = (String) type.get(); //type casting, error prone and can cause ClassCastException
	}
}
Posted by: Guest on December-06-2020
-1

generics in java

import java.util.Scanner;
import reader.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String filePath = scanner.nextLine().trim();
        System.out.println("The file path is " + filePath);
        String type = filePath.substring(filePath.lastIndexOf('.') + 1).replaceAll("\\W", "");
        System.out.println("The reader type is " + type);
        AbstractReaderService reader = getReaderService(type);
        // reader.read(filePath);
        // reader.printContent();

    }

    private static AbstractReaderService getReaderService (String type) throws Exceptions.TypeNotFoundException {
        switch (type) {
            case "txt": return new TxtReaderService();
            case "json": return new JsonReaderService();
            default: throw new Exceptions().new TypeNotFoundException("There is no \"" + type + "\" type of reader.");
        }
    }
}
Posted by: Guest on August-10-2021
-1

generics in java

import java.util.Scanner;
import reader.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String filePath = scanner.nextLine().trim();
        System.out.println("The file path is " + filePath);
        String type = filePath.substring(filePath.lastIndexOf('.') + 1).replaceAll("\\W", "");
        System.out.println("The reader type is " + type);
        AbstractReaderService reader = getReaderService(type);
        // reader.read(filePath);
        // reader.printContent();

    }

    private static AbstractReaderService getReaderService (String type) throws Exceptions.TypeNotFoundException {
        switch (type) {
            case "txt": return new TxtReaderService();
            case "json": return new JsonReaderService();
            default: throw new Exceptions().new TypeNotFoundException("There is no \"" + type + "\" type of reader.");
        }
    }
}
Posted by: Guest on August-10-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language