<CoreJava> 12.4类型变量的定义

本文介绍Java中如何使用泛型及限定类型变量的方法,通过示例讲解如何确保泛型类或方法中的类型参数具备特定的方法如compareTo。同时,提供了一个求最小最大值的泛型方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时类或方法需要对类型变量加以约束。

  1. class ArrayAlg {  
  2.     public static <T> T min(T[] a) {  
  3.         if (a == null || a.length == 0)  
  4.             return null;  
  5.         T smallest = a[0];  
  6.         for (int i = 1; i < a.length; i++)  
  7.             if (smallest.compareTo(a[i]) > 0)  
  8.                 smallest = a[i];  
  9.         return smallest;  
  10.     }  
  11. }  

问题:min方法内部的变量smallest类型为T,意味着它可以是任意类的对象。那么如何确定T所属的类有compareTo方法呢?

解答:将T限制为实现Comparable接口的类。通过对类型变量T设置限定实现这一点:public static <T extends Comparable> T min(T[] a).......

注意:一个类型变量或通配符可以有多个限定:T extends Comparable & Serializable(限定类型用&分隔,类型变量用,分隔)

例12-2中重新编写了一个泛型方法minmax。方法是计算数组中的最大值和最小值并返回Pair<T>

  1. package core.pair_12_2;  
  2.   
  3. import java.util.Calendar;  
  4. import java.util.GregorianCalendar;  
  5.   
  6. /** 
  7.  * @author vincent 
  8.  */  
  9.   
  10. public class PairTest2 {  
  11.   
  12.     public static void main(String[] args) {  
  13.         GregorianCalendar[] birthdays =   
  14.             {  
  15.              new GregorianCalendar(1906, Calendar.DECEMBER, 9),  
  16.              new GregorianCalendar(1815, Calendar.DECEMBER, 10),  
  17.              new GregorianCalendar(1903, Calendar.DECEMBER, 3),  
  18.              new GregorianCalendar(1910, Calendar.JUNE, 22),  
  19.             };  
  20.         Pair<GregorianCalendar> mm = ArrayAlg.minmax(birthdays);  
  21.         System.out.println("min = " + mm.getFirst().getTime());  
  22.         System.out.println("max = " + mm.getSecond().getTime());  
  23.     }  
  24. }  
  25.   
  26. class ArrayAlg {  
  27.   
  28.     @SuppressWarnings({ "rawtypes""unchecked" })  
  29.     public static <T extends Comparable> Pair<T> minmax(T[] a) {  
  30.         if (a == null || a.length == 0return null;  
  31.         T min = a[0];  
  32.         T max = a[0];  
  33.         for (int i = 1; i < a.length; i++) {  
  34.             if (min.compareTo(a[i]) > 0) min = a[i];  
  35.             if (max.compareTo(a[i]) < 0) max = a[i];  
  36.         }  
  37.         return new Pair<T>(min, max);  
  38.     }  
  39. }  
  40.   
  41. class Pair<T> {  
  42.     private T first;  
  43.     private T second;  
  44.   
  45.     public Pair() {  
  46.         first = null;  
  47.         second = null;  
  48.     }  
  49.     public Pair(T first, T second) {  
  50.         this.first = first;  
  51.         this.second = second;  
  52.     }  
  53.       
  54.     public T getFirst() {  
  55.         return first;  
  56.     }  
  57.     public T getSecond() {  
  58.         return second;  
  59.     }  
  60.       
  61.     public void setFirst(T newValue) {  
  62.         first = newValue;  
  63.     }  
  64.     public void setSecond(T newValue) {  
  65.         second = newValue;  
  66.     }  
  67. }  

转载于:https://www.cnblogs.com/vincent-hv/archive/2013/02/28/2958546.html

你可以使用Java的反序列化方法来解压`Flux<Map<String, Object>>`到`Map<String, Object>`类型变量中。首先,你需要将`Flux<Map<String, Object>>`转换为字节数组,然后使用Java的反序列化工具将字节数组转换为`Map<String, Object>`对象。 下面是一个示例代码: ```java import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import reactor.core.publisher.Flux; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.Map; public class FluxMapDeserializer { public Map<String, Object> deserializeFluxMap(Flux<Map<String, Object>> fluxMap) throws IOException, ClassNotFoundException { byte[] bytes = fluxMap .collectList() .map(list -> list.toString().getBytes()) .block(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); Map<String, Object> resultMap = (Map<String, Object>) ois.readObject(); ois.close(); bis.close(); return resultMap; } } ``` 在上述示例中,我们使用了Spring Framework的`DataBufferUtils`来将`Flux<Map<String, Object>>`转换为字节数组。然后,我们使用`ByteArrayInputStream`和`ObjectInputStream`来反序列化字节数组为`Map<String, Object>`。 请注意,上述代码中的示例只是一种方法,具体的实现可能会因你所使用的框架或库而有所不同。确保根据自己的需求进行适当的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值