需要使用java使用lamda表达式打印字符串的最后一位

需要使用java使用lamda表达式打印字符串的最后一位,java,Java,要使用lamda表达式打印字符串的最后一位数字。使用下面的代码可以打印完整的数字,但要打印最后一位数字 public static void main(String[] args) { List<TestDTO> studs = new ArrayList<>(); studs.add(new TestDTO("101", "Test 101")); studs.add(new Tes

要使用lamda表达式打印字符串的最后一位数字。使用下面的代码可以打印完整的数字,但要打印最后一位数字

public static void main(String[] args) {
        List<TestDTO> studs = new ArrayList<>();
        studs.add(new TestDTO("101", "Test 101"));
        studs.add(new TestDTO("102", "Test 102"));

        Map<String, TestDTO> mapDbCardDtl = studs.stream().collect(Collectors.toMap(TestDTO::getId, Function.identity()));

        Set<String> s = mapDbCardDtl.keySet();
        System.out.println("s: " + s.toString());
    }
从上述代码中获取输出

s: [101, 102]
预期产量

S : [1, 2]

如果您只对打印id中的最后一个数字感兴趣,这是一个写为字符串的数字,那么:

 List<String> s = studs.stream()
            .map(dto->dto.getId())
            .map(id -> String.valueOf(id.charAt(id.length() - 1))) // take last character and cast to String
            .collect(Collectors.toList()); 
第二种解决方案是使用groupBy方法,该方法将TestDTO聚合到
Map>
。这里的关键是您的数字和值:具有此数字的Dto列表

  • 你已经完成了它的大部分,只需要一点集合操作

  • 看看我在主方法中所做的编辑,它会打印出你想要的结果

      public static void main(String[] args) {
         List<TestDTO> studs = new ArrayList<>();
         studs.add(new TestDTO("101", "Test 101"));
         studs.add(new TestDTO("102", "Test 102"));
    
         Map<String, TestDTO> mapDbCardDtl = studs.stream().collect(Collectors.toMap(TestDTO::getId, Function.identity()));
    
         Set<String> s = mapDbCardDtl.keySet().stream()
                 .map(number -> String.valueOf(number.charAt(number.length() - 1))) // take last character and cast to String
                 .collect(Collectors.toSet());;
         System.out.println("s: " + s);
     }
    
    publicstaticvoidmain(字符串[]args){
    列表螺柱=新的ArrayList();
    增加(新的测试编号(“101”,“测试101”));
    螺柱。添加(新的测试编号(“102”,“测试102”));
    Map mapDbCardDtl=stows.stream().collect(Collectors.toMap(TestDTO::getId,Function.identity());
    设置s=mapDbCardDtl.keySet().stream()
    .map(number->String.valueOf(number.charAt(number.length()-1))//取最后一个字符并转换为字符串
    .collect(收集器.toSet());;
    System.out.println(“s:+s”);
    }
    

  • 您可以将学生的对象转换为
    id
    的最后一位,然后收集到列表中

    List<String> lastDigits = 
             studs.stream()
                  .map(s -> s.getId().substring(s.getId().length() - 1)))
                  .collect(Collectors.toList());
    
    列出最后的数字=
    3.stream()
    .map(s->s.getId().substring(s.getId().length()-1)))
    .collect(Collectors.toList());
    
    注意:如果您在集合中收集,则它将只包含唯一的数字。

    您不能使用并期望具有修改值的不同实体。一种方法是将
    Map
    转换为
    Map
    ,并使用以下代码:

    Map<String, String> mapDbCardDtl = studs
                                          .stream()
                                          .collect(Collectors.toMap(TestDTO::getId, 
                                          (testDto) -> String.valueOf(testDto.getId().charAt(testDto.getId().length() - 1))));
    
    Set<String> s = mapDbCardDtl.keySet();
    System.out.println("s: " + s.toString());
    
    Map-mapDbCardDtl=螺柱
    .stream()
    .collect(Collectors.toMap(TestDTO::getId、,
    (testDto)->String.valueOf(testDto.getId().charAt(testDto.getId().length()-1));
    Set s=mapDbCardDtl.keySet();
    System.out.println(“s:+s.toString());
    
    如果只想打印键的最后一个字符,请在
    println
    语句前面添加此行:

    s=s.stream().map(x->x.substring(x.length()-1)).collect(Collectors.toSet());
    
    如果确实希望映射中的键仅为最后一个字符,请将流逻辑更改为:

    Map mapDbCardDtl=studs.stream()
    .collect(Collectors.toMap(t->t.getId().substring(t.getId().length()-1),Function.identity());
    
    map中是否要使用相同的键?ID声明为
    String
    type,字符串可以是非数字的还是字母数字的?最好使用
    String.valueOf()
    而不是空字符串串联“要使用lamda表达式打印字符串中的最后一个数字”
      public static void main(String[] args) {
         List<TestDTO> studs = new ArrayList<>();
         studs.add(new TestDTO("101", "Test 101"));
         studs.add(new TestDTO("102", "Test 102"));
    
         Map<String, TestDTO> mapDbCardDtl = studs.stream().collect(Collectors.toMap(TestDTO::getId, Function.identity()));
    
         Set<String> s = mapDbCardDtl.keySet().stream()
                 .map(number -> String.valueOf(number.charAt(number.length() - 1))) // take last character and cast to String
                 .collect(Collectors.toSet());;
         System.out.println("s: " + s);
     }
    
    List<String> lastDigits = 
             studs.stream()
                  .map(s -> s.getId().substring(s.getId().length() - 1)))
                  .collect(Collectors.toList());
    
    Map<String, String> mapDbCardDtl = studs
                                          .stream()
                                          .collect(Collectors.toMap(TestDTO::getId, 
                                          (testDto) -> String.valueOf(testDto.getId().charAt(testDto.getId().length() - 1))));
    
    Set<String> s = mapDbCardDtl.keySet();
    System.out.println("s: " + s.toString());