Refactoring Java code to make it reusable - java

I recently created a simple method that takes a HashMap and LinkedList as arguments. It iterates through the HashMap, find any two entries that follows these rules:
Sum of keys of this entries must be divisible by 100
Sum of keys must be less than 1000
If the entry with the same value appears more than once the case should be skipped
Pairs that follows these rules are added to the LinkedList. It looks like this:
private static void compare2(HashMap<Integer,String> values,List<String>results){
if (values.size()>1) {
for(HashMap.Entry<Integer,String> entry1:values.entrySet()){
for (HashMap.Entry<Integer,String> entry2:values.entrySet()){
if (entry1.getValue().equals(entry2.getValue()))continue;
if ((entry1.getKey() + entry2.getKey())%100 == 0 && (entry1.getKey() + entry2.getKey())<1000){
results.add(entry1.getKey() + "+" + entry2.getKey() + "=" + entry1.getKey() + entry2.getKey());
results.add(entry1.getValue());
results.add(entry2.getValue());
}
}
}
}
}
Now I wanted to create similar method that finds 3 entries that follows the same rules. Problem is that I would like to reuse existing code instead of copy/pasting this and modyfiyng, and I can't seem to find a way to do that. I don't mind if I need to change my method as long as the result is the same.

You could make the amount of numbers a parameter: N.
You could stay using for loops, but an alternative example approach could be to refactor your method with lambda's and streams as follows:
List<List<Map.Entry<Integer, String>>> compareN(HashMap<Integer, String> map, int n) {
return map.entrySet().stream()
.map(entry -> listOfNAccompanyingEntriesThatSatisfyTheConditions(entry, emptyList(), map, n - 1))
.filter(list -> !list.isEmpty())
.collect(toList());
}
Where the method listOfNAccompanyingEntriesThatSatisfyTheConditions is a recursive method:
private List<Map.Entry<Integer, String>>
listOfNAccompanyingEntriesThatSatisfyTheConditions(Map.Entry<Integer, String> newEntry,
List<Map.Entry<Integer, String>> selectedEntries,
HashMap<Integer, String> originalMap,
int n) {
List<Map.Entry<Integer, String>> newSelectedEntries = join(newEntry, selectedEntries);
if (n == 0) return satisifiesCondition(newSelectedEntries) ? selectedEntries : emptyList();
return originalMap.entrySet().stream()
.filter(entry -> !selectedEntries.contains(entry) && !entry.equals(newEntry))
.map(entry -> listOfNAccompanyingEntriesThatSatisfyTheConditions(entry, newSelectedEntries, originalMap, n - 1))
.flatMap(Collection::stream)
.collect(toList());
}
For every n, the method takes another shot at the original full list to accumulate a sublist that might fulfil the requirements. If the amount of numbers is reached (n==0), the recursion stops and the stop condition is verified:
private static boolean satisifiesCondition(List<Map.Entry<Integer, String>> entries) {
int sum = sumOfTheKeysFrom(entries);
return sum % 100 == 0 && sum < 1000;
}
However, this approach is really an exact translation of your implementation and has still the same couple of issues. For example, if you run it for
HashMap<Integer, String> map = new HashMap<Integer, String>() {{
put(1, "fred");
put(2, "anja");
put(24, "tom");
put(45, "eddy");
put(22, "lenny");
put(77, "tommy");
put(55, "henry");
put(43, "alfred");
}};
You will yield double results and results with the same entry twice, such as:
[1=fred, 22=lenny, 1=fred, 77=tommy] [2=anja, 55=henry, 2=anja,
43=alfred]
These are however easily solved by some small tweaks.
If you are not familiar with streams, lambda's or recursion, I'd suggest you implement the same with the imperative approach. Also be aware I did not care about performance in my example code, as I perceived this question as some kind of exercise.

