Fill objects fields from Stream<Integer> - java

I have class
#Data
Test{
private int[] a;
private int[] b;
}
And I have private Stream<Integer> stream with 1 000 000 integers.
How can I make the following:
iterate over all integers in stream
create new Test
if i!=0 add i to test.a else add i to test.b
At last I need a Test object with 2 arrays where a contains all non-zero elements and b contains all zeroes.
I need 1 Test object.

Maybe you could use 2 stream operations and on 1 instance of the test use setters for a and b.
public class Foo {
public static void main(String[] args) throws IOException {
List<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(0);
myList.add(1);
Test t = new Test();
t.setA(myList.stream().filter(x -> x != 0).mapToInt(x -> x).toArray());
t.setB(myList.stream().filter(x -> x == 0).mapToInt(x -> x).toArray());
System.out.println("t: " + t);
}
}
class Test {
private int[] a;
private int[] b;
public void setA(int[] array) {
a = array;
}
public void setB(int[] array) {
b = array;
}
#Override
public String toString() {
return "Test {a=" + Arrays.toString(a) + ", b=" + Arrays.toString(b) + "}";
}
}

If you know beforehand that you have 1,000,000 integers, there is no need to collect zeros at all:
int[] a = stream.mapToInt(Integer::intValue).filter(i -> i!=0).toArray();
int[] b = new int[1_000_000 - a.length];
Test test = new Test(a, b);

Here is a sample implementation that splits the Stream into two Streams
public class Main {
static class Test {
private int[] a;
private int[] b;
Test(int[] a, int[] b) {
this.a = a;
this.b = b;
}
}
public static void main(String[] args) {
IntStream.Builder zeroStreamBuilder = IntStream.builder();
IntStream.Builder nonZeroStreamBuilder = IntStream.builder();
IntStream intStream = IntStream.of(0, 0, 1, 2, 3, 4);
intStream.forEach(value -> {
if (value == 0)
zeroStreamBuilder.add(value);
else
nonZeroStreamBuilder.add(value);
}
);
int[] a = zeroStreamBuilder.build().toArray();
int[] b = nonZeroStreamBuilder.build().toArray();
Test result = new Test(a, b);
}
}
If you are willing to use a List, then it will be possible skip building arrays from streams .build().toArray() as you can directly add the values to the resulting lists.

Related

How can I create a stream out of a finite number of objects passed?

I have a finite list of objects and when I call getElements I want to have a stream of all objects in that list.
private Set<T> object;
public List(Set<T> objects) {
this.object = objects;
}
public Stream<T> getElements() {
Stream<T> stream = object.stream();
return stream;
}
So if I create a set and add 1 and 2 to it:
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
List<Integer> nums = new List<Integer>(set);
System.out.println(nums.getElements());
I am supposed to get 1,2 as output, but I get this instead:
java.util.stream.ReferencePipeline$Head#7cc355be
How could I solve this?
You can implement the method in one of the following ways:
public class Test {
public static void main(String[] args) throws Exception {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
MyList<Integer> nums = new MyList<Integer>(set);
nums.printElements();
System.out.println("\n" + nums.getAsList());
System.out.println("Number of elements: " + nums.getElementStream().count());
}
}
class MyList<T> {
private Set<T> object;
public MyList(Set<T> objects) {
this.object = objects;
}
//If you want to print all elements
public void printElements() {
object.stream().forEach(e -> System.out.print(e + " "));
}
//if you want them as a java.utli.List
public List<T> getAsList(){
return object.stream().collect(Collectors.toList());
}
//if you want stream of the elements
public Stream<T> getElementStream() {
Stream<T> stream = object.stream();
return stream;
}
}
Output:
1 2
[1, 2]
Number of elements: 2

Java 8 Grouping by Multiple Fields into Single Map

