2022-06-30

Java Map

Map map = new HashMap<>(); 
IntStream.range(0,4).forEach(i->map.put("str"+i,i)); 
System.out.println(map);
 ///////////////////////////////////////////
   

        List<Map.Entry<String, Integer>> list = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .sorted((a,b)->b.getValue()-a.getValue())
                .limit(3).collect(Collectors.toList());
        System.out.printf("\nList:%s:%d", list.get(0).getKey(), list.get(0).getValue());
        list.forEach(entry -> System.out.printf("\n%s:%d", entry.getKey(), entry.getValue()));

        Optional<Map.Entry<String, Integer>> opt = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .limit(3).findFirst();
        System.out.printf("\nOpt:%s:%d", opt.get().getKey(), opt.get().getValue());

Map<String, Integer> map2 = map.entrySet().stream()
                .sorted((a,b)->b.getValue()-a.getValue())
                .limit(1).collect(Collectors.toMap(m->m.getKey(),m->m.getValue()));
        System.out.println("map2:" + map2);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
map.forEach((k, v) -> System.out.println((k + ":" + v)));
map.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); }

for (String key : map.keySet()) { System.out.println(key + ":" + map.get(key)); }

for (Integer value : map.values()) { System.out.println(value); }

2022-06-16

Java ArrayDeque

ArrayDeque<Integer> q = new ArrayDeque<>();

IntStream.range(0, 5).forEach((i) -> {q.addFirst(i);});
System.out.println(q);

IntStream.range(0, 5).forEach((i) -> {
System.out.printf("%d, ", q.pollFirst());
} );

System.out.println(q);

        ArrayDeque<Integer> q = new ArrayDeque<>();


        IntStream.range(0, 5).forEach((i) -> { q.addFirst(i);  });

        System.out.println(q);

//[4, 3, 2, 1, 0]


        IntStream.range(0, 5).forEach((i) ->{System.out.printf("%d,",q.pollFirst());});

//4, 3, 2, 1, 0, 


        System.out.println(q); //[]

        //q.clear();

 ###################################




2022-06-09

Java Collections

 https://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

1. Four Different Ways to Check If an Array Contains a Value

1) Using List:

public static boolean useList(String[] arr, String targetValue) {
	return Arrays.asList(arr).contains(targetValue);
}

2) Using Set:

public static boolean useSet(String[] arr, String targetValue) {
	Set<String> set = new HashSet<String>(Arrays.asList(arr));
	return set.contains(targetValue);
}

3) Using a simple loop:

public static boolean useLoop(String[] arr, String targetValue) {
	for(String s: arr){
		if(s.equals(targetValue))
			return true;
	}
	return false;
}

4) Using Arrays.binarySearch():
binarySearch() can ONLY be used on sorted arrays. If the array is sorted, you can use the following code to search the target element:

public static boolean useArraysBinarySearch(String[] arr, String targetValue) {	
	int a =  Arrays.binarySearch(arr, targetValue);
	if(a > 0)
		return true;
	else
		return false;
}
Comparator<Arrow> cmp = Comparator
        .comparing(Arrow::getWeight)
        .thenComparing(Arrow::getCount, Comparator.reverseOrder())
        .thenComparing((a, b) -> Character.compare(a.ch, b.ch));

listChar.sort(cmp);
 
List<Employee> sortedEmployees = employees.stream()
                .sorted(cmp).collect(Collectors.toList());

////////////////////////////////////////////////////////////////////////////////////////////////////////// 

 Collections.sort(scores, new Comparator<Score>() {

        public int compare(Score o1, Score o2) {
            return o2.getScores().get(0).compareTo(o1.getScores().get(0));
        }
    });
Collections.sort(scores, (s1, s2) -> { return s1 - s2; });
Collections.binarySearch(cats, key);

String s = list.stream().map(Objects::toString)
.sorted(Comparator.reverseOrder()).collect(Collectors.joining(""));
int[] arr = {1,2,3};
var list = Arrays.stream(arr).boxed().toList();

list.subArray(int begin, int end)