diff --git a/Main0.java b/Main0.java new file mode 100644 index 0000000..666e8f5 --- /dev/null +++ b/Main0.java @@ -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 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 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 > 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 result = new LinkedHashMap(); + + // get entry from list to the map + for (Map.Entry entry : capitalList) { + result.put(entry.getKey(), entry.getValue()); + } + + return result; + } +} diff --git a/armstrong.java b/armstrong.java new file mode 100644 index 0000000..dadb749 --- /dev/null +++ b/armstrong.java @@ -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."); + } +}