Skip to content

Commit 8370d14

Browse files
New Examples
1 parent c53d3bf commit 8370d14

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
<artifactId>commons-compress</artifactId>
100100
<version>1.21</version>
101101
</dependency>
102+
<dependency>
103+
<groupId>org.apache.commons</groupId>
104+
<artifactId>commons-math3</artifactId>
105+
<version>3.5</version>
106+
</dependency>
102107
</dependencies>
103108
<build>
104109
<sourceDirectory>src/main/java</sourceDirectory>

src/main/java/com/howtodoinjava/core/basic/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
6. [Java 14 - Pattern Matching for instanceof](https://howtodoinjava.com/java14/pattern-matching-instanceof/)
99
7. [Java While Loop](https://howtodoinjava.com/java/basics/while-loop-in-java/)
1010
8. [Java String substring()](https://howtodoinjava.com/java/string/java-string-substring-example/)
11-
9. [Printf-Style Output Formatting in Java](https://howtodoinjava.com/java/basics/printf-style-output-formatting/)
11+
9. [Printf-Style Output Formatting in Java](https://howtodoinjava.com/java/basics/printf-style-output-formatting/)
12+
10. [Java – Round Off Double or Float Values to 2 Decimal Places](https://howtodoinjava.com/java/basics/round-off-n-decimal-places/)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.howtodoinjava.core.basic;
2+
3+
import java.math.BigDecimal;
4+
import java.math.MathContext;
5+
import java.text.DecimalFormat;
6+
import org.apache.commons.math3.dfp.DfpField.RoundingMode;
7+
import org.apache.commons.math3.util.Precision;
8+
9+
public class RoundOffFloats {
10+
11+
public static void main(String[] args) {
12+
float number = 123.456f;
13+
14+
//1. Apache commons Math's Precision
15+
float roundedOffNumber = Precision.round(number, 2, RoundingMode.ROUND_HALF_EVEN.ordinal());
16+
System.out.println(roundedOffNumber); //123.45
17+
18+
//2 - Math.round()
19+
System.out.println(roundUp(number, 2));
20+
21+
//3 - Print formatted value with DecimalFormat
22+
DecimalFormat df = new DecimalFormat("###.##");
23+
System.out.println(df.format(number));
24+
25+
//4 - BigDecimal
26+
BigDecimal bd = new BigDecimal(number);
27+
BigDecimal roundedOffBd = bd.setScale(2, java.math.RoundingMode.HALF_EVEN);
28+
System.out.println(roundedOffBd);
29+
}
30+
31+
public static double roundUp(double value, int places) {
32+
double scale = Math.pow(10, places);
33+
return Math.round(value * scale) / scale;
34+
}
35+
36+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.howtodoinjava.core.collections.map;
2+
3+
import com.google.common.collect.BiMap;
4+
import com.google.common.collect.HashBiMap;
5+
import com.google.common.collect.ImmutableBiMap;
6+
import com.google.common.collect.ImmutableMultimap;
7+
import com.google.common.collect.Multimap;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Map.Entry;
13+
import java.util.stream.Collectors;
14+
import org.apache.commons.collections4.BidiMap;
15+
import org.apache.commons.collections4.MapUtils;
16+
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
17+
18+
public class InvertedMap {
19+
20+
public static void main(String[] args) {
21+
Map<String, String> map = Map.of("key1", "value1", "key2", "value2");
22+
System.out.println(map);
23+
24+
Map<String, Integer> originalMap = new HashMap<>();
25+
originalMap.put("Key1", 1);
26+
originalMap.put("Key2", 2);
27+
28+
Map<Integer, String> invertedMap = new HashMap<>();
29+
for (Entry<String, Integer> entry : originalMap.entrySet()) {
30+
invertedMap.put(entry.getValue(), entry.getKey());
31+
}
32+
System.out.println(invertedMap); //{1=Key1, 2=Key2}
33+
34+
Map<Integer, String> inverseMap2 = originalMap.entrySet()
35+
.stream()
36+
.collect(Collectors.toMap(Entry::getValue, Entry::getKey));
37+
38+
Map<Integer, String> mapWithDuplicateValues = new HashMap<Integer, String>();
39+
mapWithDuplicateValues.put(1, "Value1");
40+
mapWithDuplicateValues.put(2, "Value2");
41+
mapWithDuplicateValues.put(3, "Value2");
42+
43+
HashMap<String, List<Integer>> inverseMap = new HashMap<String, List<Integer>>();
44+
45+
for (Entry<Integer, String> entry : mapWithDuplicateValues.entrySet()) {
46+
if (inverseMap.containsKey(entry.getValue())) {
47+
inverseMap.get(entry.getValue()).add(entry.getKey());
48+
} else {
49+
List<Integer> list = new ArrayList<Integer>();
50+
list.add(entry.getKey());
51+
inverseMap.put(entry.getValue(), list);
52+
}
53+
}
54+
System.out.println(inverseMap);
55+
56+
/*inverseMap = originalMap.entrySet()
57+
.stream()
58+
.collect(Collectors.groupingBy(Entry::getValue,
59+
Collectors.mapping(Entry::getKey, Collectors.toList())));
60+
61+
System.out.println(inverseMap);*/
62+
63+
BiMap<Integer, String> biMap = ImmutableBiMap.of(1, "Key1", 2, "Key2", 3, "Key3");
64+
BiMap<String, Integer> inverseBiMap = biMap.inverse();
65+
66+
System.out.println(inverseBiMap); //{Key1=1, Key2=2, Key3=3}
67+
68+
//
69+
biMap = HashBiMap.create();
70+
BiMap<String, Integer> inversedMap = biMap.inverse();
71+
System.out.println(inversedMap); //{Key1=1, Key2=2, Key3=3}
72+
73+
Multimap<Integer, String> multimap = ImmutableMultimap.of(1, "Key1", 1, "Key2", 2, "Key3");
74+
System.out.println(multimap); //{1=[Key1, Key2], 2=[Key3]}
75+
76+
BidiMap bidiMap = new DualHashBidiMap();
77+
bidiMap.put(1, "Value1");
78+
bidiMap.put(2, "Value2");
79+
80+
System.out.println(bidiMap.inverseBidiMap()); //{Value1=1, Value2=2}
81+
82+
Map<Integer, String> hashMap = new HashMap<Integer, String>();
83+
hashMap.put(1,"Value1");
84+
hashMap.put(2,"Value2");
85+
hashMap.put(3,"Value2");
86+
87+
Map<String,Integer> inversedMap1 = MapUtils.invertMap(hashMap);
88+
System.out.println(inversedMap1); //{Value1=1, Value2=3}
89+
90+
}
91+
92+
}

0 commit comments

Comments
 (0)