I have a class like this:
public class Foo {
private String a;
private String b;
private LocalDate c;
private int d;
}
I have a list of Foo objects that I want to group by a, b, and c and produce a map. Here's what I have so far:
Map<String, List<Foo>> test = foos.stream().collect(Collectors.groupingBy(Foo::getA, Collectors.collectingAndThen(Collectors.groupingBy(Foo::getB), Collections.unmodifiableList())));
But that itself is wrong. I don't know how to groupby multiple fields but still produce a Map<String, List<Foo>>. Any ideas what I'm doing wrong?
Edit 1: If I have the following Foo's:
{"Test", "Test", "10/02/2015", 5}
{"Test", "Test", "10/02/2015", 4}
{"Test", "Test", "10/02/2015", 3}
{"Test", "Test", "2/02/2015", 5}
{"Test", "Potato", "2/02/2015", 5}
Then it should group to:
{"Test", "Test", "10/02/2015", [5, 4, 3]}
{"Test", "Test", "2/02/2015", 5}
{"Test", "Potato", "2/02/2015", 5}
My original post was misleading in what exactly I wanted but basically it needs to group by a, b, d and produce a list of d. I know I'll probably have to create a new class to store them in like so:
public class FooResult {
private String a;
private String b;
private LocalDate c;
private List<Integer> d;
}
How can I group and map to a new class like shown above?
As a group by multiple fields is not implemented you have to use a composite key consisting of values from a, b and c. With that key the collect operation can be used like this with the Collector#of() factory method.
Map<String, List<Integer>> result = foos.stream().collect(Collector.of(
HashMap::new,
( map, foo ) -> {
map.compute(foo.a + "_" + foo.b + "_" + foo.c, (key,list) -> {
if(list == null){
list = new ArrayList<>();
}
list.add(foo.d);
return list;
});
},
( map1, map2 ) -> {
map2.forEach(( k, v ) -> {
map1.compute(k, (key, list) -> {
if(list == null){
list = v;
} else {
list.addAll(v);
}
return list;
});
});
return map1;
}
));
You can also use intermediate Map with a key that aggregates fields a, b and c from Foo class and List<Integer> value that collects all d field values.. In below example I have created MapKey class - a helper class that aggregates those fields and implements hashCode and equals methods so it can be used as a key in a HashMap.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FooMain {
public static void main(String[] args) {
final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
final List<Foo> foos = Arrays.asList(
new Foo("Test", "Test", LocalDate.parse("10/02/2015", dateFormat), 5),
new Foo("Test", "Test", LocalDate.parse("10/02/2015", dateFormat), 4),
new Foo("Test", "Test", LocalDate.parse("10/02/2015", dateFormat), 3),
new Foo("Test", "Test", LocalDate.parse("02/02/2015", dateFormat), 5),
new Foo("Test", "Potato", LocalDate.parse("02/02/2015", dateFormat), 5)
);
List<FooResult> result = foos.stream()
.collect(Collectors.groupingBy(foo -> new MapKey(foo.a, foo.b, foo.c), Collectors.mapping(Foo::getD, Collectors.toList())))
.entrySet()
.stream()
.map(entry -> new FooResult(entry.getKey().a, entry.getKey().b, entry.getKey().c, entry.getValue()))
.collect(Collectors.toList());
result.forEach(System.out::println);
}
public static final class Foo {
private final String a;
private final String b;
private final LocalDate c;
private final int d;
Foo(String a, String b, LocalDate c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
int getD() {
return d;
}
}
public static final class FooResult {
private final String a;
private final String b;
private final LocalDate c;
private final List<Integer> d;
FooResult(String a, String b, LocalDate c, List<Integer> d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
#Override
public String toString() {
return "FooResult{" +
"a='" + a + '\'' +
", b='" + b + '\'' +
", c=" + c +
", d=" + d +
'}';
}
}
public static final class MapKey {
private final String a;
private final String b;
private final LocalDate c;
MapKey(String a, String b, LocalDate c) {
this.a = a;
this.b = b;
this.c = c;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MapKey)) return false;
MapKey mapKey = (MapKey) o;
if (a != null ? !a.equals(mapKey.a) : mapKey.a != null) return false;
if (b != null ? !b.equals(mapKey.b) : mapKey.b != null) return false;
return c != null ? c.equals(mapKey.c) : mapKey.c == null;
}
#Override
public int hashCode() {
int result = a != null ? a.hashCode() : 0;
result = 31 * result + (b != null ? b.hashCode() : 0);
result = 31 * result + (c != null ? c.hashCode() : 0);
return result;
}
}
}
Then as you can see you can do your transformation is 6 lines of code. The output of this program is following:
FooResult{a='Test', b='Potato', c=2015-02-02, d=[5]}
FooResult{a='Test', b='Test', c=2015-02-02, d=[5]}
FooResult{a='Test', b='Test', c=2015-10-02, d=[5, 4, 3]}
I've also made Foo, FooResult and MapKey immutable - this is always a good choice when you have to deal with stream transformations. You don't want to have any side effects during stream manipulation and immutable objects guarantee that.

