Scanner String Input and Collections in Java
Scanner String Input and Collections in Java
next(): Reads only one word (separated by space). If you use next(), it will stop
reading as soon as it encounters a space.
o Example:
java
Copy code
Scanner scanner = new Scanner(System.in);
String s = scanner.next(); // if input is: "Hello World", s will
be "Hello"
nextLine(): Reads the entire line of input, including spaces, until it encounters a
newline (\n).
o Example:
java
Copy code
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine(); // if input is: "Hello World", s
will be "Hello World"
You use nextLine() when you expect multiple words or a full sentence from the input, while
next() is for single-word inputs.
2. Why trim()?
trim() removes leading and trailing whitespace (spaces, tabs, newlines). It ensures that there are
no extra spaces that could interfere with the logic in your code (like when comparing strings).
Example:
java
Copy code
String input = " Hello World ";
String trimmedInput = input.trim(); // "Hello World" (without extra
spaces)
You use trim() to avoid issues when the user mistakenly adds extra spaces before or after the
input, which could affect comparisons or logic.
It's part of the Java Collection Framework (a set of classes and interfaces for handling
collections of objects).
You need it because methods like Collections.frequency() make working with lists
easier, as they provide ready-made functionality like counting occurrences of an element.
To use it, you need to import:
java
Copy code
import java.util.Collections;
4. What is Collections.frequency()?
Example:
java
Copy code
List<String> list = Arrays.asList("apple", "banana", "apple",
"orange");
int count = Collections.frequency(list, "apple"); // count will be 2
It is useful when you need to count how many times a specific item appears in a collection, like
in your code where you're comparing character frequencies.
The Collections class provides many useful static methods. Here are some important ones:
1. sort(List<T> list):
o Sorts the elements of the list in ascending order.
o Example:
java
Copy code
List<Integer> numbers = Arrays.asList(3, 2, 5, 1);
Collections.sort(numbers); // numbers becomes [1, 2, 3, 5]
2. reverse(List<?> list):
o Reverses the order of elements in the list.
o Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3);
Collections.reverse(numbers); // numbers becomes [3, 2, 1]
3. shuffle(List<?> list):
o Randomly shuffles the elements in the list.
o Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
Collections.shuffle(numbers); // the order is randomized
java
Copy code
List<Integer> numbers = Arrays.asList(3, 2, 5, 1);
int minValue = Collections.min(numbers); // minValue will be 1
int maxValue = Collections.max(numbers); // maxValue will be 5
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int index = Collections.binarySearch(numbers, 3); // index will
be 2 (zero-based index)
java
Copy code
List<String> src = Arrays.asList("a", "b", "c");
List<String> dest = new ArrayList<>(Arrays.asList("x", "y",
"z"));
Collections.copy(dest, src); // dest becomes ["a", "b", "c"]
Each of these methods simplifies working with Java collections like lists, sets, and maps,
providing reusable functionality.