Open-source News

OneXPlayer AMD Sensor Driver Coming For Linux 6.2

Phoronix - Sun, 11/13/2022 - 18:45
Last month I wrote about a Linux sensor driver being written for the AMD-powered OneXPlayer Mini gaming handheld device. The good news is that this driver has matured enough that it's now queued for introduction in the Linux 6.2 kernel...

Mold 1.7 Released But May Need To Change Software License If Funding Not Secured

Phoronix - Sun, 11/13/2022 - 18:36
Mold is the modern, high performance, and open-source linker taking on the likes of LLVM LLD and GNU Gold. Mold 1.7 has been released as the newest update to this very promising linker, but unfortunately the lead developer is evaluating a license change. Due to still losing money over working on it full-time, he may be forced to change the software license without obtaining sustainable funding...

Running The Open-Source Upstream V3D Driver On The Raspberry Pi 4 & Newer

Phoronix - Sun, 11/13/2022 - 17:58
As of this summer the upstream, open-source Broadcom V3D direct rendering manager kernel driver has enabled support for the Raspberry Pi 4 (and newer). With the latest mainline Linux kernel builds this means the ability to enjoy accelerated graphics on the Raspberry Pi hardware paired with the latest Mesa OpenGL/Vulkan driver code without worrying about out-of-tree patches...

Use a hashmap in Java

opensource.com - Sun, 11/13/2022 - 16:00
Use a hashmap in Java Seth Kenlon Sun, 11/13/2022 - 03:00

In the Java programming language, a hashmap is a list of associated values. Java uses hashmaps to store data. If you keep a lot of structured data, it's useful to know the various retrieval methods available.

More on Java What is enterprise Java programming? An open source alternative to Oracle JDK Java cheat sheet Red Hat build of OpenJDK Free online course: Developing cloud-native applications with microservices Fresh Java articles Create a hashmap

Create a hashmap in Java by importing and using the HashMap class. When you create a hashmap, you must define what constitutes a valid value (such as Integer or String). A hashmap holds pairs of values.

package com.opensource.example;

import java.util.HashMap;

public class Mapping {
    public static void main(String[] args) {
        HashMap<String, String> myMap = new HashMap<>();
        myMap.put("foo", "hello");
        myMap.put("bar", "world");

        System.out.println(myMap.get("foo") + " " + myMap.get("bar"));
        System.out.println(myMap.get("hello") + " " + myMap.get("world"));
    }
}

The put method allows you to add data to your hashmap, and the get method retrieves data from the hashmap.

Run the code to see the output. Assuming you've saved the code in a file called main.java, you can run it directly with the java command:

$ java ./main.java
hello world
null null

Calling the second values in the hashmap returns null, so the first value is essentially a key and the second a value. This is known as a dictionary or associative array in some languages.

You can mix and match the types of data you put into a hashmap as long as you tell Java what to expect when creating it:

package com.opensource.example;

import java.util.HashMap;

public class Mapping {
    public static void main(String[] args) {
        HashMap<Integer, String> myMap = new HashMap<>();
        myMap.put(71, "zombie");
        myMap.put(2066, "apocalypse");

        System.out.println(myMap.get(71) + " " + myMap.get(2066));
    }
}

Run the code:

$ java ./main.java
zombie apocalypseIterate over a hashmap with forEach

There are many ways to retrieve all data pairs in a hashmap, but the most direct method is a forEach loop:

package com.opensource.example;

import java.util.HashMap;

public class Mapping {

    public static void main(String[] args) {
        HashMap<String, String> myMap = new HashMap<>();
        myMap.put("foo", "hello");
        myMap.put("bar", "world");

        // retrieval
        myMap.forEach( (key, value) ->
                System.out.println(key + ": " + value));
    }
}

Run the code:

$ java ./main.java
bar: world
foo: helloStructured data

Sometimes it makes sense to use a Java hashmap so you don't have to keep track of dozens of individual variables. Once you understand how to structure and retrieve data in a language, you're empowered to generate complex data in an organized and convenient way. A hashmap isn't the only data structure you'll ever need in Java, but it's a great one for related data.

A hashmap is a useful way to store structured data in Java.

Image by:

Photo by Nathan Dumlao, Unsplash

Java What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Google Moves Ahead With Providing No-Cost Open-Source Silicon Manufacturing From GlobalFoundries

Phoronix - Sun, 11/13/2022 - 08:40
Google announced funding for silicon manufacturing for participating open-source projects using the process design kit with GlobalFoundries...

MPV 0.35 Media Player Released With PipeWire Backend, Wayland DMA-BUF Support

Phoronix - Sat, 11/12/2022 - 22:09
MPV as the popular open-source media player forked from MPlayer/mplayer2 and leveraging the FFmpeg library is out with its newest feature release...

wlroots 0.16 Released With More Stable Vulkan Renderer, High Resolution Scrolling

Phoronix - Sat, 11/12/2022 - 20:27
The wlroots Wayland compositor support library that started out as a companion project to Sway is out with a shiny new feature release...

AMD Makes More Updates Around New Radeon GPU Driver Code In Linux 6.2

Phoronix - Sat, 11/12/2022 - 20:10
Following last week's batch of AMDGPU/AMDKFD changes slated for Linux 6.2, on Friday another round of feature patches were sent in for DRM-Next ahead of the Linux 6.2 cycle. There is continued work around new IP blocks presumably for RDNA3 and MI300 graphics while given the more modularized development approach with block-by-block enablement makes it harder to ascertain the current status...

Pages