How to transform this class to immutable?

I am in process of learning immutability but I am not able to exactly digest how this works. So in order for me to understand immutability, I created a test program.
The funtion getArray(Box b) will make an ArrayList of Box objects.
Expected output: Actual output:
Output Output
a is 5 a is 5
b is 10 b is 10
Output Output
a is 0 a is 4
b is 0 b is 40
Output Output
a is 1 a is 4
b is 10 b is 40
Output Output
a is 2 a is 4
b is 20 b is 40
Output Output
a is 3 a is 4
b is 30 b is 40
Output Output
a is 4 a is 4
b is 40 b is 40
Logic:
public class Box {
static int a;
static int b;
public Box() {
a = 5;
b = 10;
}
public int getA() {
return a;
}
public void setA(int x) {
a = x;
}
public int getB() {
return b;
}
public void setB(int x) {
b = x;
}
public void display() {
System.out.println("Output");
System.out.println("a is " + a);
System.out.println("b is " + b);
System.out.println();
}
}
Main Class
import java.util.ArrayList;
public class Check {
public static void main(String[] args) {
Box b = new Box();
b.display();
ArrayList<Box> arr2 = new ArrayList<Box>();
arr2 = getArray(b);
for (int i = 0; i < arr2.size(); i++) {
arr2.get(i).display();
}
}
public static ArrayList<Box> getArray(Box b) {
ArrayList<Box> arr = new ArrayList<Box>();
for (int i = 0; i < 5; i++) {
b.setA(i);
b.setB(i * 10);
arr.add(b);
}
return arr;
}
}
How do I change the logic in such a way that I get the desired output? How do we decide how and where to edit the code to ensure immutability?
This would be an immutable:
public final class Box {
final int a;
final int b;
public Box(int a, int b) {
this.a = a;
this.b = b;
}
}
And then your array method would be:
public static ArrayList<Box> getArray(Box b) {
ArrayList<Box> arr = new ArrayList<Box> ();
for(int i =0 ;i <5; i++) {
arr.add(new Box(i, i*10));
}
return arr;
}
The data members are declared final because they're immutable, and so getters are pointless and setters just make no sense.
The class is declared final so you cannot subclass it.
In short, an immutable object is an object whose state cannot be modified after it's created. Your immutable Box object would look like this:
public final class Box {
private final int a;
private final int b;
public Box(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
public void display() {
System.out.println("Output");
System.out.println("a is " + a);
System.out.println("b is " + b);
System.out.println();
}
}
Notice that the variables a and b are assigned exactly once, during the construction of the Box instance. There are no setters, because Box's immutability means that its state (including variables a and b) will not change over its lifetime.
The final keyword in front of a and b means that you must assign them exactly once. It's actually considered good practice to make all your variables final unless you specifically need them not to be; but for an immutable object it's essential.
You were using the static keyword. Static has nothing to do with immutability. It means the variable is shared among all instances of the Box class. In my example, each Box instance has its own copies of a and b, because I didn't make them static.
To wrap this up, I'll give an example of your main class which has the desired output:
public class Check {
public static void main(String[] args) {
final List<Box> arr2 = getArray();
for (int i = 0; i < arr2.size(); i++) {
arr2.get(i).display();
}
}
public static ArrayList<Box> getArray() {
final ArrayList<Box> arr = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final Box box = new Box(i, i * 10);
arr.add(box);
}
return arr;
}
}
Note that a new instance of box is created at every iteration of the loop.