Try something like this:
#SafeVarargs
private static void check( final List<String> results,
final Map.Entry<Integer, String>... vals )
{
int sum = 0;
for ( final Map.Entry<Integer, String> val : vals )
{
final Integer key = val.getKey();
sum += null == key ? 0 : key;
}
if ( sum < 1000 && 0 == ( sum % 100 ) )
{
final StringBuilder result = new StringBuilder( 200 );
for ( final Map.Entry<Integer, String> val : vals )
{
result.append( " + " ).append( val.getKey() );
}
results.add( result.append( " = " ).append( sum ).substring( 3 ) );
for ( final Map.Entry<Integer, String> val : vals )
{
results.add( val.getValue() );
}
}
}
private static void compare2( final HashMap<Integer, String> values,
final List<String> results )
{
if ( values.size() > 1 )
{
for ( final HashMap.Entry<Integer, String> entry1 : values.entrySet() )
{
for ( final HashMap.Entry<Integer, String> entry2 : values.entrySet() )
{
if ( entry1 == entry2 )
continue;
check( results, entry1, entry2 );
}
}
}
}
private static void compare3( final HashMap<Integer, String> values,
final List<String> results )
{
if ( values.size() > 2 )
{
for ( final HashMap.Entry<Integer, String> entry1 : values.entrySet() )
{
for ( final HashMap.Entry<Integer, String> entry2 : values.entrySet() )
{
for ( final HashMap.Entry<Integer, String> entry3 : values.entrySet() )
{
if ( entry1 == entry2 || entry1 == entry3 || entry2 == entry3 )
continue;
check( results, entry1, entry2, entry3 );
}
}
}
}
}

Related

Remove duplicates from list with objects based on an attribute of an object

So i have this Arraylist of objects,those objects have name and timestamp as attributes.I need a method that when it finds a duplicate name in the arraylist,it removes the object with the smaller timpestamp.It seem pretty easy in theory but i am really stuck.What i ve tried is this :
for (int j=0; j<travellers.size(); j++){
if ( (travellers.get(i).getName() ).equals( sorted_travellers.get(j).getName() ) ){
if (travellers.get(i).getTimestamp() < sorted_travellers.get(j).getTimestamp()){
sorted_travellers.remove(j);
}else if (travellers.get(i).getTimestamp() > sorted_travellers.get(j).getTimestamp()){
sorted_travellers.remove(i);
}
}
}
}
You could try the following:
List<Traveller> travellers = new ArrayList<>();
travellers.add(new Traveller("test", 0L));
travellers.add(new Traveller("test", 1L));
Map<String, Traveller> latestTravellers = new HashMap<>();
for (var traveller : travellers) {
latestTravellers.merge(traveller.getName(), traveller,
(existing, incoming) -> incoming.getTimestamp() > existing.getTimestamp() ? incoming : existing);
}
Collection<Traveller> updatedTravellers = latestTravellers.values();
public static List<Traveler> filter(List<Traveler> travelers) {
List<String> found = new ArrayList<>();
return travelers.stream()
.sorted((t1, t2) -> -t1.timestamp.compareTo(t2.timestamp))
.filter(t -> !found.contains(t.name))
.peek(t -> found.add(t.name))
.collect(Collectors.toList());
}
If you don't mind using StreamEx - it can be done nice and easy:
StreamEx.of(travelers)
.sorted(Comparator.comparing(Traveler::getName))
.collapse((t1,t2) -> t1.getName().equals(t2.getName()),
(t1,t2) -> {
if(t1.getTimestamp().compareTo(t2.getTimestamp()) > 0){
return t1;
}
return t2;
}).toList();
Another solution, which doesn't allocate any additional memory and has n*lgn time complexity could be:
travelers.sort(Comparator.comparing(Traveler::getName)
.thenComparing(Traveler::getTimestamp).reversed()
);
//Now list sorted in order when all items with the same name are near and highest timestamp is first
Iterator<Traveler> iterator = travelers.iterator();
Traveler current = null;
Traveler next = null;
while (iterator.hasNext()) {
if (current == null) {
current = iterator.next();
} else {
next = iterator.next();
if (next.getName().equals(current.getName())) {
iterator.remove();
} else {
current = next;
}
}
}
To remove duplicates, and keep only the record with the latest timestamp, you can do it like this:
List<Traveller> travellers = ...
// Determine traveller objects to keep.
Map<String, Traveller> byName = new HashMap<>();
for (Traveller traveller : travellers) {
byName.merge(traveller.getName(), traveller,
(a, b) -> a.getTimestamp() > b.getTimestamp() ? a : b);
}
// Remove traveller objects that shouldn't be kept.
// Use iterator to prevent ConcurrentModificationException.
for (Iterator<Traveller> iter = travellers.iterator(); iter.hasNext(); ) {
Traveller traveller = iter.next();
if (traveller != byName.get(traveller.getName()))
iter.remove();
}

How to compare two maps by their values and print max value for each key(key is common in both map)

