Compare commits

...

2 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 033faac85c More Streams
- Added unrelated things
2023-10-12 13:12:26 +02:00
Tristan B. Velloza Kildaire da3b919140 Even More OOP
- Added
2023-10-12 13:12:18 +02:00
6 changed files with 271 additions and 3 deletions

View File

@ -1,8 +1,14 @@
package ocp.practice.basics.MoreStreams;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@ -11,7 +17,7 @@ import java.util.stream.Stream;
*/
public class App
{
public static void main( String[] args )
public static void main( String[] args ) throws IOException
{
// Practice dirtying streams
// practiceDirtyLinkage();
@ -23,10 +29,88 @@ public class App
// reductionExamples();
// Stream operations (findFirst, anyMatch, allMatch)
matchingExamples();
// matchingExamples();
// Flat mapping
flatMapping();
// flatMapping();
// More complicated grouping by
groupingByWithMapping();
}
/**
* Here we look at using grouping by
* but in a more complicated manner
*
* This is cascading collectors
* @throws IOException
*/
private static void groupingByWithMapping() throws IOException
{
List<String> names = List.of("Tristan", "Thomas", "Abigail", "Andy");
Map<Character, List<Integer>> map = names.stream()
.collect(Collectors.groupingBy(App::getFirstLetter,
Collectors.mapping(App::getCharValue, Collectors.toList())
));
System.out.println(map);
/**
* The {@link Collectors#toMap} method will raise
* an exception if the duplicate key, from the key
* mapper function, maps to an existing key.
*
* Therefore we provide it with a BinaryOperator
* which is called in such occasions and is passed
* the present key-value and the incoming one,
* combines them are stores that at the key's
* value
*/
names = List.of("Tristan", "Thomas", "Abigail", "Andy");
System.out.println(names.stream().collect(Collectors.toMap(App::getFirstLetter, i->i, (i,x)->i+x)));
var things = new ArrayList<String>();
testMethod(things);
Path p1 = Path.of("/");
Path p2 = Path.of("/././dev2");
System.out.println("IsSameFile?: "+Files.isSameFile(p1, p2));
int i1 = 1;
double i2 = i1;
Double i3 = i2;
Integer i4 = 1;
i3 = (double)i4;
i2=i4;
// i3=2;
// i4 = 2.0;
}
private static void testMethod(List<String>... listOfLists)
{
for(List<String> d: listOfLists)
{
// d.get(0);
}
}
private static int getCharValue(String name)
{
return name.length();
}
private static char getFirstLetter(String name)
{
return name.charAt(0);
}
/**

View File

@ -0,0 +1 @@
/target/

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ocp.oop</groupId>
<artifactId>EvenMoreOOP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EvenMoreOOP</name>
<description>A simple EvenMoreOOP.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>

View File

@ -0,0 +1,38 @@
package ocp.oop.EvenMoreOOP;
/**
* Just testing some things out
*
*/
public class App
{
public class A
{
}
public static class B
{
}
public static void main(String[] args)
{
class C
{
};
App app = new App();
// Non-static class must be accessed via a nn-static way (object instance)
app.new A();
new App().new A();
// B MUST (not "can") as a static instance explicit top-class or not
new App.B();
// app.new B();
new B();
new C();
}
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="EvenMoreOOP" xmlns="http://maven.apache.org/DECORATION/1.8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.8.0 http://maven.apache.org/xsd/decoration-1.8.0.xsd">
<bannerLeft>
<name>EvenMoreOOP</name>
<src>https://maven.apache.org/images/apache-maven-project.png</src>
<href>https://www.apache.org/</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
<href>https://maven.apache.org/</href>
</bannerRight>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.7</version>
</skin>
<body>
<menu ref="parent" />
<menu ref="reports" />
</body>
</project>

View File

@ -0,0 +1,38 @@
package ocp.oop.EvenMoreOOP;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}