Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Scanner String Input and Collections in Java

Uploaded by

sec22it136
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Scanner String Input and Collections in Java

Uploaded by

sec22it136
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Scanner string input and collections in java

1. Why scanner.nextLine() instead of next()?

 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.

3. What is Collections? Why is it needed?


Collections is a utility class in Java that provides static methods for operating on collections,
such as lists, sets, and maps.

 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()?

Collections.frequency(Collection<?> c, Object o) counts the number of occurrences of


a specified element o in the collection c.

 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.

5. Similar important methods in Collections class

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

4. min(Collection<? extends T> c) / max(Collection<? extends T> c):


o Finds the minimum or maximum element in a collection.
o Example:

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

5. binarySearch(List<? extends Comparable<? super T>> list, T key):


o Searches for a key in a sorted list using binary search.
o Example:

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)

6. copy(List<? super T> dest, List<? extends T> src):


o Copies all elements from the source list to the destination list.
o Example:

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.

You might also like