Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Main0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*;
import java.util.Map.Entry;

class Main0 {

public static void main(String[] args) {

// create a map and store elements to it
LinkedHashMap<String, String> capitals = new LinkedHashMap();
capitals.put("Nepal", "Kathmandu");
capitals.put("India", "New Delhi");
capitals.put("United States", "Washington");
capitals.put("England", "London");
capitals.put("Australia", "Canberra");

// call the sortMap() method to sort the map
Map<String, String> result = sortMap(capitals);

for (Map.Entry entry : result.entrySet()) {
System.out.print("Key: " + entry.getKey());
System.out.println(" Value: " + entry.getValue());
}
}

public static LinkedHashMap sortMap(LinkedHashMap map) {
List <Entry<String, String>> capitalList = new LinkedList<>(map.entrySet());

// call the sort() method of Collections
Collections.sort(capitalList, (l1, l2) -> l1.getValue().compareTo(l2.getValue()));

// create a new map
LinkedHashMap<String, String> result = new LinkedHashMap();

// get entry from list to the map
for (Map.Entry<String, String> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}

return result;
}
}
21 changes: 21 additions & 0 deletions armstrong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Armstrong {

public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}

if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}