CharBuffer chars() methods in Java with Examples

The chars() method of java.nio.CharBuffer Class is used to return a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted. The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.

Syntax:

public IntStream chars()

Return Value: This method returns an IntStream of char values from this sequence.



Below are the examples to illustrate the chars() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// chars() method
  
import java.nio.*;
import java.util.*;
import java.util.stream.IntStream;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer charbuffer
            = CharBuffer.allocate(3);
  
        // append the value in CharBuffer
        // using append() method
        charbuffer.append('a')
            .append('b')
            .append('c')
            .rewind();
  
        // print the CharBuffer
        System.out.println("Original CharBuffer:  "
                           + Arrays.toString(
                                 charbuffer.array())
                           + "\n");
  
        // Read char at particular Index
        // using charAt() method
        IntStream stream = charbuffer.chars();
  
        // Display the stream of int zero-extending
        // the char values from this sequence
        stream.forEach(System.out::println);
    }
}

chevron_right


Output:

Original CharBuffer:  [a, b, c]

97
98
99

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// chars() method
  
import java.nio.*;
import java.util.*;
import java.util.stream.IntStream;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating object of CharBuffer
        // and allocating size capacity
        CharBuffer charbuffer
            = CharBuffer.allocate(5);
  
        // append the value in CharBuffer
        // using append() method
        charbuffer.append((char)140)
            .append((char)117)
            .append((char)118)
            .append((char)0)
            .append((char)90)
            .rewind();
  
        // print the CharBuffer
        System.out.println("Original CharBuffer:  "
                           + Arrays.toString(
                                 charbuffer.array())
                           + "\n");
  
        // Read char at particular Index
        // using charAt() method
        IntStream stream = charbuffer.chars();
  
        // Display the stream of int zero-extending
        // the char values from this sequence
        stream.forEach(System.out::println);
    }
}

chevron_right


Output:

Original CharBuffer:  [?, u, v, , Z]

140
117
118
0
90

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#chars–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.