How to make generic Counting Sort Method?

Okay I am a pretty beginner java coder, and I am doing an assignment where I am stuck. I need to create a generic method (sort) that sorts a Type array according to frequency, basically, I am taking the CountingSort Algorithm and making it a generic method. This is where I am lost. I can't seem to figure out how to do this.
Here is a link to my instructions,
https://classes.cs.siue.edu/pluginfile.php/7068/mod_assign/intro/150mp08.pdf
Code:
Driver Class
package mp08;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Lists array = new Lists();
array.populateLists();
System.out.println("Original Int List: \n");
array.sort(Lists.intList);
System.out.println("Sorted Int List: \n");
}
}
Lists Class
package mp08;
import java.util.Arrays;
import java.util.Random;
public class Lists {
public static Integer[] intList;
public static Integer[] sortedintList;
public static Integer[] frequency;
public static Character[] charList;
public static Character[] sortedcharList;
public static int MAX_SIZE = 101;
public static int lengthInt;
public static int lengthChar;
public Lists(){
this.intList = new Integer[MAX_SIZE];
this.sortedintList = new Integer[MAX_SIZE];
this.charList = new Character[MAX_SIZE];
this.sortedcharList = new Character[MAX_SIZE];
this.frequency = new Integer[MAX_SIZE];
this.lengthInt = 0;
this.lengthChar = 0;
}
//Makes random integer for populated lists method.
public int randomInt(int min, int max){
Random rand = new Random();
int randomNum = rand.nextInt((max-min)+1)+min;
return randomNum;
}
//Makes random character for populated lists method.
public char randomChar(){
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = alphabet.length();
Random rand = new Random();
char randomLet = alphabet.charAt(rand.nextInt(N));
return randomLet;
}
//Populates intList and charList with random values.
public void populateLists(){
for (int i = 0; i < MAX_SIZE; i++) {
intList[i] = randomInt(1,100);
lengthInt++;
}
for (int i = 0; i < MAX_SIZE; i++) {
charList[i] = randomChar();
lengthChar++;
}
}
//Returns sorted array
public Integer[] sorted(){
return intList;
}
public static <T> void sort(T[] array) {
// array to be sorted in, this array is necessary
// when we sort object datatypes, if we don't,
// we can sort directly into the input array
Integer[] aux = new Integer[array.length];
// find the smallest and the largest value
int min = 1;
int max = 101;
// init array of frequencies
int[] counts = new int[max - min + 1];
// init the frequencies
for (int i = 0; i < array.length; i++) {
counts[array[i] - min]++;
}
// recalculate the array - create the array of occurence
counts[0]--;
for (int i = 1; i < counts.length; i++) {
counts[i] = counts[i] + counts[i-1];
}
/*
Sort the array right to the left
1) Look up in the array of occurences the last occurence of the given value
2) Place it into the sorted array
3) Decrement the index of the last occurence of the given value
4) Continue with the previous value of the input array (goto set1),
terminate if all values were already sorted
*/
for (int i = array.length - 1; i >= 0; i--) {
aux[counts[array[i] - min]--] = array[i];
}
}
public static void main(String[] args) {
Integer [] unsorted = {5,3,0,2,4,1,0,5,2,3,1,4};
System.out.println("Before: " + Arrays.toString(unsorted));
Integer [] sorted = sort(unsorted);
System.out.println("After: " + Arrays.toString(sorted));
}
}
I obviously have not finished my driver class yet and I would appreciate any help I can get!
There's no generic way for any Comparable type to get its ordinal number. Sometimes such numbers do not exist at all (for example, String is Comparable, but you cannot map any String to the integer number). I can propose two solutions.
First one is to store counts not in the array, but in TreeMap instead creating new entries on demand (using Java-8 syntax for brevity):
public static <T extends Comparable<T>> void sort(T[] array) {
Map<T, Integer> counts = new TreeMap<>();
for(T t : array) {
counts.merge(t, 1, Integer::sum);
}
int i=0;
for(Map.Entry<T, Integer> entry : counts.entrySet()) {
for(int j=0; j<entry.getValue(); j++)
array[i++] = entry.getKey();
}
}
public static void main(String[] args) {
Integer[] data = { 5, 3, 0, 2, 4, 1, 0, 5, 2, 3, 1, 4 };
System.out.println("Before: " + Arrays.toString(data));
sort(data);
System.out.println("After: " + Arrays.toString(data));
Character[] chars = { 'A', 'Z', 'B', 'D', 'F' };
System.out.println("Before: " + Arrays.toString(chars));
sort(chars);
System.out.println("After: " + Arrays.toString(chars));
}
Such solution looks clean, but probably not very optimal (though its advantage is that it does not care whether all numbers are from 1 to 100 or not).
Another possible solution is to create some additional interface which defines ordering for given type:
public interface Ordering<T> {
int toOrdinal(T obj);
T toObject(int ordinal);
}
public class IntegerOrdering implements Ordering<Integer> {
#Override
public int toOrdinal(Integer obj) {
return obj;
}
#Override
public Integer toObject(int ordinal) {
return ordinal;
}
}
public class CharacterOrdering implements Ordering<Character> {
#Override
public int toOrdinal(Character obj) {
return obj;
}
#Override
public Character toObject(int ordinal) {
return (char)ordinal;
}
}
Now you may make your sort method accepting the ordering parameter:
public static <T> void sort(T[] array, Ordering<T> ordering) { ... }
Every time you need to get counts array index by T object, just call ordering.toOrdinal(object). Every time you need to get object by array index, just use ordering.toObject(index). So, for example, instead of
counts[array[i] - min]++;
Use
counts[ordering.toOrdinal(array[i]) - min]++;
And call the sorting method like this:
sort(characterArray, new CharacterOrdering());
sort(integerArray, new IntegerOrdering());

