JavaRush /Java Blog /Random EN /Coffee break #115. A simple way to group objects in Java....

Coffee break #115. A simple way to group objects in Java. charAt() method - how to use it in Java

Published in the Random EN group

An easy way to group objects in Java

Source: Dev.to In this example, we will learn how to simplify grouping for Map objects that contain collections as values. Coffee break #115.  A simple way to group objects in Java.  charAt() method - how to use it in Java - 1For example, we have a Map object where the key is an Integer and the values ​​are an array of strings:
Grouper<Integer, String> grouper = new ArrayListGrouper<>();
        grouper.put(1, "a");
        grouper.put(1, "b");
        grouper.put(1, "c");
        grouper.put(1, "c");
        grouper.put(2, "c");
Result:
{1=[a, b, c, c], 2=[c]}
All we need to do is define the grouping strategy. For example, the already defined ArrayListGrouper class uses ArrayList as its strategy . We can always define a new Grouper that will use a different GroupingStrateg . Now let's change the ArrayList to a HashSet to make the elements unique:
public class HashSetGrouper<K, V> extends Grouper<K, V> {

    public HashSetGrouper() {
        super(HashSet::new);
    }
}
Then we test:
@Test
    public void testHashSetGrouper() {
        Grouper<Integer, String> grouper = new HashSetGrouper<>();
        grouper.put(1, "a");
        grouper.put(1, "b");
        grouper.put(1, "c");
        grouper.put(1, "c");
        grouper.put(2, "c");
        System.out.println(grouper);

    }
Result:
{1=[a, b, c], 2=[c]}
Key 1 now has a set in which the value c is not repeated. The example code is posted on Github .

charAt() method - how to use it in Java

Source: FreeCodeCamp The charAt() method in Java returns the value of the char character in a string at a given or specified index. Today we will see how to use the charAt() method , starting with its syntax, and then look at some examples and use cases.

How to use Java charAt() method

Here's what the syntax for the charAt() method looks like :
public char charAt(int index)
Note that the characters returned from a string using the charAt() method are of the char data type . We'll see how this affects the concatenation of return values ​​in a moment. Now let's look at examples:
public class Main {
  public static void main(String[] args) {

    String greetings = "Hello World";

    System.out.println(greetings.charAt(0));
    // H
  }
}
In the code above, our string, stored in a variable called greetings , says “Hello World”. We used the charAt() method to get the character at index 0. That character is the letter H. The first character will always be at index 0, the second at index 1, and so on. The space between substrings is also considered an index. In the following example we will see what happens when we try to connect different returned characters. Concatenation means joining two or more values ​​together (in most cases, the term is used to connect characters or substrings in a string).
public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    char ch1 = greetings.charAt(0); // H
    char ch2 = greetings.charAt(4); // o
    char ch3 = greetings.charAt(9); // l
    char ch4 = greetings.charAt(10); // d

    System.out.println(ch1 + ch2 + ch3 + ch4);
    // 391
  }
}
Using the charAt() method , we got the characters with indexes 0, 4, 9 and 10, which are the letters H, o, l and d respectively. We then tried to print and connect these characters:
System.out.println(ch1 + ch2 + ch3 + ch4);
But instead of returning “Hold”, we got 391. This is because the returned values ​​are no longer strings, but are of type char . So when we concatenate them, the interpreter adds their ASCII value instead. H has an ASCII value of 72, o has a value of 111, l has a value of 108, and d has a value of 100. If we add them together, we get 391, which was returned in the last example.

Error StringIndexOutOfBoundsException

When we pass a sequence number that is greater than the number of characters in our string, we get a StringIndexOutOfBoundsException error in the console. This error also applies to the use of negative indexing, which is not supported in Java. In programming languages ​​like Python that support negative indexing, passing -1 will give you the last character or value in the data set, similar to how 0 always returns the first character. Here's an example:
public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    char ch1 = greetings.charAt(20);

    System.out.println(ch1);

    /* Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 20
    */
  }
}
In the above code we have passed index 20: char ch1 = greetings.charAt(20); , which exceeds the number of characters in our greetings variable , so we got an error. You can see the error message commented out in the code block above. Likewise, if we pass a negative value like this: char ch1 = greetings.charAt(-1); , then we get a similar error.

Conclusion

In this article, we learned how to use the charAt() method in Java. We saw how to return characters in a string based on their index number and what happens when we concatenate those characters. We also talked about some cases where you can get an error response when using the charAt() method in Java.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION