Categories
collections java

convert iterator to list in java

In this tutorial, we will learn convert iterator to list in java

Here we are giving two approach to convert iterator to list in java

Syntax in Java 8
ArrayList<String> list = new ArrayList<String>();
iterator.forEachRemaining(list::add);
Syntax in Java 7
ArrayList<String> list = new ArrayList<String>();
while (iterator.hasNext()) {
  String string = (String) iterator.next();
  list.add(string);
}
Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorToList {

	public static void main(String[] args) {

		Iterator<String> iterator = Arrays.asList("Rajesh", "Kumar", "Beginners", "Bug").iterator();
		convertToListJava7(iterator);
		convertToListJava8(iterator);

	}

	/**
	 * Java 7 Approach
	 * 
	 * @param iterator
	 * @return
	 */
	public static ArrayList<String> convertToListJava7(Iterator<String> iterator) {
		ArrayList<String> list = new ArrayList<String>();
		while (iterator.hasNext()) {
			String string = (String) iterator.next();
			list.add(string);
		}

		return list;
	}

	/**
	 * Java 8 Apporach
	 * 
	 * @param iterator
	 * @return
	 */
	public static ArrayList<String> convertToListJava8(Iterator<String> iterator) {
		ArrayList<String> list = new ArrayList<String>();
		iterator.forEachRemaining(list::add);
		return list;
	}

}
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/list/IteratorToList.java

Related Articles

Iterate list using streams in java

Categories
collections java

sorting operations in java using streams

In this example, we learn sorting operations in java using streams

In Java 8 it is easy to sort records from an arraylist

Streams API providing powerful features to sort the records from a list without modifying it

This will returns a stream consisting of the sorted elements

Syntax

Stream sorted()
list.stream().sorted({operations});

Ascending Example

import java.util.ArrayList;
import java.util.List;

public class AscendingSortingStreams {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("Ball");
        list.add("Apple");
        list.add("Cat"); // printitng values before sorting
        System.out.println("Values before sorting ");
        list.stream().forEach(x -> System.out.println(x));
        System.out.println("*** Values After sorting ***");
        list.stream().sorted().forEach(x -> System.out.println(x));
    }
}

Output

Values before sorting 
Ball
Apple
Cat
*** Values After  sorting ***
Apple
Ball
Cat

Descending Example

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class DescendingSortingStreams {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("Ball");
        list.add("Apple");
        list.add("Cat");
        // printitng values before sorting
        System.out.println("Values before sorting ");
        list.stream().forEach(x -> System.out.println(x));
        System.out.println("*** Values After sorting ***");
        // Comparator.reverseOrder() will sort the records in descending order
        list.stream().sorted(Comparator.reverseOrder()).forEach(x -> System.out.println(x));
    }
}

Output

Values before sorting 
Ball
Apple
Cat
*** Values After  sorting ***
Cat
Ball
Apple

References

Oracle Docs

Related Articles

filter operations in streams on java 

Iterate list using streams in java

Categories
collections java

Sort an Arraylist in java with example

In this tutorial, we will learn how to Sort an Arraylist in java with example by ascending order using collections class

If you are new to array list refer below link to understand array list
https://beginnersbug.com/arraylist-in-java/

Features

  • Sort the specified list into ascending order,
  • This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
  • The specified list must be modifiable, but need not be resizable.

Syntax

Collections.sort(list);

Example

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortArrayListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList();
        list.add("Car");
        list.add("Zebra");
        list.add("Apple");
        list.add("Ball");
        System.out.println("Array list values before sorting \n");
        for (String string : list) {
            System.out.println("Value is " + string);
        }
        // Below sort method will sort your list
        Collections.sort(list);
        System.out.println("\nArray list values after sorting \n");
        for (String string : list) {
            System.out.println("Value is " + string);
        }
    }
}

Output

Array list values before sorting 

Value is Car
Value is Zebra
Value is Apple
Value is Ball

Array list values after sorting 

Value is Apple
Value is Ball
Value is Car
Value is Zebra

Reference

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

Related Articles

iterate stream with index in Java8

foreach in java8 using streams