Java: strange anomaly when printing int[]

When I try to concatenate elements from a int[] that is a member of a class into a String, it works fine. But when I do the same procedure with a temp int[] (generated from Arrays.copyOf), I get jibberish. I don't understand why this is happening. A boiled down version of my code goes like this:
import java.util.*;
import java.io.*;
public class Test {
public int[] arr;
public Test(int[] arr) {
this.arr = arr;
}
public void fn() {
// ...
int[] temp = Arrays.copyOf(arr, arr.length);
System.out.println(this);
System.out.println(temp);
}
/*
* Takes an integer array as input and returns a string representation
* of the elements in the array.
*/
public String arrayToString(int[] arr) {
String s = "[";
int i = 0;
while(i < (arr.length - 1)) {
s = s + arr[i++] + ", ";
}
s = s + arr[i] + "]";
return s;
}
public String toString() {
return arrayToString(this.arr);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
Test test = new Test(arr);
test.fn();
/*
* Produces result:
* [1, 2, 3, 4]
* [I#3343c8b3
*/
}
}
Why does this happen? As far as I can tell, both arr and temp should be of type int[].
Your local copy is going via arrayToString() because you println(this) which invokes toString().
Your temp copy is just trying to dump the int[] - try using arrayToString(temp) instead of println(temp).
This is unnecessary:
public String toString() {
return arrayToString(this.arr);
}
but you could replace it with:
public String toString() {
return Arrays.toString(this.arr);
}

Categories