void displayFeedback(){
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
//same staffs taking two different subjects maths and english
//values taken as feedback for each subject
// i need to compare both subject feedback and print only max as o/p
System.out.println(maths);
System.out.println(maths.entrySet());
//maths.
//maths.entrySet();
//Collections.max(coll, comp)
Map<String, Integer> top = new HashMap<String, Integer>();
System.out.println(maths.size());
for (Map.Entry<String, Integer> math: maths.entrySet()){
for (Map.Entry<String, Integer> eng: english.entrySet()){
//for(int i = 0;i<maths.size();i++){
//math.comparingByValue()
System.out.println(math.getValue()+" "+eng.getValue());
//Collections.max(math.getValue(), eng.getValue());
if(math.getValue() <= eng.getValue()){
//System.out.println(" math <= eng");
}else
{
//System.out.println("math > eng");
}
Teachers are common in both map ,they are handling both maths and eng subject
and their feed back in each subjeck getting as values for both subjects
I need to compare and find maximum feedback value and print only max value to
each teacher....t
teacher is common key in both map
you can get the value of english feedback map using the math entrySet() iterator key and select the max value
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
System.out.println(english.entrySet());
System.out.println(maths.entrySet());
Map<String, Integer> top = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> math: maths.entrySet()){
System.out.println( "Teacher : " +math.getKey() + " Max Feedback :" + Math.max(math.getValue(), english.get(math.getKey())));
}
Here is mine suggstion ;)
private void printMaxValues(Map<String, Integer> first, Map<String, Integer> second) {
Set<String> keys = first.keySet();
for (String s : keys) {
Integer val1 = first.get(s);
Integer val2 = second.get(s);
if (val1 == null && val2 == null) {
System.out.println("No values for key: " + s);
} else if (val1 == null) {
System.out.println(s + "=" + val2);
} else if (val2 == null) {
System.out.println(s + "=" + val1);
} else {
System.out.println(s + "=" + Math.max(val1, val2));
}
}
}
You can do as follow :
1. build a map from the subjects as key is the teacher, and value is all its feebacks
Map<String, List<Integer>> feedbacks =
Stream.of(maths, english)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(),
e -> new ArrayList<Integer>(Arrays.asList(e.getValue())),
(l1, l2)-> {l1.addAll(l2); return l1;}));
System.out.println(feedbacks); //{Subaksha=[80, 60], Nirmala=[70, 75]}
2. buid a map from the feedbacks as key is the teacher, and value is the max of its feedback
Map<String, Integer> maxs = feedbacks.entrySet().stream()
.collect(Collectors.toMap(e->e.getKey(),
e-> e.getValue().stream().max(Integer::compare).get()));
System.out.println(maxs); //{Nirmala=75, Subaksha=80}
You can try something like:
static <V extends Comparable<V>>
boolean valuesEquals(Map<?,V> map1, Map<?,V> map2) {
List<V> values1 = new ArrayList<V>(map1.values());
List<V> values2 = new ArrayList<V>(map2.values());
Collections.sort(values1);
Collections.sort(values2);
return values1.equals(values2);
}
or
map1.keySet().equals(map2.keySet())
to compare values. And if equals is true then you find greater Key of the two values.
Refer : http://thelogofthewook.blogspot.com/2011/12/hashmap-comparison-how-to-compare-two.html
You could create a helper Collection for each Teacher key (Map<String, Collection<Integer>>) that you fill up by iterating over both existing maps and then putting the scores into them.
Or you could create a Java 8 Stream over both map's entrySets and then merge them into one Map by calling the Collectors utils:
Map<String, Optional<Integer>> data = Arrays.asList( map1.entrySet(), map2.entrySet() )
.stream()
.flatMap( Set::stream )
.collect( Collectors.groupingBy( Entry::getKey, HashMap::new, Collectors.mapping( Entry::getValue, Collectors.maxBy( Integer::compare ) ) ) );
for( Entry<String, Optional<Integer>> entry : data.entrySet() )
{
System.out.println( entry.getKey() + ": " + entry.getValue().orElse( 0 ) );
}
If you are sure that you will get feedback for every teacher in every subject, you can use following.
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("T1", 75);
maths.put("T2", 68);
maths.put("T3", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("T1",60);
english.put("T2",70);
english.put("T3",79);
for(String s:maths.keySet())
System.out.println("Teacher "+s+" with max feedback:"+Math.max(maths.get(s),english.get(s)));

Java 8 function that takes a List<V> and return a HashMap<K, List<V>>

I am trying to create a function in Java 8 that takes a list as a parameter and returns a HashMap as a result.
I already did it in Java 7 but I want to do it by using Java 8 streams:
Here is the code in Java 7:
public static HashMap<String, List<Eleve>> getMentions(List<Eleve> liste){
HashMap<String, List<Eleve>> listeMentions = new HashMap<String, List<Eleve>>();
List<Eleve> noMention = new ArrayList<Eleve>();
List<Eleve> mentionAB = new ArrayList<Eleve>();
List<Eleve> mentionB = new ArrayList<Eleve>();
List<Eleve> mentionTB = new ArrayList<Eleve>();
for (Eleve eleve: liste) {
if (eleve.getAverage()>=12 && eleve.getAverage()<14) {mentionAB.add(eleve);}
else if(eleve.getAverage()>=14 && eleve.getAverage()<16) {mentionB.add(eleve);}
else if (eleve.getAverage()>=16) {mentionTB.add(eleve);}
else{noMention.add(eleve);}
}
listeMentions.put("No mention", noMention);
listeMentions.put("Mention AB", mentionAB);
listeMentions.put("Mention B", mentionB);
listeMentions.put("Mention TB", mentionTB);
return listeMentions;
}
I tried with the collect(Collectors.toMap) stream but I did not get the estimated outcome.
You can create a method that accepts an int average and returns the corresponding group for that average.
public static String getGroup (int average) {
if (average >= 12 && average < 14) {
return "Mention AB";
} else if(average >= 14 && average < 16) {
return "Mention B";
} else if (average >= 16) {
return "Mention TB";
} else {
return "No mention";
}
}
Now you can use Collectors.groupingBy to group your instances by this criteria:
Map<String, List<Eleve>> listeMentions =
liste.stream()
.collect(Collectors.groupingBy(e -> getGroup(e.getAverage())));
Alternately, you can pass the Eleve instance to the getGroup() method, so the stream pipeline will become:
Map<String, List<Eleve>> listeMentions =
liste.stream()
.collect(Collectors.groupingBy(e -> getGroup(e)));
or, if you make getGroup() an instance method of Eleve (i.e public String getGroup() {...}):
Map<String, List<Eleve>> listeMentions =
liste.stream()
.collect(Collectors.groupingBy(Eleve::getGroup));
Thanks a lot for the help.
The groupingBy method was indeed the solution.
Instead of creating a function I created an ENUM and then use it into my steam, but It works the same way.
Here is the code:
public enum Mention
{
BIEN, ASSEZ_BIEN,TRES_BIEN, BOF;
public static Mention find(double average) {
if (average >= 12 && average < 14) {
return ASSEZ_BIEN;
} else if(average >= 14 && average < 16) {
return BIEN;
} else if (average >= 16) {
return TRES_BIEN;
} else {
return BOF;
}
}
}
public static Map<Object, List<Eleve>> getMentions8(List<Eleve> liste){
Map<Object, List<Eleve>> listeMentions =
liste.stream()
.collect(Collectors.groupingBy(e -> Mention.find(e.average)));
return listeMentions;
}

Add up the column values in while loop if it comes repetedly

I am creating a table from ajax and the getting of values using a while loop:
while (rstdb.next()) {
rstdb.getInt(1)+"~"+ rstdb.getInt(2);
}
In my while loop my rstdb.getInt(1) will be 2,2,2,2,2,3,3...... and second values rstdb.getInt(2) are 10,20,30,40,50,10,20,.....
I need to sum up the values specific to 2 and values specific to 3 seperate.
ie,
It means 10+20+30+40+50 =150 for 2 and 10+20 =30 for 3.
It may contain single values also for example it may have 4,5,5,6,7,7....
How can I do that?
I need something like:
while (rstdb.next()) {
rstdb.getInt(1)+"~"+ rstdb.getInt(2)+"~"+sum;
}
The variable sum should contain the sum up value.
Use map for this. You can have a map which should be mapping the specific number with sum of it's corresponding value.
int c1, c2;
Map<Integer, Integer> sum = new HashMap<>();
while (rstdb.next()) {
c1 = rstdb.getInt(1);
c2 = rstdb.getInt(2);
if(sum.containsKey(c1)) {
sum.put(c1, sum.get(c1) + c2);
// ^ will return current sum of second column
} else {
sum.put(c1, c2);
}
rstdb.getInt(1)+"~"+ rstdb.getInt(2)+"~"+sum.get(c1);
}
You can use an integer to integer map:
Map<Integer, Integer> integerMap = new HashMap<>();
while (rstdb.next()) {
int column1 = rstdb.getInt(1);
int column2 = rstdb.getInt(2);
if (integerMap.containsKey(column1)) {
int currentSum = integerMap.get(column1);
integerMap.put(column1, currentSum + column2);
} else {
integerMap.put(column1, column2);
}
}
Edit: to print out the map, you can use loop through the entrySet of the map:
for (Map.Entry entry : integerMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}

Custom Generic Class as Key to Hash Map Issue

I have this following test code:
public static final String[] list = {
"apple","ball","cat","dog","egg","fan","girl","hat","igloo","jerk"
};
...
HashMap<DoubleKey<Integer, Integer>, String> hm = new HashMap<DoubleKey<Integer, Integer>, String>();
Set<DoubleKey<Integer, Integer>> s = new TreeSet<DoubleKey<Integer, Integer>>();
Random g = new Random();
for(int i=0; i<10; i++){
int first = g.nextInt(9999) + 1000;
int second = g.nextInt(9999) + 1000;
DoubleKey<Integer, Integer> k1 = new DoubleKey<Integer, Integer>(first, second);
DoubleKey<Integer, Integer> k2 = new DoubleKey<Integer, Integer>(first, second);
s.add(k1);
hm.put(k2, list[i]);
}
Set<DoubleKey<Integer, Integer>> ts = hm.keySet();
Iterator<DoubleKey<Integer, Integer>> itr = ts.iterator();
while(itr.hasNext()){
DoubleKey<Integer, Integer> k = itr.next();
System.out.println(k.getFirstKey().toString() + " + " + k.getSecondKey().toString() + " -> " + hm.get(k).toString());
}
System.out.println("----");
Iterator<DoubleKey<Integer, Integer>> sItr = s.iterator();
while(sItr.hasNext()){
DoubleKey<Integer, Integer> k = sItr.next();
String currStr = hm.get(k);
System.out.println(k.getFirstKey().toString() + " + " + k.getSecondKey().toString() + " -> " + currStr);
}
What I did is to create a Custom Generic Class DoubleKey<K, J> to contain a key having two parts. As you can see, the Set s and the keys of HashMap hm are have the same components, but was instantiated differently (k1 = k2). When I try to get a value using the keys on s to hm, it returns null, though at the first printing it shows the correct mapping.
Sample Output:
3922 + 2544 -> girl
9267 + 3750 -> hat
3107 + 10929 -> apple
5162 + 8834 -> fan
8786 + 1125 -> cat
10650 + 4078 -> egg
3808 + 7363 -> jerk
1364 + 7657 -> dog
1364 + 4412 -> ball
1583 + 1460 -> igloo
----
10650 + 4078 -> null
1364 + 4412 -> null
1364 + 7657 -> null
1583 + 1460 -> null
3107 + 10929 -> null
3808 + 7363 -> null
3922 + 2544 -> null
5162 + 8834 -> null
8786 + 1125 -> null
9267 + 3750 -> null
This is my DoubleKey implemention:
public class DoubleKey<K extends Comparable<K>,J extends Comparable<J>> implements Comparable<DoubleKey<K,J>>{
private K key1;
private J key2;
public DoubleKey(K key1, J key2){
this.key1 = key1;
this.key2 = key2;
}
public K getFirstKey(){
return this.key1;
}
public J getSecondKey(){
return this.key2;
}
// need for Comparable interface
public int compareTo(DoubleKey<K,J> aThat){
// NOTE: check for nulls
return (this.key1.toString() + this.key2.toString()).compareTo(aThat.key1.toString() + aThat.key2.toString());
}
public boolean equals(DoubleKey<K,J> aThat){
return (this.key1.toString() + this.key2.toString()).equals(aThat.key1.toString() + aThat.key2.toString());
}
}
How did it happened? Can two objecst (in this case from a custom generic) be different eve3n if they have instantiated with 2 same values? How can I correct this? I hope someone can help me here. Thanks!
Additionally to .hashCode(), you should have an implementation of equals(Object), not (only) equals(DoubleKey<...>), since otherwise you'll have two independent methods here (and only the first one is actually called by the HashMap). Here is a proposal:
public boolean equals(Object other) {
if(this == other)
return true;
if(!(other instanceof DoubleKey))
return false;
DoubleKey that = (DoubleKey)other;
return (this.key1 == null ? that.key1 == null : this.key1.equals(that.key1)) &&
(this.key2 == null ? that.key2 == null : this.key2.equals(that.key2));
}
The hashCode method should be made to fit this, too, for example like this:
public int hashCode() {
return key1.hashCode() * 3 + key2.hashCode() * 5;
}
Your key1.toString()+key2.toString() comparison is a bit dangerous, as it lets (1, 21).equals((12,1)) be true, which is usually not intended. The same is true for your compareTo method - compare the components using their compareTo method, not the concatenated String.
Learn this lesson now: If you override the equals method (as you have done), then you MUST override the hashcode method too. That method is used for various things, including looking up items in HashMaps.
Where is DoubleKey class's hashCode method override? I don't think that it will work as a proper key unless you implement this because otherwise your two objects will be considered different.

Categories